Rewrite shell signal handling

This commit is contained in:
Alexey Khit
2023-05-20 06:29:14 +03:00
parent e29307125c
commit 82a8e07b66
5 changed files with 59 additions and 48 deletions

View File

@@ -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())
}