From 32bfd936e744ea5881ecbaca1276c16f21a1c7d3 Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Sun, 2 Jun 2024 12:12:12 +0200 Subject: [PATCH] add support for environment files At times, it is necessary or at least easier to set environment variables using a file. Add a function that augments the environment of the spawned process with the provided variables. Signed-off-by: Moritz Poldrack --- runner/config.go | 1 + runner/environment.go | 30 ++++++++++++++++++++++++++++++ runner/util_linux.go | 4 ++++ runner/util_unix.go | 4 ++++ runner/util_windows.go | 4 ++++ 5 files changed, 43 insertions(+) create mode 100644 runner/environment.go diff --git a/runner/config.go b/runner/config.go index 78a4eaa5..2d8bfeb4 100644 --- a/runner/config.go +++ b/runner/config.go @@ -58,6 +58,7 @@ type cfgBuild struct { KillDelay time.Duration `toml:"kill_delay"` Rerun bool `toml:"rerun"` RerunDelay int `toml:"rerun_delay"` + EnvironmentFile string `toml:"environment_file"` regexCompiled []*regexp.Regexp } diff --git a/runner/environment.go b/runner/environment.go new file mode 100644 index 00000000..55cb2438 --- /dev/null +++ b/runner/environment.go @@ -0,0 +1,30 @@ +package runner + +import ( + "bufio" + "fmt" + "os" + "os/exec" +) + +func (e *Engine) modifyEnvironment(c *exec.Cmd) error { + if e.config.Build.EnvironmentFile == "" { + return nil + } + + env := os.Environ() + + envFile, err := os.Open(e.config.Build.EnvironmentFile) + if err != nil { + return fmt.Errorf("failed to open environment file: %w") + } + defer envFile.Close() + + scan := bufio.NewScanner(envFile) + for scan.Scan() { + env = append(env, scan.Text()) + } + + c.Env = env + return nil +} diff --git a/runner/util_linux.go b/runner/util_linux.go index 658671b3..d11ff93b 100644 --- a/runner/util_linux.go +++ b/runner/util_linux.go @@ -30,6 +30,10 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) { func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) { c := exec.Command("/bin/sh", "-c", cmd) + err := e.modifyEnvironment(c) + if err != nil { + return nil, nil, nil, err + } f, err := pty.Start(c) return c, f, f, err } diff --git a/runner/util_unix.go b/runner/util_unix.go index afd47c89..d9f2cb2a 100644 --- a/runner/util_unix.go +++ b/runner/util_unix.go @@ -33,6 +33,10 @@ func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, c.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, } + err := e.modifyEnvironment(c) + if err != nil { + return nil, nil, nil, err + } stderr, err := c.StderrPipe() if err != nil { diff --git a/runner/util_windows.go b/runner/util_windows.go index eaa09e06..afe3fbf2 100644 --- a/runner/util_windows.go +++ b/runner/util_windows.go @@ -25,6 +25,10 @@ func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, if err != nil { return nil, nil, nil, err } + err = e.modifyEnvironment(c) + if err != nil { + return nil, nil, nil, err + } stdout, err := c.StdoutPipe() if err != nil { return nil, nil, nil, err