File tree Expand file tree Collapse file tree 3 files changed +51
-4
lines changed
Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,8 @@ Default option functions:
3838 - ` cmd.WithTimeout(time.Duration) `
3939 - ` cmd.WithoutTimeout `
4040 - ` cmd.WithWorkingDir(string) `
41- - ` cmd.WithEnvironment `
41+ - ` cmd.WithEnvironmentVariables(cmd.EnvVars) `
42+ - ` cmd.WithInheritedEnvironment(cmd.EnvVars) `
4243
4344#### Example
4445
Original file line number Diff line number Diff line change @@ -27,6 +27,15 @@ type Command struct {
2727 combined bytes.Buffer
2828}
2929
30+ // EnvVars represents a map where the key is the name of the env variable
31+ // and the value is the value of the variable
32+ //
33+ // Example:
34+ //
35+ // env := map[string]string{"ENV": "VALUE"}
36+ //
37+ type EnvVars map [string ]string
38+
3039// NewCommand creates a new command
3140// You can add option with variadic option argument
3241// Default timeout is set to 30 minutes
@@ -116,9 +125,25 @@ func WithWorkingDir(dir string) func(c *Command) {
116125 }
117126}
118127
119- // WithEnvironment adds all environments from the current process to the command
120- func WithEnvironment (c * Command ) {
121- c .Env = os .Environ ()
128+ // WithInheritedEnvironment uses the env from the current process and
129+ // allow to add more variables.
130+ func WithInheritedEnvironment (env EnvVars ) func (c * Command ) {
131+ return func (c * Command ) {
132+ c .Env = os .Environ ()
133+
134+ // Set custom variables
135+ fn := WithEnvironmentVariables (env )
136+ fn (c )
137+ }
138+ }
139+
140+ // WithEnvironmentVariables sets environment variables for the executed command
141+ func WithEnvironmentVariables (env EnvVars ) func (c * Command ) {
142+ return func (c * Command ) {
143+ for key , value := range env {
144+ c .AddEnv (key , value )
145+ }
146+ }
122147}
123148
124149// AddEnv adds an environment variable to the command
Original file line number Diff line number Diff line change @@ -159,3 +159,24 @@ func TestWithStandardStreams(t *testing.T) {
159159 assertEqualWithLineBreak (t , "stderr\n stdout" , out )
160160 assert .Nil (t , err )
161161}
162+
163+ func TestWithEnvironmentVariables (t * testing.T ) {
164+ c := NewCommand ("echo $env" , WithEnvironmentVariables (map [string ]string {"env" : "value" }))
165+ c .Execute ()
166+
167+ assert .Equal (t , "value" , c .Stdout ())
168+ }
169+
170+ func TestWithInheritedEnvironment (t * testing.T ) {
171+ os .Setenv ("FROM_OS" , "is on os" )
172+ os .Setenv ("OVERWRITE" , "is on os but should be overwritten" )
173+ defer func () {
174+ os .Unsetenv ("FROM_OS" )
175+ os .Unsetenv ("OVERWRITE" )
176+ }()
177+
178+ c := NewCommand ("echo $FROM_OS $OVERWRITE" , WithInheritedEnvironment (map [string ]string {"OVERWRITE" : "overwritten" }))
179+ c .Execute ()
180+
181+ assertEqualWithLineBreak (t , "is on os overwritten" , c .Stdout ())
182+ }
You can’t perform that action at this time.
0 commit comments