diff --git a/subprocess.go b/subprocess.go index 0d13c75..da6809b 100644 --- a/subprocess.go +++ b/subprocess.go @@ -18,6 +18,7 @@ type Subprocess struct { process *exec.Cmd // The underlying *exec.Cmd that represents the subprocess. args []string // The sanitized command arguments. + env []string // Environment variables for the subprocess. hideStderr bool // Hide stderr output. hideStdout bool // Hide stdout output. shell bool // Executes the command directly in the shell without sanitization. @@ -55,6 +56,16 @@ var ( s.context = path } } + EnvVars = func(envVars ...string) Option { + return func(s *Subprocess) { + s.env = append(s.env, envVars...) + } + } + EnvVar = func(envVar string) Option { + return func(s *Subprocess) { + s.env = append(s.env, envVar) + } + } // Silent hides all output from the subprocess. Silent Option = func(s *Subprocess) { s.hideStdout = true @@ -138,6 +149,10 @@ func (s *Subprocess) Exec() error { return err } + if s.env != nil { + cmd.Env = append(os.Environ(), s.env...) + } + stdout, err := cmd.StdoutPipe() if err != nil { return err