Skip to content
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
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string)
defaultBoolValue = b
}
flagSet.Bool(prefix+tag, defaultBoolValue, description)
case reflect.Slice:
if fieldValue.Type().Elem().Kind() != reflect.String {
return fmt.Errorf("unsupported slice element type %s for field %s", fieldValue.Type().Elem().Kind(), field.Name)
}
flagSet.StringSlice(prefix+tag, []string{}, description)
default:
return fmt.Errorf("unsupported field type %s for field %s", fieldValue.Kind(), field.Name)
}
Expand Down
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestBindConfigToFlags(t *testing.T) {
CustomFlagStruct2 struct {
CustomFlagDuration time.Duration `mapstructure:"custom-flag-duration-2"`
} `mapstructure:"le-strFlag"`
Slice []string `mapstructure:"slice" description:"This is a slice of strings"`
}

testStruct := test{}
Expand Down Expand Up @@ -171,7 +172,7 @@ func TestNewDefaultConfig(t *testing.T) {

func TestGenerateFlagSetUnsupportedType(t *testing.T) {
type test struct {
UnsupportedField []string `mapstructure:"unsupported-field"`
UnsupportedField []int `mapstructure:"unsupported-field"`
}
testStruct := test{}
err := config.BindConfigToFlags(viper.New(), &cobra.Command{}, &testStruct)
Expand Down
Loading