Skip to content

Commit 93f45f3

Browse files
authored
Merge pull request #3 from snorwin/additional-env-vars
Load additional environment vars dynamically from file
2 parents 55d2826 + f19e93a commit 93f45f3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func main() {
3232
cmd := exec.Command(executable, os.Args[1:]...)
3333
cmd.Stdout = os.Stdout
3434
cmd.Stderr = os.Stderr
35+
cmd.Env = utils.LoadEnvFile()
3536
if err := cmd.AsyncRun(); err != nil {
3637
log.Emergency(err.Error())
3738
os.Exit(1)
@@ -72,6 +73,7 @@ func main() {
7273
tmp := exec.Command(executable, append([]string{"-x", utils.LookupHAProxySocketPath(), "-sf", strconv.Itoa(cmd.Process.Pid)}, os.Args[1:]...)...)
7374
tmp.Stdout = os.Stdout
7475
tmp.Stderr = os.Stderr
76+
tmp.Env = utils.LoadEnvFile()
7577
if err := tmp.AsyncRun(); err != nil {
7678
log.Warning(err.Error())
7779
log.Warning("reload failed")

pkg/utils/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7+
"strings"
78
)
89

910
// LookupExecutablePathAbs lookup the $PATH to find the absolut path of an executable
@@ -36,3 +37,16 @@ func LookupHAProxySocketPath() string {
3637

3738
return "/var/run/haproxy.sock"
3839
}
40+
41+
// LoadEnvFile load additional dynamic environment variables from a file which contains them in the form "key=value".
42+
func LoadEnvFile() []string {
43+
env := os.Environ()
44+
45+
if file, ok := os.LookupEnv("ENV_FILE"); ok {
46+
if data, err := os.ReadFile(file); err != nil {
47+
env = append(env, strings.Split(string(data), "/n")...)
48+
}
49+
}
50+
51+
return env
52+
}

0 commit comments

Comments
 (0)