2021-09-28 20:42:31 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-28 21:05:45 +02:00
|
|
|
"flag"
|
2021-09-28 20:42:31 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-12-11 16:53:54 +01:00
|
|
|
"os/signal"
|
2021-09-28 20:42:31 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-12-11 16:53:54 +01:00
|
|
|
"syscall"
|
2022-09-17 00:15:05 +02:00
|
|
|
|
|
|
|
"git.milar.in/milarin/slices"
|
2021-09-28 20:42:31 +02:00
|
|
|
)
|
|
|
|
|
2021-09-28 21:05:45 +02:00
|
|
|
var (
|
|
|
|
envFile = flag.String("env", "$HOME/.config/i3-wrapper/environment", "environment file with variable definitions")
|
|
|
|
)
|
|
|
|
|
2021-12-11 16:53:54 +01:00
|
|
|
var (
|
|
|
|
i3 *os.Process
|
|
|
|
running bool
|
|
|
|
)
|
|
|
|
|
2021-09-28 20:42:31 +02:00
|
|
|
func main() {
|
2021-09-28 21:05:45 +02:00
|
|
|
logFile, err := os.Create(filepath.Join(os.Getenv("HOME"), ".i3.log"))
|
2021-09-28 20:42:31 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
defer logFile.Close()
|
|
|
|
|
|
|
|
os.Stdout = logFile
|
|
|
|
os.Stderr = logFile
|
|
|
|
|
2021-12-11 16:53:54 +01:00
|
|
|
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 {
|
2021-09-28 20:42:31 +02:00
|
|
|
cmd := exec.Command("i3")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
cmd.Env = os.Environ()
|
2022-09-17 00:15:05 +02:00
|
|
|
if envData, err := os.ReadFile(os.ExpandEnv(*envFile)); err == nil {
|
|
|
|
cmd.Env = append(cmd.Env, slices.Filter(strings.Split(string(envData), "\n"), IsNotEmpty)...)
|
2021-09-28 20:42:31 +02:00
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-12-11 16:53:54 +01:00
|
|
|
i3 = cmd.Process
|
2021-09-28 20:42:31 +02:00
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 00:15:05 +02:00
|
|
|
|
|
|
|
func IsNotEmpty(str string) bool {
|
|
|
|
return len(strings.TrimSpace(str)) > 0
|
|
|
|
}
|