Skip to content

Commit 8990b6b

Browse files
fix: simplify env vars parsing (#1988)
Fixes: #1986
1 parent 80265a8 commit 8990b6b

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

internal/execenv/execenv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func Generate(plugin plugins.Plugin, callbackEnv map[string]string) (env map[str
2727
// executing the callback isn't enough. We actually need to source it (.) so
2828
// the environment variables get set, and then run `env` so they get printed
2929
// to STDOUT.
30-
expression := execute.NewExpression(fmt.Sprintf(". \"%s\"; env", execEnvPath), []string{})
30+
expression := execute.NewExpression(fmt.Sprintf(". \"%s\"; env -0", execEnvPath), []string{})
3131
expression.Env = callbackEnv
3232
expression.Stdout = &stdout
3333
err = expression.Run()
3434

3535
str := stdout.String()
36-
return execute.SliceToMap(strings.Split(str, "\n")), err
36+
return execute.SliceToMap(strings.Split(str, "\x00")), err
3737
}

internal/execenv/execenv_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,17 @@ func TestGenerate(t *testing.T) {
6565
assert.Equal(t, "bar", env["BAZ"])
6666
assert.Equal(t, "abc\n123", env["EQUALSTEST"])
6767
})
68+
69+
t.Run("preserves environment variables that contain equals sign and line breaks in value", func(t *testing.T) {
70+
value := "-----BEGIN CERTIFICATE-----\nMANY\\LINES\\THE\nLAST\\ONE\\ENDS\\IN\nAN=\n-----END CERTIFICATE-----"
71+
testDataDir := t.TempDir()
72+
conf := config.Config{DataDir: testDataDir}
73+
_, err := repotest.InstallPlugin("dummy_plugin", testDataDir, testPluginName)
74+
assert.Nil(t, err)
75+
plugin := plugins.New(conf, testPluginName)
76+
assert.Nil(t, repotest.WritePluginCallback(plugin.Dir, "exec-env", "#!/usr/bin/env bash\nexport BAZ=\""+value+"\""))
77+
env, err := Generate(plugin, map[string]string{})
78+
assert.Nil(t, err)
79+
assert.Equal(t, value, env["BAZ"])
80+
})
6881
}

internal/execute/execute.go

-8
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,11 @@ func MapToSlice(env map[string]string) (slice []string) {
9595
func SliceToMap(env []string) map[string]string {
9696
envMap := map[string]string{}
9797

98-
var previousKey string
99-
10098
for _, envVar := range env {
10199
varValue := strings.SplitN(envVar, "=", 2)
102100

103101
if len(varValue) == 2 {
104-
// new var=value line
105-
previousKey = varValue[0]
106102
envMap[varValue[0]] = varValue[1]
107-
} else {
108-
// value from variable defined on a previous line, append
109-
val := envMap[previousKey]
110-
envMap[previousKey] = val + "\n" + varValue[0]
111103
}
112104
}
113105

internal/execute/execute_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ func TestSliceToMap(t *testing.T) {
269269
input: []string{"MYVAR=value\nwith\nnewlines"},
270270
output: map[string]string{"MYVAR": "value\nwith\nnewlines"},
271271
},
272-
{
273-
input: []string{"MYVAR=value", "with", "newlines"},
274-
output: map[string]string{"MYVAR": "value\nwith\nnewlines"},
275-
},
276272
}
277273

278274
for _, tt := range tests {

0 commit comments

Comments
 (0)