-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
295 lines (273 loc) · 8.05 KB
/
config.go
File metadata and controls
295 lines (273 loc) · 8.05 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package repogov
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
// configNames lists supported config filenames in search order.
// JSON is preferred over YAML.
var configNames = []string{
"repogov-config.json",
"repogov-config.yaml",
"repogov-config.yml",
}
// LoadConfig reads a configuration file from path and returns a [Config].
// JSON and YAML formats are supported, detected by file extension
// (.yaml/.yml for YAML, everything else as JSON). Missing fields are
// filled from [DefaultConfig]. If the file does not exist, [DefaultConfig]
// is returned with a nil error, making configuration optional for callers.
func LoadConfig(path string) (Config, error) {
def := DefaultConfig()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return def, nil
}
return Config{}, err
}
var cfg Config
if isYAML(path) {
if err := unmarshalYAML(data, &cfg); err != nil {
return Config{}, err
}
} else {
if err := json.Unmarshal(data, &cfg); err != nil {
return Config{}, err
}
}
// Merge missing fields from defaults.
if cfg.Default == 0 {
cfg.Default = def.Default
}
if cfg.WarningThreshold == 0 {
cfg.WarningThreshold = def.WarningThreshold
}
if cfg.SkipDirs == nil {
cfg.SkipDirs = def.SkipDirs
}
if cfg.IncludeExts == nil {
cfg.IncludeExts = def.IncludeExts
}
if cfg.Rules == nil {
cfg.Rules = def.Rules
}
// Merge default file entries; user entries take precedence.
if cfg.Files == nil {
cfg.Files = def.Files
} else {
for k, v := range def.Files {
if _, ok := cfg.Files[k]; !ok {
cfg.Files[k] = v
}
}
}
return cfg, nil
}
// FindConfig searches for a repogov configuration file relative to root.
// It checks the repo root first, then .github/, returning the first file
// found. Within each location it tries filenames in order:
// repogov-config.json, repogov.json, repogov-config.yaml,
// repogov-config.yml, repogov.yaml, repogov.yml.
// Returns an empty string if no config file exists.
//
// Precedence: repo root over .github/, JSON over YAML,
// repogov-config over repogov.
func FindConfig(root string) string {
dirs := []string{
root,
filepath.Join(root, ".github"),
}
for _, dir := range dirs {
for _, name := range configNames {
p := filepath.Join(dir, name)
if _, err := os.Stat(p); err == nil {
return p
}
}
}
return ""
}
// FindAllConfigs returns every config file present under root, ordered from
// highest to lowest precedence (matching the search order of [FindConfig]).
// The first entry, if any, is the active config. Subsequent entries are
// present but overridden.
func FindAllConfigs(root string) []string {
dirs := []string{
root,
filepath.Join(root, ".github"),
}
var found []string
for _, dir := range dirs {
for _, name := range configNames {
p := filepath.Join(dir, name)
if _, err := os.Stat(p); err == nil {
found = append(found, p)
}
}
}
return found
}
// SaveConfig writes cfg to the given path. The format is determined by
// file extension: .yaml/.yml produces YAML, everything else produces
// indented JSON. The file is created with mode 0644.
func SaveConfig(path string, cfg Config) error { //nolint:gocritic // hugeParam: stable public API
var data []byte
var err error
if isYAML(path) {
data, err = marshalYAML(cfg)
} else {
data, err = json.MarshalIndent(cfg, "", " ")
if err == nil {
data = append(data, '\n')
}
}
if err != nil {
return err
}
return os.WriteFile(path, data, 0o644)
}
// Violation describes a single config validation issue.
type Violation struct {
// Field is the config field name (e.g. "default", "rules[0].glob").
Field string
// Message is a human-readable description of the issue.
Message string
// Severity is "error" for invalid config or "warning" for suboptimal.
Severity string
}
// ValidateConfig checks cfg for structural and semantic issues and
// returns any [Violation] entries found. An empty slice means the
// config is valid. This is intended for CLI feedback and CI pipelines.
func ValidateConfig(cfg Config) []Violation { //nolint:gocritic // hugeParam: stable public API
var vs []Violation
// default must be non-negative.
if cfg.Default < 0 {
vs = append(vs, Violation{
Field: "default",
Message: fmt.Sprintf("must be >= 0, got %d", cfg.Default),
Severity: "error",
})
}
// warning_threshold must be 0-100.
if cfg.WarningThreshold < 0 || cfg.WarningThreshold > 100 {
vs = append(vs, Violation{
Field: "warning_threshold",
Message: fmt.Sprintf("must be 0-100, got %d", cfg.WarningThreshold),
Severity: "error",
})
}
// Rules validation.
for i, r := range cfg.Rules {
field := fmt.Sprintf("rules[%d]", i)
if r.Glob == "" {
vs = append(vs, Violation{
Field: field + ".glob",
Message: "glob pattern is empty",
Severity: "error",
})
} else if _, err := filepath.Match(r.Glob, "test"); err != nil {
vs = append(vs, Violation{
Field: field + ".glob",
Message: fmt.Sprintf("invalid glob pattern %q: %v", r.Glob, err),
Severity: "error",
})
}
if r.Limit != nil && *r.Limit < 0 {
vs = append(vs, Violation{
Field: field + ".limit",
Message: fmt.Sprintf("must be >= 0, got %d", *r.Limit),
Severity: "error",
})
}
}
// Files validation.
for path, limit := range cfg.Files {
field := fmt.Sprintf("files[%q]", path)
if path == "" {
vs = append(vs, Violation{
Field: "files",
Message: "empty file path key",
Severity: "error",
})
}
if limit < 0 {
vs = append(vs, Violation{
Field: field,
Message: fmt.Sprintf("must be >= 0, got %d", limit),
Severity: "error",
})
}
// Warn on backslashes (should use forward slashes).
if strings.Contains(path, "\\") {
vs = append(vs, Violation{
Field: field,
Message: "use forward slashes in file paths",
Severity: "warning",
})
}
// Each path segment must use safe characters (A-Z, a-z, 0-9, _, -, .).
// Normalize backslashes first so Windows-style separators do not produce
// a second violation on top of the backslash warning above.
for _, seg := range strings.Split(strings.ReplaceAll(path, "\\", "/"), "/") {
if seg != "" && !isSafeFileSegment(seg) {
vs = append(vs, Violation{
Field: field,
Message: fmt.Sprintf("path segment %q contains unsafe characters: only A-Z, a-z, 0-9, _, -, . are allowed", seg),
Severity: "error",
})
break
}
}
}
// init_include_files / init_exclude_files must contain safe stem names.
for i, entry := range cfg.InitIncludeFiles {
if bare := templateStem(entry); !isSafeFileSegment(bare) {
vs = append(vs, Violation{
Field: fmt.Sprintf("init_include_files[%d]", i),
Message: fmt.Sprintf("unsafe stem %q: only A-Z, a-z, 0-9, _, -, . are allowed", entry),
Severity: "error",
})
}
}
for i, entry := range cfg.InitExcludeFiles {
if bare := templateStem(entry); !isSafeFileSegment(bare) {
vs = append(vs, Violation{
Field: fmt.Sprintf("init_exclude_files[%d]", i),
Message: fmt.Sprintf("unsafe stem %q: only A-Z, a-z, 0-9, _, -, . are allowed", entry),
Severity: "error",
})
}
}
// Warn on duplicate glob patterns.
seen := make(map[string]int)
for i, r := range cfg.Rules {
if prev, ok := seen[r.Glob]; ok {
vs = append(vs, Violation{
Field: fmt.Sprintf("rules[%d].glob", i),
Message: fmt.Sprintf("duplicate glob %q (first at rules[%d])", r.Glob, prev),
Severity: "warning",
})
}
seen[r.Glob] = i
}
// Warn on files with a less-restrictive limit than a matching rule.
// A stricter per-file limit (lower or zero/exempt) is fine, but a
// higher limit may indicate a misconfiguration.
for path, limit := range cfg.Files {
for _, r := range cfg.Rules {
if ok, _ := filepath.Match(r.Glob, path); ok {
if r.Limit != nil && *r.Limit > 0 && limit > *r.Limit {
vs = append(vs, Violation{
Field: fmt.Sprintf("files[%q]", path),
Message: fmt.Sprintf("limit %d exceeds rule %q (limit %d); per-file entry takes precedence", limit, r.Glob, *r.Limit),
Severity: "warning",
})
}
break
}
}
}
return vs
}