Skip to content

Commit eec2615

Browse files
authored
feat(config): add support for string slice flags in traverseStruct (#80)
* feat(config): add support for string slice flags in traverseStruct * chore: fix test
1 parent 5272cdc commit eec2615

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string)
166166
defaultBoolValue = b
167167
}
168168
flagSet.Bool(prefix+tag, defaultBoolValue, description)
169+
case reflect.Slice:
170+
if fieldValue.Type().Elem().Kind() != reflect.String {
171+
return fmt.Errorf("unsupported slice element type %s for field %s", fieldValue.Type().Elem().Kind(), field.Name)
172+
}
173+
flagSet.StringSlice(prefix+tag, []string{}, description)
169174
default:
170175
return fmt.Errorf("unsupported field type %s for field %s", fieldValue.Kind(), field.Name)
171176
}

config/config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestBindConfigToFlags(t *testing.T) {
3636
CustomFlagStruct2 struct {
3737
CustomFlagDuration time.Duration `mapstructure:"custom-flag-duration-2"`
3838
} `mapstructure:"le-strFlag"`
39+
Slice []string `mapstructure:"slice" description:"This is a slice of strings"`
3940
}
4041

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

172173
func TestGenerateFlagSetUnsupportedType(t *testing.T) {
173174
type test struct {
174-
UnsupportedField []string `mapstructure:"unsupported-field"`
175+
UnsupportedField []int `mapstructure:"unsupported-field"`
175176
}
176177
testStruct := test{}
177178
err := config.BindConfigToFlags(viper.New(), &cobra.Command{}, &testStruct)

0 commit comments

Comments
 (0)