Plugging in main

This commit is contained in:
sfigato 2023-03-13 01:09:07 +01:00
parent a252110fcd
commit 5397775ab7
Signed by: blallo
GPG Key ID: C530464EEDCF489A

View File

@ -19,10 +19,13 @@ import (
) )
var ( var (
debug = flag.Bool("debug", false, "Enable debug logging") debug = flag.Bool("debug", false, "Enable debug logging")
addr = flag.String("addr", ":8080", "Address to bind to, in the 'ipaddress:port' format") 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") startNow = flag.Bool("start", false, "If set, try to start the process now")
tlsKey = flag.String("tls-key", "", "Path to key file for TLS connection") 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() { func main() {
@ -41,10 +44,26 @@ func main() {
logger.SetLevel(logz.LogDebug) logger.SetLevel(logz.LogDebug)
} }
prog := flag.Arg(0) runnableName := flag.Arg(0)
cmdLine := flag.Args()[1:]
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 { if err != nil {
logger.Err(map[string]any{ logger.Err(map[string]any{
"msg": "Failed to start", "msg": "Failed to start",
@ -83,15 +102,6 @@ func main() {
"context": "os", "context": "os",
}) })
} }
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
case <-stop: case <-stop:
resp := <-radio.Stop() resp := <-radio.Stop()
if resp != nil { if resp != nil {
@ -106,15 +116,6 @@ func main() {
"context": "os", "context": "os",
}) })
} }
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
case <-status: case <-status:
resp := <-radio.Status() resp := <-radio.Status()
for i, line := range resp.([]string) { for i, line := range resp.([]string) {
@ -128,24 +129,26 @@ func main() {
} }
}() }()
go func() { if *startNow {
resp, err := withTimeout(ctx, radio.Start()) go func() {
if err != nil { resp, err := withTimeout(ctx, radio.Start())
logger.Err(map[string]any{ if err != nil {
"msg": "Cannot start", logger.Err(map[string]any{
"context": "os", "msg": "Cannot start",
"err": err.Error(),
})
} else {
if resp != nil {
logger.Info(map[string]any{
"msg": "Started",
"context": "os", "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) handler, err := setupHandler(radio, logger, *addr)
if err != nil { if err != nil {