-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
229 lines (207 loc) · 5.39 KB
/
config.go
File metadata and controls
229 lines (207 loc) · 5.39 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
package inspect
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// FileConfig represents the contents of an .inspect.toml configuration file.
type FileConfig struct {
Depth int `json:"depth"`
Checks []string `json:"checks"`
Exclude []string `json:"exclude"`
FailOn string `json:"fail_on"`
Concurrency int `json:"concurrency"`
RateLimit int `json:"rate_limit"`
Timeout string `json:"timeout"`
PageTimeout string `json:"page_timeout"`
UserAgent string `json:"user_agent"`
AuthHeader string `json:"auth_header"`
AuthValue string `json:"auth_value"`
AcceptedStatusCodes []int `json:"accepted_status_codes"`
}
// LoadConfig reads .inspect.toml or .inspect.yaml from the given directory
// (searching upward to parent directories). Returns nil options and nil error
// if no config file is found. Returns an error only on malformed files.
func LoadConfig(dir string) ([]Option, error) {
fc, err := loadConfigFile(dir)
if err != nil {
return nil, err
}
if fc == nil {
return nil, nil
}
return applyFileConfig(fc), nil
}
func loadConfigFile(dir string) (*FileConfig, error) {
path := findInspectConfigFile(dir)
if path == "" {
return nil, nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return parseInspectTOML(string(data))
}
func findInspectConfigFile(dir string) string {
names := []string{".inspect.toml", ".inspect.yaml", "inspect.toml"}
for {
for _, name := range names {
path := filepath.Join(dir, name)
if _, err := os.Stat(path); err == nil {
return path
}
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return ""
}
// parseInspectTOML parses a simplified TOML format.
// Supports key = "value", key = true/false, key = 123, arrays, and [section] headers.
func parseInspectTOML(content string) (*FileConfig, error) {
cfg := &FileConfig{}
for _, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Skip section headers for now (flat config)
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
continue
}
key, value, ok := parseInspectKeyValue(line)
if !ok {
continue
}
switch key {
case "depth":
if n := parseIntVal(value); n > 0 {
cfg.Depth = n
}
case "checks":
cfg.Checks = parseTOMLArrayVal(value)
case "exclude":
cfg.Exclude = parseTOMLArrayVal(value)
case "fail_on":
cfg.FailOn = value
case "concurrency":
if n := parseIntVal(value); n > 0 {
cfg.Concurrency = n
}
case "rate_limit":
if n := parseIntVal(value); n > 0 {
cfg.RateLimit = n
}
case "timeout":
cfg.Timeout = value
case "page_timeout":
cfg.PageTimeout = value
case "user_agent":
cfg.UserAgent = value
case "auth_header":
cfg.AuthHeader = value
case "auth_value":
cfg.AuthValue = value
case "accepted_status_codes":
cfg.AcceptedStatusCodes = parseTOMLIntArray(value)
}
}
return cfg, nil
}
// parseInspectKeyValue splits a TOML line into key and value, handling quoted
// values that may contain '=' signs.
func parseInspectKeyValue(line string) (key, value string, ok bool) {
idx := strings.Index(line, "=")
if idx < 0 {
return "", "", false
}
key = strings.TrimSpace(line[:idx])
value = strings.TrimSpace(line[idx+1:])
// Strip matching outer quotes
if len(value) >= 2 {
if (value[0] == '"' && value[len(value)-1] == '"') ||
(value[0] == '\'' && value[len(value)-1] == '\'') {
value = value[1 : len(value)-1]
}
}
return key, value, key != ""
}
func applyFileConfig(fc *FileConfig) []Option {
if fc == nil {
return nil
}
var opts []Option
if fc.Depth > 0 {
opts = append(opts, WithDepth(fc.Depth))
}
if len(fc.Checks) > 0 {
opts = append(opts, WithChecks(fc.Checks...))
}
if len(fc.Exclude) > 0 {
opts = append(opts, WithExclude(fc.Exclude...))
}
if fc.FailOn != "" {
opts = append(opts, WithFailOn(ParseSeverity(fc.FailOn)))
}
if fc.Concurrency > 0 {
opts = append(opts, WithConcurrency(fc.Concurrency))
}
if fc.RateLimit > 0 {
opts = append(opts, WithRateLimit(fc.RateLimit))
}
if fc.Timeout != "" {
if d, err := time.ParseDuration(fc.Timeout); err == nil {
opts = append(opts, WithTimeout(d))
}
}
if fc.PageTimeout != "" {
if d, err := time.ParseDuration(fc.PageTimeout); err == nil {
opts = append(opts, WithPageTimeout(d))
}
}
if fc.UserAgent != "" {
opts = append(opts, WithUserAgent(fc.UserAgent))
}
if fc.AuthHeader != "" && fc.AuthValue != "" {
opts = append(opts, WithAuth(fc.AuthHeader, fc.AuthValue))
}
if len(fc.AcceptedStatusCodes) > 0 {
opts = append(opts, WithAcceptedStatusCodes(fc.AcceptedStatusCodes...))
}
return opts
}
func parseTOMLArrayVal(s string) []string {
s = strings.Trim(s, "[]\"'")
parts := strings.Split(s, ",")
var result []string
for _, p := range parts {
p = strings.TrimSpace(p)
p = strings.Trim(p, `"'`)
if p != "" {
result = append(result, p)
}
}
return result
}
func parseTOMLIntArray(s string) []int {
s = strings.Trim(s, "[]")
parts := strings.Split(s, ",")
var result []int
for _, p := range parts {
p = strings.TrimSpace(p)
if n, err := strconv.Atoi(p); err == nil {
result = append(result, n)
}
}
return result
}
func parseIntVal(s string) int {
n, _ := strconv.Atoi(s)
return n
}