From c13f6d5ca860f45d0fcfe68b3ed438817a7dec4c Mon Sep 17 00:00:00 2001 From: Tordarus Date: Sat, 11 Dec 2021 16:53:54 +0100 Subject: [PATCH] check for system signals --- main.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 37be73d..ffdffb3 100644 --- a/main.go +++ b/main.go @@ -6,14 +6,21 @@ import ( "io/ioutil" "os" "os/exec" + "os/signal" "path/filepath" "strings" + "syscall" ) var ( envFile = flag.String("env", "$HOME/.config/i3-wrapper/environment", "environment file with variable definitions") ) +var ( + i3 *os.Process + running bool +) + func main() { logFile, err := os.Create(filepath.Join(os.Getenv("HOME"), ".i3.log")) if err != nil { @@ -24,7 +31,18 @@ func main() { os.Stdout = logFile os.Stderr = logFile - for { + fmt.Println("environment file:", os.ExpandEnv(*envFile)) + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go func() { + sig := <-sigs + running = false + i3.Signal(sig) + }() + + running = true + for running { cmd := exec.Command("i3") cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -41,6 +59,7 @@ func main() { if err != nil { fmt.Fprintln(os.Stderr, err) } + i3 = cmd.Process err = cmd.Wait() if err != nil {