diff --git a/config/config.go b/config/config.go index 5260f4b..6d327a5 100644 --- a/config/config.go +++ b/config/config.go @@ -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) } diff --git a/config/config_test.go b/config/config_test.go index 86812c3..5967097 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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{} @@ -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)