generated from jimschubert/go-cli-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
98 lines (82 loc) · 2.86 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package docked
import (
"errors"
"os"
"sort"
"github.com/jimschubert/docked/model"
"github.com/jimschubert/docked/model/validations"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// ConfigRuleOverride defines the id-priority override mapping used in a config file
type ConfigRuleOverride struct {
// The rule id to override
ID string `yaml:"id"`
// The overridden priority
Priority *model.Priority `yaml:"priority,omitempty"`
}
// RuleOverrides is a slice of ConfigRuleOverride. This type allows for simpler definitions and YAML parsing.
type RuleOverrides []ConfigRuleOverride
// UnmarshalYAML implements the interface necessary to have greater control over deserializing RuleOverrides
func (r *RuleOverrides) UnmarshalYAML(value *yaml.Node) error {
*r = make([]ConfigRuleOverride, 0)
var kvp map[string]model.Priority
if err := value.Decode(&kvp); err == nil {
for key, priority := range kvp {
*r = append(*r, ConfigRuleOverride{key, priority.Ptr()})
}
return nil
}
type raw RuleOverrides
return value.Decode((*raw)(r))
}
// Config represents the YAML config structure exposed to users
type Config struct {
// Ignore this collection of rule ids
Ignore []string `yaml:"ignore"`
// RuleOverrides allows users to override the ConfigRuleOverride.Priority of a specific rule by ConfigRuleOverride.ID
RuleOverrides *RuleOverrides `yaml:"rule_overrides,omitempty"`
CustomRules []validations.SimpleRegexRule `yaml:"custom_rules,omitempty"`
SkipDefaultRules bool `yaml:"skip_default_rules,omitempty"`
// IncludeRules allows setting an approved list of rules to include when SkipDefaultRules is true
IncludeRules []string `yaml:"include_rules,omitempty"`
}
// Load a Config from path with sorted members (by ID for rule overrides, by Name for custom rules)
func (c *Config) Load(path string) error {
b, err := os.ReadFile(path)
if os.IsNotExist(err) {
log.Warnf("Config file does not exist! Attempted: %s", path)
return nil
}
if err != nil {
return err
}
if err = yaml.Unmarshal(b, c); err != nil {
return err
}
if len(c.Ignore) > 0 {
if c.SkipDefaultRules {
return errors.New("defining both skip_default_rules and ignores at the same time in config is unsupported")
}
// Sorting each slice in config. Necessary because yaml v3 doesn't always return top-down parsing order.
sort.Strings(c.Ignore)
}
if len(c.IncludeRules) > 0 {
if !c.SkipDefaultRules {
return errors.New("must set skip_default_rules to true when defining include_rules")
}
}
if c.RuleOverrides != nil {
overrides := *c.RuleOverrides
sort.Slice(overrides, func(i, j int) bool {
return overrides[i].ID < overrides[j].ID
})
c.RuleOverrides = &overrides
}
if len(c.CustomRules) > 0 {
sort.Slice(c.CustomRules, func(i, j int) bool {
return c.CustomRules[i].Name < c.CustomRules[j].Name
})
}
return nil
}