filter empty lines in env file

This commit is contained in:
Timon Ringwald 2022-09-17 00:15:05 +02:00
parent c460609d8a
commit d14b67a4e0
3 changed files with 14 additions and 5 deletions

6
go.mod
View File

@ -1,3 +1,5 @@
module git.tordarus.net/tordarus/i3-wrapper module git.milar.in/milarin/i3-wrapper
go 1.17 go 1.18
require git.milar.in/milarin/slices v0.0.6 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s=
git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4=

11
main.go
View File

@ -3,13 +3,14 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall" "syscall"
"git.milar.in/milarin/slices"
) )
var ( var (
@ -49,8 +50,8 @@ func main() {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Env = os.Environ() cmd.Env = os.Environ()
if envData, err := ioutil.ReadFile(os.ExpandEnv(*envFile)); err == nil { if envData, err := os.ReadFile(os.ExpandEnv(*envFile)); err == nil {
cmd.Env = append(cmd.Env, strings.Split(string(envData), "\n")...) cmd.Env = append(cmd.Env, slices.Filter(strings.Split(string(envData), "\n"), IsNotEmpty)...)
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
@ -71,3 +72,7 @@ func main() {
} }
} }
} }
func IsNotEmpty(str string) bool {
return len(strings.TrimSpace(str)) > 0
}