Skip to content

fix(clibase): allow empty values to unset defaults #6832

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

Merged
merged 1 commit into from
Mar 28, 2023
Merged
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
10 changes: 9 additions & 1 deletion cli/clibase/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,19 @@ func (i *Invocation) run(state *runState) error {
return
}

// If flag was changed, we need to remove the default values.
sv, ok := f.Value.(pflag.SliceValue)
if !ok {
panic("defaulted array option is not a slice value")
}
err := sv.Replace(sv.GetSlice()[i:])
ss := sv.GetSlice()
if len(ss) == 0 {
// Slice likely zeroed by a flag.
// E.g. "--fruit" may default to "apples,oranges" but the user
// provided "--fruit=""".
return
}
err := sv.Replace(ss[i:])
if err != nil {
panic(err)
}
Expand Down
32 changes: 32 additions & 0 deletions cli/clibase/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,35 @@ func TestCommand_SliceFlags(t *testing.T) {
err = cmd("bad", "bad", "bad").Invoke().Run()
require.NoError(t, err)
}

func TestCommand_EmptySlice(t *testing.T) {
t.Parallel()

cmd := func(want ...string) *clibase.Cmd {
var got []string
return &clibase.Cmd{
Use: "root",
Options: clibase.OptionSet{
{
Name: "arr",
Flag: "arr",
Default: "bad,bad,bad",
Env: "ARR",
Value: clibase.StringArrayOf(&got),
},
},
Handler: (func(i *clibase.Invocation) error {
require.Equal(t, want, got)
return nil
}),
}
}

// Base-case
err := cmd("bad", "bad", "bad").Invoke().Run()
require.NoError(t, err)

inv := cmd().Invoke("--arr", "")
err = inv.Run()
require.NoError(t, err)
}
3 changes: 3 additions & 0 deletions cli/clibase/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ 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.
//
// TODO: We should remove this hack in May 2023, when deployments
// have had months to migrate to the new behavior.
if !ok || envVal == "" {
continue
}
Expand Down
4 changes: 4 additions & 0 deletions cli/clibase/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func writeAsCSV(vals []string) string {
}

func (s *StringArray) Set(v string) error {
if v == "" {
*s = nil
return nil
}
ss, err := readAsCSV(v)
if err != nil {
return err
Expand Down