From 303051fd46bea2e6f035419628479a8e5fbe8322 Mon Sep 17 00:00:00 2001 From: Tordarus Date: Tue, 28 Sep 2021 20:42:31 +0200 Subject: [PATCH 1/3] initial commit --- .gitignore | 1 + go.mod | 3 +++ main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c1e4aa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +i3-wrapper* diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a072565 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.tordarus.net/tordarus/i3-wrapper + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..2a1529e --- /dev/null +++ b/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" +) + +func main() { + logFile, err := os.Create("/home/tordarus/.i3.log") + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + defer logFile.Close() + + os.Stdout = logFile + os.Stderr = logFile + + for { + cmd := exec.Command("i3") + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + cmd.Env = os.Environ() + if envData, err := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".config/i3wrapper/environment")); err == nil { + cmd.Env = append(cmd.Env, strings.Split(string(envData), "\n")...) + } else { + fmt.Println(err) + } + + err = cmd.Start() + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + err = cmd.Wait() + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + if _, ok := err.(*exec.ExitError); ok { + break + } + } +} From e3cc32259475b29a50d2d47f9bd05315ab20ac4b Mon Sep 17 00:00:00 2001 From: Tordarus Date: Tue, 28 Sep 2021 21:05:45 +0200 Subject: [PATCH 2/3] fixed env and log file path --- main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 2a1529e..37be73d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "io/ioutil" "os" @@ -9,8 +10,12 @@ import ( "strings" ) +var ( + envFile = flag.String("env", "$HOME/.config/i3-wrapper/environment", "environment file with variable definitions") +) + func main() { - logFile, err := os.Create("/home/tordarus/.i3.log") + logFile, err := os.Create(filepath.Join(os.Getenv("HOME"), ".i3.log")) if err != nil { fmt.Fprintln(os.Stderr, err) } @@ -26,7 +31,7 @@ func main() { cmd.Stderr = os.Stderr cmd.Env = os.Environ() - if envData, err := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".config/i3wrapper/environment")); err == nil { + if envData, err := ioutil.ReadFile(os.ExpandEnv(*envFile)); err == nil { cmd.Env = append(cmd.Env, strings.Split(string(envData), "\n")...) } else { fmt.Println(err) From c13f6d5ca860f45d0fcfe68b3ed438817a7dec4c Mon Sep 17 00:00:00 2001 From: Tordarus Date: Sat, 11 Dec 2021 16:53:54 +0100 Subject: [PATCH 3/3] 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 {