Skip to content

Commit

Permalink
fix: load StringArray from config file (#178)
Browse files Browse the repository at this point in the history
Co-authored-by: Jossef Harush Kadouri <[email protected]>
  • Loading branch information
Baruch Odem (Rothkoff) and jossef authored Sep 10, 2023
1 parent 135b2d3 commit e57c528
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ jobs:
We've built `2ms` command line interface to be as self-descriptive as possible. This is the help message that you will see if you executed `2ms` without args:

<!-- command-line:start -->

```
2ms Secrets Detection: A tool to detect secrets in public websites and communication services.

Expand Down Expand Up @@ -130,6 +129,7 @@ Flags:

Use "2ms [command] --help" for more information about a command.
```
<!-- command-line:end -->
## Custom Regex Rules
Expand All @@ -148,10 +148,6 @@ username=admin
[![asciicast](https://asciinema.org/a/607198.svg)](https://asciinema.org/a/607198)
<!-- command-line:end -->
## Plugins
We offer the following list of integrations in the form of plugins
Expand Down
9 changes: 3 additions & 6 deletions lib/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions lib/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e57c528

Please sign in to comment.