79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"git.milar.in/milarin/slices"
|
|
)
|
|
|
|
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 {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
}
|
|
defer logFile.Close()
|
|
|
|
os.Stdout = logFile
|
|
os.Stderr = logFile
|
|
|
|
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
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Env = os.Environ()
|
|
if envData, err := os.ReadFile(os.ExpandEnv(*envFile)); err == nil {
|
|
cmd.Env = append(cmd.Env, slices.Filter(strings.Split(string(envData), "\n"), IsNotEmpty)...)
|
|
} else {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
}
|
|
i3 = cmd.Process
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
}
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func IsNotEmpty(str string) bool {
|
|
return len(strings.TrimSpace(str)) > 0
|
|
}
|