-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
84 lines (71 loc) · 1.76 KB
/
Copy pathconfig.go
File metadata and controls
84 lines (71 loc) · 1.76 KB
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
package logmsglint
import (
"fmt"
"regexp"
)
type Config struct {
EnableFixes bool
AllowedPunct string
ExtraKeywords []string
SensitivePatterns []*regexp.Regexp
}
type rawConfig struct {
EnableFixes bool `yaml:"enable_fixes"`
AllowedPunct string `yaml:"allowed_punct"`
SensitiveKeywords []string `yaml:"sensitive_keywords"`
SensitiveRegexps []string `yaml:"sensitive_regexps"`
}
func defaultConfig() Config {
return Config{
EnableFixes: true,
AllowedPunct: "",
}
}
func loadConfig(settings any) (Config, error) {
cfg := defaultConfig()
if settings == nil {
return cfg, nil
}
m, ok := settings.(map[string]any)
if !ok {
return cfg, fmt.Errorf("logmsglint: unexpected settings type %T", settings)
}
var raw rawConfig
if v, ok := m["enable_fixes"].(bool); ok {
raw.EnableFixes = v
}
if v, ok := m["allowed_punct"].(string); ok {
raw.AllowedPunct = v
}
if v, ok := m["sensitive_keywords"].([]any); ok {
for _, x := range v {
if s, ok := x.(string); ok {
raw.SensitiveKeywords = append(raw.SensitiveKeywords, s)
}
}
}
if v, ok := m["sensitive_regexps"].([]any); ok {
for _, x := range v {
if s, ok := x.(string); ok {
raw.SensitiveRegexps = append(raw.SensitiveRegexps, s)
}
}
}
cfg.EnableFixes = raw.EnableFixes
cfg.AllowedPunct = raw.AllowedPunct
cfg.ExtraKeywords = raw.SensitiveKeywords
for _, pattern := range raw.SensitiveRegexps {
re, err := regexp.Compile(pattern)
if err != nil {
return cfg, fmt.Errorf("logmsglint: invalid regexp %q: %w", pattern, err)
}
cfg.SensitivePatterns = append(cfg.SensitivePatterns, re)
}
return cfg, nil
}
func LoadConfig(settings any) (Config, error) {
return loadConfig(settings)
}
func SetConfig(cfg Config) {
currentConfig = cfg
}