-
Notifications
You must be signed in to change notification settings - Fork 4
/
lintflags.go
452 lines (396 loc) · 10.5 KB
/
lintflags.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package lintflags
import (
"fmt"
"runtime"
"sort"
"strings"
"time"
"github.com/urfave/cli"
)
var (
// defaults as gometalinter expects them
defaultConcurrency = 16
defaultCycloOver = 10
defaultLineLength = 80
defaultMinConfidence = 0.80
defaultMinOccurrences = 3
defaultMinConstLength = 3
defaultDuplThreshold = 50
defaultDeadline = 5 * time.Second
defaultSort = SortNone
// defaultFormat = "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})"
)
type Data struct {
NoVendoredLinters bool
Fast bool
Install bool
Update bool
Force bool
Debug bool
Concurrency int
Exclude cli.StringSlice
Include cli.StringSlice
Skip cli.StringSlice
// Vendor bool
CycloOver int
LineLength int
MinConfidence float64
MinOccurrences int
MinConstLength int
DuplThreshold int
Sort SortKey
NoTests bool
Deadline time.Duration
Errors bool
JSON bool
Checkstyle bool
EnableGC bool
Aggregate bool
Disable cli.StringSlice
Enable cli.StringSlice
Linter cli.StringSlice
MessageOverrides cli.StringSlice
Severity cli.StringSlice
DisableAll bool
EnableAll bool
// Format string
}
func (f *Data) LintFlags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "no-vendored-linters",
Usage: "Do not use vendored linters (not recommended).",
Destination: &f.NoVendoredLinters,
},
cli.BoolFlag{
Name: "fast",
Usage: "Only run fast linters.",
Destination: &f.Fast,
},
cli.BoolFlag{
Name: "install, i",
Usage: "Attempt to install all known linters.",
Destination: &f.Install,
},
cli.BoolFlag{
Name: "update, u",
Usage: "Pass -u to go tool when installing.",
Destination: &f.Update,
},
cli.BoolFlag{
Name: "force, f",
Usage: "Pass -f to go tool when installing. When linting treats all lint results as uncached.",
Destination: &f.Force,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Display messages for failed linters, etc.",
Destination: &f.Debug,
},
cli.IntFlag{
Name: "concurrency, j",
Usage: "Number of concurrent linters to run.",
Value: getConcurrency(),
Destination: &f.Concurrency,
},
cli.StringSliceFlag{
Name: "exclude, e",
Usage: "Exclude messages matching these regular expressions.",
Value: &f.Exclude,
},
cli.StringSliceFlag{
Name: "include, I",
Usage: "Include messages matching these regular expressions.",
Value: &f.Include,
},
cli.StringSliceFlag{
Name: "skip, s",
Usage: "Skip directories with this name when expanding '...'.",
Value: &f.Skip,
},
// cli.BoolFlag{
// Name: "vendor",
// Usage: "Enable vendoring support (skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1).",
// Destination: &f.Vendor,
// },
cli.IntFlag{
Name: "cyclo-over",
Usage: "Report functions with cyclomatic complexity over N (using gocyclo).",
Value: defaultCycloOver,
Destination: &f.CycloOver,
},
cli.IntFlag{
Name: "line-length",
Usage: "Report lines longer than N (using lll).",
Value: defaultLineLength,
Destination: &f.LineLength,
},
cli.Float64Flag{
Name: "min-confidence",
Usage: "Minimum confidence interval to pass to golint.",
Value: defaultMinConfidence,
Destination: &f.MinConfidence,
},
cli.IntFlag{
Name: "min-occurrences",
Usage: "Minimum occurrences to pass to goconst.",
Value: defaultMinOccurrences,
Destination: &f.MinOccurrences,
},
cli.IntFlag{
Name: "min-const-length",
Usage: "Minimumum constant length.",
Value: defaultMinConstLength,
Destination: &f.MinConstLength,
},
cli.IntFlag{
Name: "dupl-threshold",
Usage: "Minimum token sequence as a clone for dupl.",
Value: defaultDuplThreshold,
Destination: &f.DuplThreshold,
},
cli.GenericFlag{
Name: "sort",
Usage: fmt.Sprintf("Sort output by any of %s.", strings.Join(sortKeysStr, ", ")),
Value: &f.Sort,
},
cli.BoolFlag{
Name: "no-tests",
Usage: "Do not include test files for linters that support this option",
Destination: &f.NoTests,
},
cli.DurationFlag{
Name: "deadline",
Usage: "Cancel linters if they have not completed within this duration.",
Value: 30 * time.Second,
Destination: &f.Deadline,
},
cli.BoolFlag{
Name: "errors",
Usage: "Only show errors.",
Destination: &f.Errors,
},
cli.BoolFlag{
Name: "json",
Usage: "Generate structured JSON rather than standard line-based output.",
Destination: &f.JSON,
},
cli.BoolFlag{
Name: "checkstyle",
Usage: "Generate checkstyle XML rather than standard line-based output.",
Destination: &f.Checkstyle,
},
cli.BoolFlag{
Name: "enable-gc",
Usage: "Do not enable GC for linters (useful on large repositories).",
Destination: &f.EnableGC,
},
cli.BoolFlag{
Name: "aggregate",
Usage: "Aggregate issues reported by several linters.",
Destination: &f.Aggregate,
},
cli.StringSliceFlag{
Name: "disable, D",
Usage: fmt.Sprintf("List of linters to disable (%s).", strings.Join(disabledLinters, ",")),
Value: &f.Disable,
},
cli.StringSliceFlag{
Name: "enable, E",
Usage: fmt.Sprintf("Enable previously disabled linters (%s).", strings.Join(enabledLinters, ",")),
Value: &f.Enable,
},
cli.StringSliceFlag{
Name: "linter",
Usage: "Specify a linter.",
Value: &f.Linter,
},
cli.StringSliceFlag{
Name: "message-overrides",
Usage: "Override message from linter. {message} will be expanded to the original message.",
Value: &f.MessageOverrides,
},
cli.StringSliceFlag{
Name: "severity",
Usage: "Map of linter severities.",
Value: &f.Severity,
},
cli.BoolFlag{
Name: "disable-all",
Usage: "Disable all linters.",
Destination: &f.DisableAll,
},
cli.BoolFlag{
Name: "enable-all",
Usage: "Enable all linters.",
Destination: &f.EnableAll,
},
// cli.StringFlag{
// Name: "format",
// Usage: "Output format.",
// Value: defaultFormat,
// Destination: &f.Format,
// },
}
}
func getConcurrency() int {
// return at least 1
// the lesser of:
// * if even number of cpus, 1 less than half that number
// * if odd number of cpus, half that number rounded down (truncated)
// * GOMAXPROCS
n := runtime.NumCPU()
var cpu int
if n%2 == 0 { // even
cpu = n/2 - 1
} else { // odd
cpu = n / 2
}
m := runtime.GOMAXPROCS(-1)
r := m
if cpu < m {
r = cpu
}
if r <= 0 {
r = 1
}
return r
}
// these are disabled in zb by default
var disabledLinters = []string{"aligncheck", "gocyclo"}
// these are enabled in zb by default
var enabledLinters = []string{"gofmt", "goimports", "misspell"}
const (
disable = "-D"
enable = "-E"
)
func (f *Data) linters() []string {
lm := map[string]string{}
for _, linter := range disabledLinters {
lm[linter] = disable
}
for _, linter := range enabledLinters {
lm[linter] = enable
}
for _, v := range f.Disable {
lm[v] = disable
}
for _, v := range f.Enable {
lm[v] = enable
}
var disabled, enabled []string
for l, f := range lm {
if f == disable {
disabled = append(disabled, l)
} else {
enabled = append(enabled, l)
}
}
sort.Strings(disabled)
sort.Strings(enabled)
var args []string
for _, v := range disabled {
args = append(args, disable, v)
}
for _, v := range enabled {
args = append(args, enable, v)
}
if f.Fast {
args = append(args, "--fast")
}
if f.DisableAll {
args = append(args, "--disable-all")
}
if f.EnableAll {
args = append(args, "--enable-all")
}
return args
}
func (f *Data) LintArgs() []string {
var args []string
if f.NoVendoredLinters {
args = append(args, "--no-vendored-linters")
}
if f.Install {
args = append(args, "--install")
if f.Update {
args = append(args, "--update")
}
if f.Force {
args = append(args, "--force")
}
}
if f.Debug {
args = append(args, "--debug")
}
if f.Concurrency != 0 && f.Concurrency != defaultConcurrency {
args = append(args, "-j", fmt.Sprintf("%d", f.Concurrency))
}
for _, v := range f.Exclude {
args = append(args, "--exclude", v)
}
for _, v := range f.Include {
args = append(args, "--include", v)
}
for _, v := range f.Skip {
args = append(args, "--skip", v)
}
// if f.Vendor {
// args = append(args, "--vendor")
// }
if f.CycloOver != 0 && f.CycloOver != defaultCycloOver {
args = append(args, "--cyclo-over", fmt.Sprintf("%d", f.CycloOver))
}
if f.LineLength != 0 && f.LineLength != defaultLineLength {
args = append(args, "--line-length", fmt.Sprintf("%d", f.LineLength))
}
if f.MinConfidence != 0 && f.MinConfidence != defaultMinConfidence {
args = append(args, "--min-confidence", fmt.Sprintf("%f", f.MinConfidence))
}
if f.MinOccurrences != 0 && f.MinOccurrences != defaultMinOccurrences {
args = append(args, "--min-occurrences", fmt.Sprintf("%d", f.MinOccurrences))
}
if f.MinConstLength != 0 && f.MinConstLength != defaultMinConstLength {
args = append(args, "--min-const-length", fmt.Sprintf("%d", f.MinConstLength))
}
if f.DuplThreshold != 0 && f.DuplThreshold != defaultDuplThreshold {
args = append(args, "--dupl-threshold", fmt.Sprintf("%d", f.DuplThreshold))
}
if f.Sort != "" && f.Sort != defaultSort {
args = append(args, "--sort", string(f.Sort))
}
if !f.NoTests {
args = append(args, "--tests")
}
if f.Deadline != 0 && f.Deadline != defaultDeadline {
args = append(args, "--deadline", f.Deadline.String())
}
if f.Errors {
args = append(args, "--errors")
}
if f.JSON {
args = append(args, "--json")
}
if f.Checkstyle {
args = append(args, "--checkstyle")
}
if f.EnableGC {
args = append(args, "--enable-gc")
}
if f.Aggregate {
args = append(args, "--aggregate")
}
for _, v := range f.Linter {
args = append(args, "--linter", v)
}
for _, v := range f.MessageOverrides {
args = append(args, "--message-overrides", v)
}
for _, v := range f.Severity {
args = append(args, "--severity", v)
}
// if f.Format != "" && f.Format != defaultFormat {
// args = append(args, "--format", f.Format)
// }
return append(args, f.linters()...)
}