Skip to content

Commit

Permalink
fix: rule config overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithans committed Jul 1, 2021
1 parent ca6b5ce commit 4e5ba58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/BurntSushi/toml v0.3.1
github.com/imdario/mergo v0.3.12
github.com/nicksnyder/go-i18n/v2 v2.1.2
github.com/spf13/cobra v1.1.3
golang.org/x/text v0.3.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -89,6 +89,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down
39 changes: 18 additions & 21 deletions validator/rules/executor.go
@@ -1,15 +1,17 @@
package rules

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/aerogear/charmil/validator"
"github.com/imdario/mergo"
"github.com/spf13/cobra"
)

// Rules is an interface that is implemented
// by every rule defined in rules package
type Rules interface {
Validate() []validator.ValidationError
}
Expand Down Expand Up @@ -84,6 +86,7 @@ func (config *RuleConfig) validate(cmd *cobra.Command, info *validator.StatusLog
&MustExistHelper{cmd: cmd, config: config},
}

// traverse all rules and validate
for _, rule := range rules {
validationErrors := rule.Validate()
info.TotalErrors += len(validationErrors)
Expand Down Expand Up @@ -113,28 +116,22 @@ func (config *RuleConfig) initDefaultRules() {
},
}

// Check verbose input from user
var verbose bool
if config != nil && config.Verbose {
verbose = true
if err := mergo.Merge(&config.Length, defaultConfig.Length); err != nil {
log.Fatal(err)
}
config.Fields = append(config.Fields, defaultConfig.Fields...)
config.Fields = removeDuplicates(config.Fields)
}

// Set Config to defaultConfig
*config = *defaultConfig
config.Verbose = verbose

// Merge the defaultConfig and Config given by user
if config.Length.Limits != nil && config.MustExist.Fields != nil {
out, err := json.Marshal(config)
if err != nil {
log.Fatal(err)
}
data := []byte(out)

errr := json.Unmarshal(data, &defaultConfig)
if errr != nil {
log.Fatal(errr)
// remove duplicates from slice
func removeDuplicates(input []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range input {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}

return list
}

0 comments on commit 4e5ba58

Please sign in to comment.