-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
79 lines (69 loc) · 2.13 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package rules
import (
"log"
"github.com/aerogear/charmil/validator"
"github.com/imdario/mergo"
"github.com/spf13/cobra"
)
// ValidatorConfig is provided to user for overriding default rules
type ValidatorConfig struct {
ValidatorOptions `json:"ValidatorOptions"`
ValidatorRules `json:"ValidatorRules"`
}
// ValidatorOptions provide additional configurations
// to the rules
type ValidatorOptions struct {
Verbose bool `json:"Verbose"`
IgnoreCommands map[string]bool `json:"IgnoreCommands"`
}
// ValidatorRules consists of all the rules
// present in validator
type ValidatorRules struct {
Length `json:"Length"`
MustExist `json:"MustExist"`
UseMatches `json:"UseMatches"`
}
// ExecuteRules executes all the rules
// provided by validatorConfig
func ExecuteRules(cmd *cobra.Command, validatorConfig *ValidatorConfig) []validator.ValidationError {
var ruleConfig RuleConfig
return ExecuteRulesInternal(cmd, &ruleConfig, validatorConfig)
}
// ValidatorConfigToRuleConfig intializes the default config
// and overrides default with user provided config
func ValidatorConfigToRuleConfig(validatorConfig *ValidatorConfig, ruleConfig *RuleConfig) {
defaultVerbose := validatorConfig.ValidatorOptions.Verbose
// unmarshal defaultConfigJson in configHelper
var configHelper ValidatorConfig = ValidatorConfig{
ValidatorOptions: ValidatorOptions{
Verbose: defaultVerbose,
},
ValidatorRules: ValidatorRules{
Length: Length{
Verbose: defaultVerbose,
Limits: map[string]Limit{
"Use": {Min: 2},
"Short": {Min: 15},
"Long": {Min: 50},
"Example": {Min: 50},
},
},
MustExist: MustExist{
Verbose: defaultVerbose,
Fields: map[string]bool{"Use": true, "Short": true, "Long": true, "Example": true},
},
},
}
// Merge user provided config into configHelper
if err := mergo.Merge(&configHelper, validatorConfig, mergo.WithSliceDeepCopy); err != nil {
log.Fatal(err)
}
validatorConfig = &configHelper
// append rules to execute
ruleConfig.Rules = append(
ruleConfig.Rules,
&validatorConfig.Length,
&validatorConfig.MustExist,
&validatorConfig.UseMatches,
)
}