diff --git a/lib/flags.go b/lib/flags.go index b9a3fbea..1b14cd8a 100644 --- a/lib/flags.go +++ b/lib/flags.go @@ -66,13 +66,10 @@ func bindEnvVarIntoViper(v *viper.Viper, fullFlagName, envPrefix string) { func applyViperFlagToCommand(flag *pflag.Flag, val interface{}, cmd *cobra.Command) { switch t := val.(type) { case []interface{}: - var paramSlice []string for _, param := range t { - paramSlice = append(paramSlice, param.(string)) - } - valStr := strings.Join(paramSlice, ",") - if err := flag.Value.Set(valStr); err != nil { - log.Err(err).Msg("Failed to set Viper flags") + if err := flag.Value.Set(param.(string)); err != nil { + log.Err(err).Msg("Failed to set Viper flags") + } } default: newVal := fmt.Sprintf("%v", val) diff --git a/lib/flags_test.go b/lib/flags_test.go index b8b82f44..0d9631c3 100644 --- a/lib/flags_test.go +++ b/lib/flags_test.go @@ -331,6 +331,34 @@ test-float: 123.456 assert.Equal(t, 123.456, testFloat) }) + t.Run("BindFlags_FromYAML_StringArrayVar", func(t *testing.T) { + assertClearEnv(t) + defer clearEnvVars(t) + + yamlConfig := []byte(` +regex: + - test\= + - array\= + - flag\= +another-regex: [test\=, array\=, flag\=] +`) + + cmd := &cobra.Command{} + v := getViper() + v.SetConfigType("yaml") + assert.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlConfig))) + + var testArray []string + cmd.Flags().StringArrayVar(&testArray, "regex", []string{}, "Test array flag") + cmd.Flags().StringArrayVar(&testArray, "another-regex", []string{}, "Test array flag") + + err := lib.BindFlags(cmd, v, envVarPrefix) + assert.NoError(t, err) + + assert.Equal(t, []string{"test\\=", "array\\=", "flag\\="}, testArray) + assert.Equal(t, []string{"test\\=", "array\\=", "flag\\="}, testArray) + }) + t.Run("BindFlags_FromYAML_SubCMD", func(t *testing.T) { assertClearEnv(t) defer clearEnvVars(t)