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 (
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 {