From 5397775ab7f90dc68f47aac88fd5c89477307279 Mon Sep 17 00:00:00 2001 From: Blallo Date: Mon, 13 Mar 2023 01:09:07 +0100 Subject: [PATCH] Plugging in main --- cmd/broadcast/main.go | 83 ++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/cmd/broadcast/main.go b/cmd/broadcast/main.go index e2d14a0..f8cee5e 100644 --- a/cmd/broadcast/main.go +++ b/cmd/broadcast/main.go @@ -19,10 +19,13 @@ import ( ) var ( - debug = flag.Bool("debug", false, "Enable debug logging") - addr = flag.String("addr", ":8080", "Address to bind to, in the 'ipaddress:port' format") - tlsCert = flag.String("tls-cert", "", "Path to certificate file for TLS connection") - tlsKey = flag.String("tls-key", "", "Path to key file for TLS connection") + debug = flag.Bool("debug", false, "Enable debug logging") + addr = flag.String("addr", ":8080", "Address to bind to, in the 'ipaddress:port' format") + startNow = flag.Bool("start", false, "If set, try to start the process now") + tlsCert = flag.String("tls-cert", "", "Path to certificate file for TLS connection") + tlsKey = flag.String("tls-key", "", "Path to key file for TLS connection") + systemd = flag.Bool("systemd", false, "Interpret as systemd unit") + user = flag.Bool("systemd-as-user", false, "Connect to systemd via DBus as current user") ) func main() { @@ -41,10 +44,26 @@ func main() { logger.SetLevel(logz.LogDebug) } - prog := flag.Arg(0) - cmdLine := flag.Args()[1:] + runnableName := flag.Arg(0) - radio, err := broadcast.NewRadio(logger, prog, cmdLine...) + var runnable broadcast.Runnable + var err error + + if *systemd { + runnable = broadcast.NewSystemdUnit(logger, runnableName, *user) + } else { + cmdLine := flag.Args()[1:] + runnable, err = broadcast.NewProcess(logger, runnableName, cmdLine...) + } + if err != nil { + logger.Err(map[string]any{ + "msg": "Failed to create runnable process", + "err": err.Error(), + }) + os.Exit(2) + } + + radio, err := broadcast.NewRadio(logger, runnable) if err != nil { logger.Err(map[string]any{ "msg": "Failed to start", @@ -83,15 +102,6 @@ func main() { "context": "os", }) } - } - } - }() - - go func() { - for { - select { - case <-ctx.Done(): - return case <-stop: resp := <-radio.Stop() if resp != nil { @@ -106,15 +116,6 @@ func main() { "context": "os", }) } - } - } - }() - - go func() { - for { - select { - case <-ctx.Done(): - return case <-status: resp := <-radio.Status() for i, line := range resp.([]string) { @@ -128,24 +129,26 @@ func main() { } }() - go func() { - resp, err := withTimeout(ctx, radio.Start()) - if err != nil { - logger.Err(map[string]any{ - "msg": "Cannot start", - "context": "os", - "err": err.Error(), - }) - } else { - if resp != nil { - logger.Info(map[string]any{ - "msg": "Started", + if *startNow { + go func() { + resp, err := withTimeout(ctx, radio.Start()) + if err != nil { + logger.Err(map[string]any{ + "msg": "Cannot start", "context": "os", - "resp": resp.(error).Error(), + "err": err.Error(), }) + } else { + if resp != nil { + logger.Info(map[string]any{ + "msg": "Started", + "context": "os", + "resp": resp.(error).Error(), + }) + } } - } - }() + }() + } handler, err := setupHandler(radio, logger, *addr) if err != nil {