Skip to content

fix(clibase): allow string-array properties to be unset by environment variable #6792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli/clibase/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
return nil
}

stringArrayType := StringArray{}.Type()

var merr *multierror.Error

// We parse environment variables first instead of using a nested loop to
Expand All @@ -126,7 +128,7 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
// way for a user to change a Default value to an empty string from
// the environment. Unfortunately, we have old configuration files
// that rely on the faulty behavior.
if !ok || envVal == "" {
if !ok || (envVal == "" && opt.Value.Type() != stringArrayType) {
continue
}

Expand Down
22 changes: 22 additions & 0 deletions cli/clibase/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,26 @@ func TestOptionSet_ParseEnv(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, "defname", workspaceName)
})

t.Run("EmptyStringArrayValue", func(t *testing.T) {
t.Parallel()

var workspaceNames clibase.StringArray

os := clibase.OptionSet{
clibase.Option{
Name: "Workspace Names",
Value: &workspaceNames,
Default: "defname",
Env: "WORKSPACE_NAMES",
},
}

err := os.SetDefaults()
require.NoError(t, err)

err = os.ParseEnv(clibase.ParseEnviron([]string{"CODER_WORKSPACE_NAMES="}, "CODER_"))
require.NoError(t, err)
require.EqualValues(t, []string{}, workspaceNames)
})
}
5 changes: 5 additions & 0 deletions cli/clibase/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func writeAsCSV(vals []string) string {
}

func (s *StringArray) Set(v string) error {
if v == "" {
*s = []string{}
return nil
}

ss, err := readAsCSV(v)
if err != nil {
return err
Expand Down