Skip to content

Commit 651c32e

Browse files
configuration: env vars with array values rendered as a single string
1 parent 55753bd commit 651c32e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Diff for: internal/go-configmap/cli.go

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ func (c *Map) SetFromCLIArgs(key string, args ...string) error {
2727
return nil
2828
}
2929

30+
// Some args might be coming from env vars that are specifying multiple values
31+
// in a single string. We expand those cases by splitting every args with whitespace
32+
// Example: args=["e1 e2", "e3"] -> args=["e1","e2","e3"]
33+
argsExpantion := func(values []string) []string {
34+
result := []string{}
35+
for _, v := range values {
36+
result = append(result, strings.Split(v, " ")...)
37+
}
38+
return result
39+
}
40+
args = argsExpantion(args)
3041
// in case of schemaless configuration, we don't know the type of the setting
3142
// we will save it as a string or array of strings
3243
if len(c.schema) == 0 {

Diff for: internal/go-configmap/configuration_test.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/arduino/arduino-cli/internal/go-configmap"
23+
"github.com/stretchr/testify/assert"
2324
"github.com/stretchr/testify/require"
2425
"gopkg.in/yaml.v3"
2526
)
@@ -79,10 +80,22 @@ func TestApplyEnvVars(t *testing.T) {
7980
c.Set("foo", "bar")
8081
c.Set("fooz.bar", "baz")
8182
c.Set("answer", 42)
83+
c.Set("array", []string{})
8284
c.InjectEnvVars([]string{"APP_FOO=app-bar", "APP_FOOZ_BAR=app-baz"}, "APP")
83-
require.Equal(t, "app-bar", c.Get("foo"))
84-
require.Equal(t, "app-baz", c.Get("fooz.bar"))
85-
require.Equal(t, 42, c.Get("answer"))
85+
assert.Equal(t, "app-bar", c.Get("foo"))
86+
assert.Equal(t, "app-baz", c.Get("fooz.bar"))
87+
assert.Equal(t, 42, c.Get("answer"))
88+
89+
c.InjectEnvVars([]string{"APP_ARRAY=element1 element2 element3"}, "APP")
90+
require.Equal(t, []string{"element1", "element2", "element3"}, c.GetStringSlice("array"))
91+
92+
// Test env containing array values with typed schema
93+
{
94+
m := configmap.New()
95+
m.SetKeyTypeSchema("array", []string{})
96+
m.InjectEnvVars([]string{"APP_ARRAY=e1 e2 e3"}, "APP")
97+
require.Equal(t, []string{"e1", "e2", "e3"}, m.GetStringSlice("array"))
98+
}
8699
}
87100

88101
func TestMerge(t *testing.T) {

0 commit comments

Comments
 (0)