From 82a8e07b66da8c44c83e63a54c6d0ae78ad1bcfc Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sat, 20 May 2023 06:29:14 +0300 Subject: [PATCH] Rewrite shell signal handling --- cmd/go2rtc_hass/main.go | 20 ++++++++++++++++++++ cmd/go2rtc_rtsp/main.go | 10 ++-------- main.go | 10 ++-------- pkg/shell/env.go | 32 -------------------------------- pkg/shell/shell.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 48 deletions(-) create mode 100644 cmd/go2rtc_hass/main.go delete mode 100644 pkg/shell/env.go diff --git a/cmd/go2rtc_hass/main.go b/cmd/go2rtc_hass/main.go new file mode 100644 index 00000000..42c2d150 --- /dev/null +++ b/cmd/go2rtc_hass/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/AlexxIT/go2rtc/internal/api" + "github.com/AlexxIT/go2rtc/internal/app" + "github.com/AlexxIT/go2rtc/internal/hass" + "github.com/AlexxIT/go2rtc/internal/streams" + "github.com/AlexxIT/go2rtc/pkg/shell" +) + +func main() { + app.Init() + streams.Init() + + api.Init() + + hass.Init() + + shell.RunUntilSignal() +} diff --git a/cmd/go2rtc_rtsp/main.go b/cmd/go2rtc_rtsp/main.go index 2babffab..07d32564 100644 --- a/cmd/go2rtc_rtsp/main.go +++ b/cmd/go2rtc_rtsp/main.go @@ -4,9 +4,7 @@ import ( "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" - "os" - "os/signal" - "syscall" + "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { @@ -15,9 +13,5 @@ func main() { rtsp.Init() - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs - - println("exit OK") + shell.RunUntilSignal() } diff --git a/main.go b/main.go index 6255841d..97476cd1 100644 --- a/main.go +++ b/main.go @@ -28,9 +28,7 @@ import ( "github.com/AlexxIT/go2rtc/internal/tapo" "github.com/AlexxIT/go2rtc/internal/webrtc" "github.com/AlexxIT/go2rtc/internal/webtorrent" - "os" - "os/signal" - "syscall" + "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { @@ -66,9 +64,5 @@ func main() { ngrok.Init() debug.Init() - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs - - println("exit OK") + shell.RunUntilSignal() } diff --git a/pkg/shell/env.go b/pkg/shell/env.go deleted file mode 100644 index a8867f6a..00000000 --- a/pkg/shell/env.go +++ /dev/null @@ -1,32 +0,0 @@ -package shell - -import ( - "os" - "regexp" - "strings" -) - -func ReplaceEnvVars(text string) string { - re := regexp.MustCompile(`\${([^}{]+)}`) - return re.ReplaceAllStringFunc(text, func(match string) string { - key := match[2 : len(match)-1] - - var def string - var dok bool - - if i := strings.IndexByte(key, ':'); i > 0 { - key, def = key[:i], key[i+1:] - dok = true - } - - if value, vok := os.LookupEnv(key); vok { - return value - } - - if dok { - return def - } - - return match - }) -} diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index 0b080876..719c0e68 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -1,7 +1,11 @@ package shell import ( + "os" + "os/signal" + "regexp" "strings" + "syscall" ) func QuoteSplit(s string) []string { @@ -39,3 +43,34 @@ func QuoteSplit(s string) []string { } return a } + +func ReplaceEnvVars(text string) string { + re := regexp.MustCompile(`\${([^}{]+)}`) + return re.ReplaceAllStringFunc(text, func(match string) string { + key := match[2 : len(match)-1] + + var def string + var dok bool + + if i := strings.IndexByte(key, ':'); i > 0 { + key, def = key[:i], key[i+1:] + dok = true + } + + if value, vok := os.LookupEnv(key); vok { + return value + } + + if dok { + return def + } + + return match + }) +} + +func RunUntilSignal() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + println("exit with signal:", (<-sigs).String()) +}