Skip to content

Commit ff137cd

Browse files
guillaumerosepraveenkumar
authored andcommitted
Fix running a command with a new env variable
Previously, if the variable was not already existing, the variable was dropped.
1 parent 80c35f2 commit ff137cd

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

pkg/os/exec.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func runCmd(command string, args []string, env map[string]string) (string, strin
1414
if len(env) != 0 {
1515
cmd.Env = os.Environ()
1616
for key, value := range env {
17-
cmd.Env = ReplaceEnv(cmd.Env, key, value)
17+
cmd.Env = ReplaceOrAddEnv(cmd.Env, key, value)
1818
}
1919
}
2020
stdOut := new(bytes.Buffer)
@@ -52,9 +52,25 @@ func RunWithPrivilege(reason string, cmdAndArgs ...string) (string, string, erro
5252
}
5353

5454
func RunWithDefaultLocale(command string, args ...string) (string, string, error) {
55-
return run(command, args, map[string]string{"LC_ALL": "C", "LANG": "C"})
55+
return RunWithDefaultLocaleAndEnv(command, args, map[string]string{})
5656
}
5757

5858
func RunWithDefaultLocalePrivate(command string, args ...string) (string, string, error) {
59-
return runPrivate(command, args, map[string]string{"LC_ALL": "C", "LANG": "C"})
59+
return RunWithDefaultLocalePrivateAndEnv(command, args, map[string]string{})
60+
}
61+
62+
func RunWithDefaultLocaleAndEnv(command string, args []string, extraEnv map[string]string) (string, string, error) {
63+
env := map[string]string{"LC_ALL": "C", "LANG": "C"}
64+
for k, v := range extraEnv {
65+
env[k] = v
66+
}
67+
return run(command, args, env)
68+
}
69+
70+
func RunWithDefaultLocalePrivateAndEnv(command string, args []string, extraEnv map[string]string) (string, string, error) {
71+
env := map[string]string{"LC_ALL": "C", "LANG": "C"}
72+
for k, v := range extraEnv {
73+
env[k] = v
74+
}
75+
return runPrivate(command, args, env)
6076
}

pkg/os/util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,25 @@ func CurrentOS() OS {
3737
panic("Unexpected OS type")
3838
}
3939

40-
// ReplaceEnv changes the value of an environment variable
40+
// ReplaceOrAddEnv changes the value of an environment variable if it exists otherwise add the new variable
4141
// It drops the existing value and appends the new value in-place
42-
func ReplaceEnv(variables []string, varName string, value string) []string {
42+
func ReplaceOrAddEnv(variables []string, varName string, value string) []string {
4343
var result []string
44+
45+
found := false
4446
for _, e := range variables {
4547
pair := strings.Split(e, "=")
4648
if pair[0] != varName {
4749
result = append(result, e)
4850
} else {
51+
found = true
4952
result = append(result, fmt.Sprintf("%s=%s", varName, value))
5053
}
5154
}
5255

56+
if !found {
57+
result = append(result, fmt.Sprintf("%s=%s", varName, value))
58+
}
5359
return result
5460
}
5561

pkg/os/util_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package os
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReplaceEnv(t *testing.T) {
10+
env := []string{"HOME=/home/user/", "PATH=/bin:/sbin:/usr/bin", "LC_ALL=de_DE.UTF8"}
11+
replaced := ReplaceOrAddEnv(env, "LC_ALL", "C")
12+
13+
assert.Len(t, replaced, 3)
14+
assert.Equal(t, env[2], "LC_ALL=de_DE.UTF8")
15+
assert.Equal(t, replaced[2], "LC_ALL=C")
16+
}
17+
18+
func TestAddEnv(t *testing.T) {
19+
env := []string{"HOME=/home/user/", "PATH=/bin:/sbin:/usr/bin", "LC_ALL=de_DE.UTF8"}
20+
replaced := ReplaceOrAddEnv(env, "KUBECONFIG", "some-data")
21+
22+
assert.Len(t, replaced, 4)
23+
assert.Equal(t, replaced[3], "KUBECONFIG=some-data")
24+
}

0 commit comments

Comments
 (0)