Skip to content

Commit 6afabde

Browse files
author
Takashi Kusumi
authored
Support an array value in the config file (#303)
1 parent dd6b763 commit 6afabde

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

cmd/cmd.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,20 @@ func (o *options) overrideFlagSetDefaultFromConfig(fs *pflag.FlagSet) error {
377377
continue
378378
}
379379

380+
if valueSlice, ok := value.([]any); ok {
381+
// the value is an array
382+
if flagSlice, ok := flag.Value.(pflag.SliceValue); ok {
383+
values := make([]string, len(valueSlice))
384+
for i, v := range valueSlice {
385+
values[i] = fmt.Sprint(v)
386+
}
387+
if err := flagSlice.Replace(values); err != nil {
388+
return fmt.Errorf("invalid value %q for %q in the config file: %v", value, name, err)
389+
}
390+
continue
391+
}
392+
}
393+
380394
if err := flag.Value.Set(fmt.Sprint(value)); err != nil {
381395
return fmt.Errorf("invalid value %q for %q in the config file: %v", value, name, err)
382396
}

cmd/cmd_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,46 @@ func TestOptionsOverrideFlagSetDefaultFromConfig(t *testing.T) {
920920
})
921921
}
922922
}
923+
924+
func TestOptionsOverrideFlagSetDefaultFromConfigArray(t *testing.T) {
925+
tests := []struct {
926+
config string
927+
want []string
928+
}{
929+
{
930+
config: "testdata/config-string.yaml",
931+
want: []string{"hello-world"},
932+
},
933+
{
934+
config: "testdata/config-array0.yaml",
935+
want: []string{},
936+
},
937+
{
938+
config: "testdata/config-array1.yaml",
939+
want: []string{"abcd"},
940+
},
941+
{
942+
config: "testdata/config-array2.yaml",
943+
want: []string{"abcd", "efgh"},
944+
},
945+
}
946+
947+
for _, tt := range tests {
948+
tt := tt
949+
t.Run(tt.config, func(t *testing.T) {
950+
o := NewOptions(genericclioptions.NewTestIOStreamsDiscard())
951+
fs := pflag.NewFlagSet("", pflag.ExitOnError)
952+
o.AddFlags(fs)
953+
if err := fs.Parse([]string{"--config=" + tt.config}); err != nil {
954+
t.Fatal(err)
955+
}
956+
if err := o.overrideFlagSetDefaultFromConfig(fs); err != nil {
957+
t.Fatal(err)
958+
}
959+
if !reflect.DeepEqual(tt.want, o.exclude) {
960+
t.Errorf("expected %v, but got %v", tt.want, o.exclude)
961+
}
962+
})
963+
}
964+
965+
}

cmd/testdata/config-array0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude: []

cmd/testdata/config-array1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude: ["abcd"]

cmd/testdata/config-array2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude: ["abcd", "efgh"]

cmd/testdata/config-string.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude: "hello-world"

0 commit comments

Comments
 (0)