Skip to content

Commit

Permalink
configuration: env vars with array values rendered as a single string
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Jun 18, 2024
1 parent 55753bd commit 651c32e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 11 additions & 0 deletions internal/go-configmap/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ func (c *Map) SetFromCLIArgs(key string, args ...string) error {
return nil
}

// Some args might be coming from env vars that are specifying multiple values
// in a single string. We expand those cases by splitting every args with whitespace
// Example: args=["e1 e2", "e3"] -> args=["e1","e2","e3"]
argsExpantion := func(values []string) []string {
result := []string{}
for _, v := range values {
result = append(result, strings.Split(v, " ")...)
}
return result
}
args = argsExpantion(args)
// in case of schemaless configuration, we don't know the type of the setting
// we will save it as a string or array of strings
if len(c.schema) == 0 {
Expand Down
19 changes: 16 additions & 3 deletions internal/go-configmap/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/arduino/arduino-cli/internal/go-configmap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -79,10 +80,22 @@ func TestApplyEnvVars(t *testing.T) {
c.Set("foo", "bar")
c.Set("fooz.bar", "baz")
c.Set("answer", 42)
c.Set("array", []string{})
c.InjectEnvVars([]string{"APP_FOO=app-bar", "APP_FOOZ_BAR=app-baz"}, "APP")
require.Equal(t, "app-bar", c.Get("foo"))
require.Equal(t, "app-baz", c.Get("fooz.bar"))
require.Equal(t, 42, c.Get("answer"))
assert.Equal(t, "app-bar", c.Get("foo"))
assert.Equal(t, "app-baz", c.Get("fooz.bar"))
assert.Equal(t, 42, c.Get("answer"))

c.InjectEnvVars([]string{"APP_ARRAY=element1 element2 element3"}, "APP")
require.Equal(t, []string{"element1", "element2", "element3"}, c.GetStringSlice("array"))

// Test env containing array values with typed schema
{
m := configmap.New()
m.SetKeyTypeSchema("array", []string{})
m.InjectEnvVars([]string{"APP_ARRAY=e1 e2 e3"}, "APP")
require.Equal(t, []string{"e1", "e2", "e3"}, m.GetStringSlice("array"))
}
}

func TestMerge(t *testing.T) {
Expand Down

0 comments on commit 651c32e

Please sign in to comment.