-
Notifications
You must be signed in to change notification settings - Fork 13
/
executor.go
89 lines (74 loc) · 2.06 KB
/
executor.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
80
81
82
83
84
85
86
87
88
89
package rules
import (
"encoding/json"
"log"
"github.com/aerogear/charmil/validator"
"github.com/spf13/cobra"
)
// RuleConfig is the struct that stores
// configuration of rules
type RuleConfig struct {
Verbose bool
Length
MustExist
}
// ExecuteRules executes all the rules
// according to the RuleConfig provided
func (config *RuleConfig) ExecuteRules(cmd *cobra.Command) []validator.ValidationError {
var errors []validator.ValidationError
info := validator.StatusLog{TotalTested: 0, TotalErrors: 0, Errors: errors}
// initialize default rules
config.initDefaultRules()
return config.executeHelper(cmd, info)
}
func (config *RuleConfig) executeHelper(cmd *cobra.Command, info validator.StatusLog) []validator.ValidationError {
return validator.Traverse(
cmd,
config.Verbose,
info,
func(cmd *cobra.Command, verbose bool) []validator.ValidationError {
info.Errors = append(info.Errors, validateLength(cmd, config.Length, config.Verbose)...)
info.Errors = append(info.Errors, validateMustExist(cmd, config.MustExist, config.Verbose)...)
return info.Errors
},
)
}
// initDefaultRules initialize default rules
// and overrides the default rules if RuleConfig is provided by the user
func (config *RuleConfig) initDefaultRules() {
// default config for rules
var defaultConfig = &RuleConfig{
Verbose: false,
Length: Length{
Limits: map[string]Limit{
"Use": {Min: 2},
"Short": {Min: 15},
"Long": {Min: 100},
"Example": {Min: 100},
},
},
MustExist: MustExist{
Fields: []string{"Use", "Short", "Long", "Example"},
},
}
// Check verbose input from user
var verbose bool
if config != nil && config.Verbose {
verbose = true
}
// 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)
}
}
}