forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflagsets.go
201 lines (161 loc) · 10.5 KB
/
flagsets.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
package commands
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/commands/internal"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/result/processors"
)
const defaultMaxIssuesPerLinter = 50
func setupLintersFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddHackedStringSliceP(fs, "disable", "D", color.GreenString("Disable specific linter"))
internal.AddFlagAndBind(v, fs, fs.Bool, "disable-all", "linters.disable-all", false, color.GreenString("Disable all linters"))
internal.AddHackedStringSliceP(fs, "enable", "E", color.GreenString("Enable specific linter"))
internal.AddFlagAndBind(v, fs, fs.Bool, "enable-all", "linters.enable-all", false, color.GreenString("Enable all linters"))
internal.AddFlagAndBind(v, fs, fs.Bool, "fast", "linters.fast", false,
color.GreenString("Enable only fast linters from enabled linters set (first run won't be fast)"))
internal.AddHackedStringSliceP(fs, "presets", "p",
formatList("Enable presets of linters:", lintersdb.AllPresets(),
"Run 'golangci-lint help linters' to see them.",
"This option implies option --disable-all",
),
)
fs.StringSlice("enable-only", nil,
color.GreenString("Override linters configuration section to only run the specific linter(s)")) // Flags only.
}
func setupFormattersFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBindP(v, fs, fs.StringSliceP, "enable", "E", "formatters.enable", nil,
color.GreenString("Enable specific formatter"))
}
func setupRunFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBindP(v, fs, fs.IntP, "concurrency", "j", "run.concurrency", getDefaultConcurrency(),
color.GreenString("Number of CPUs to use (Default: number of logical CPUs)"))
internal.AddFlagAndBind(v, fs, fs.String, "modules-download-mode", "run.modules-download-mode", "",
color.GreenString("Modules download mode. If not empty, passed as -mod=<mode> to go tools"))
internal.AddFlagAndBind(v, fs, fs.Int, "issues-exit-code", "run.issues-exit-code", exitcodes.IssuesFound,
color.GreenString("Exit code when issues were found"))
internal.AddFlagAndBind(v, fs, fs.String, "go", "run.go", "", color.GreenString("Targeted Go version"))
internal.AddHackedStringSlice(fs, "build-tags", color.GreenString("Build tags"))
internal.AddFlagAndBind(v, fs, fs.Duration, "timeout", "run.timeout", defaultTimeout,
color.GreenString("Timeout for total work. If <= 0, the timeout is disabled"))
internal.AddFlagAndBind(v, fs, fs.Bool, "tests", "run.tests", true, color.GreenString("Analyze tests (*_test.go)"))
internal.AddDeprecatedHackedStringSlice(fs, "skip-files", color.GreenString("Regexps of files to skip"))
internal.AddDeprecatedHackedStringSlice(fs, "skip-dirs", color.GreenString("Regexps of directories to skip"))
const allowParallelDesc = "Allow multiple parallel golangci-lint instances running.\n" +
"If false (default) - golangci-lint acquires file lock on start."
internal.AddFlagAndBind(v, fs, fs.Bool, "allow-parallel-runners", "run.allow-parallel-runners", false,
color.GreenString(allowParallelDesc))
const allowSerialDesc = "Allow multiple golangci-lint instances running, but serialize them around a lock.\n" +
"If false (default) - golangci-lint exits with an error if it fails to acquire file lock on start."
internal.AddFlagAndBind(v, fs, fs.Bool, "allow-serial-runners", "run.allow-serial-runners", false, color.GreenString(allowSerialDesc))
}
func setupOutputFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddFlagAndBind(v, fs, fs.Bool, "sort-results", "output.sort-results", false,
color.GreenString("Sort linter results"))
internal.AddFlagAndBind(v, fs, fs.StringSlice, "sort-order", "output.sort-order", nil,
color.GreenString("Sort order of linter results"))
internal.AddFlagAndBind(v, fs, fs.String, "path-prefix", "output.path-prefix", "",
color.GreenString("Path prefix to add to output"))
internal.AddFlagAndBind(v, fs, fs.Bool, "show-stats", "output.show-stats", false, color.GreenString("Show statistics per linter"))
setupOutputFormatsFlagSet(v, fs)
}
func setupOutputFormatsFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
outputPathDesc := "Output path can be either `stdout`, `stderr` or path to the file to write to."
printLinterNameDesc := "Print linter name in the end of issue text."
colorsDesc := "Use colors."
internal.AddFlagAndBind(v, fs, fs.String, "output.text.path", "output.formats.text.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-linter-name", "output.formats.text.print-linter-name", true,
color.GreenString(printLinterNameDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.print-issued-lines", "output.formats.text.print-issued-lines", true,
color.GreenString("Print lines of code with issue."))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.text.colors", "output.formats.text.colors", true,
color.GreenString(colorsDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.json.path", "output.formats.json.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.tab.path", "output.formats.tab.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.print-linter-name", "output.formats.tab.print-linter-name",
true, color.GreenString(printLinterNameDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.tab.colors", "output.formats.tab.colors", true,
color.GreenString(colorsDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.html.path", "output.formats.html.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.checkstyle.path", "output.formats.checkstyle.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.code-climate.path", "output.formats.code-climate.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.junit-xml.path", "output.formats.junit-xml.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.Bool, "output.junit-xml.extended", "output.formats.junit-xml.extended", true,
color.GreenString("Support extra JUnit XML fields."))
internal.AddFlagAndBind(v, fs, fs.String, "output.teamcity.path", "output.formats.teamcity.path", "",
color.GreenString(outputPathDesc))
internal.AddFlagAndBind(v, fs, fs.String, "output.sarif.path", "output.formats.sarif.path", "",
color.GreenString(outputPathDesc))
}
//nolint:gomnd // magic numbers here is ok
func setupIssuesFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
internal.AddHackedStringSliceP(fs, "exclude", "e", color.GreenString("Exclude issue by regexp"))
internal.AddFlagAndBind(v, fs, fs.Bool, "exclude-use-default", "issues.exclude-use-default", true,
getDefaultIssueExcludeHelp())
internal.AddFlagAndBind(v, fs, fs.Bool, "exclude-case-sensitive", "issues.exclude-case-sensitive", false,
color.GreenString("If set to true exclude and exclude rules regular expressions are case-sensitive"))
internal.AddFlagAndBind(v, fs, fs.Int, "max-issues-per-linter", "issues.max-issues-per-linter", defaultMaxIssuesPerLinter,
color.GreenString("Maximum issues count per one linter. Set to 0 to disable"))
internal.AddFlagAndBind(v, fs, fs.Int, "max-same-issues", "issues.max-same-issues", 3,
color.GreenString("Maximum count of issues with the same text. Set to 0 to disable"))
internal.AddFlagAndBind(v, fs, fs.Bool, "uniq-by-line", "issues.uniq-by-line", true,
color.GreenString("Make issues output unique by line"))
internal.AddHackedStringSlice(fs, "exclude-files", color.GreenString("Regexps of files to exclude"))
internal.AddHackedStringSlice(fs, "exclude-dirs", color.GreenString("Regexps of directories to exclude"))
internal.AddFlagAndBind(v, fs, fs.Bool, "exclude-dirs-use-default", "issues.exclude-dirs-use-default", true,
formatList("Use or not use default excluded directories:", processors.StdExcludeDirRegexps))
internal.AddFlagAndBind(v, fs, fs.String, "exclude-generated", "issues.exclude-generated", config.GeneratedModeLax,
color.GreenString("Mode of the generated files analysis"))
const newDesc = "Show only new issues: if there are unstaged changes or untracked files, only those changes " +
"are analyzed, else only changes in HEAD~ are analyzed.\nIt's a super-useful option for integration " +
"of golangci-lint into existing large codebase.\nIt's not practical to fix all existing issues at " +
"the moment of integration: much better to not allow issues in new code.\nFor CI setups, prefer " +
"--new-from-rev=HEAD~, as --new can skip linting the current patch if any scripts generate " +
"unstaged files before golangci-lint runs."
internal.AddFlagAndBindP(v, fs, fs.BoolP, "new", "n", "issues.new", false, color.GreenString(newDesc))
internal.AddFlagAndBind(v, fs, fs.String, "new-from-rev", "issues.new-from-rev", "",
color.GreenString("Show only new issues created after git revision `REV`"))
internal.AddFlagAndBind(v, fs, fs.String, "new-from-patch", "issues.new-from-patch", "",
color.GreenString("Show only new issues created in git patch with file path `PATH`"))
internal.AddFlagAndBind(v, fs, fs.String, "new-from-merge-base", "issues.new-from-merge-base", "",
color.GreenString("Show only new issues created after the best common ancestor (merge-base against HEAD)"))
internal.AddFlagAndBind(v, fs, fs.Bool, "whole-files", "issues.whole-files", false,
color.GreenString("Show issues in any part of update files (requires new-from-rev or new-from-patch)"))
internal.AddFlagAndBind(v, fs, fs.Bool, "fix", "issues.fix", false,
color.GreenString("Fix found issues (if it's supported by the linter)"))
}
func formatList(head string, items []string, foot ...string) string {
parts := []string{color.GreenString(head)}
for _, p := range items {
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(p)))
}
for _, s := range foot {
parts = append(parts, color.GreenString(s))
}
if len(foot) == 0 {
parts = append(parts, "")
}
return strings.Join(parts, "\n")
}
func getDefaultIssueExcludeHelp() string {
parts := []string{color.GreenString("Use or not use default excludes:")}
for _, ep := range config.DefaultExcludePatterns {
parts = append(parts,
fmt.Sprintf(" - %s (%s): %s", color.BlueString(ep.ID), color.CyanString(ep.Linter), ep.Why),
fmt.Sprintf(` Pattern: %s`, color.YellowString(`'`+ep.Pattern+`'`)),
)
}
return strings.Join(parts, "\n")
}