Skip to content

Commit a727aa5

Browse files
Mateus Oliveiraldez
andauthored
dev: remove unrelated flags from config and linters command (#4284)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 4ea9468 commit a727aa5

File tree

6 files changed

+91
-62
lines changed

6 files changed

+91
-62
lines changed

.golangci.reference.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2649,7 +2649,7 @@ linters:
26492649
- test
26502650
- unused
26512651

2652-
# Run only fast linters from enabled linters set (first run won't be fast)
2652+
# Enable only fast linters from enabled linters set (first run won't be fast)
26532653
# Default: false
26542654
fast: true
26552655

pkg/commands/config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import (
55
"os"
66

77
"github.com/spf13/cobra"
8+
"github.com/spf13/pflag"
89
"github.com/spf13/viper"
910

11+
"github.com/golangci/golangci-lint/pkg/config"
1012
"github.com/golangci/golangci-lint/pkg/exitcodes"
1113
"github.com/golangci/golangci-lint/pkg/fsutils"
1214
)
1315

1416
func (e *Executor) initConfig() {
1517
cmd := &cobra.Command{
1618
Use: "config",
17-
Short: "Config",
19+
Short: "Config file information",
1820
Args: cobra.NoArgs,
1921
RunE: func(cmd *cobra.Command, _ []string) error {
2022
return cmd.Help()
@@ -29,7 +31,12 @@ func (e *Executor) initConfig() {
2931
ValidArgsFunction: cobra.NoFileCompletions,
3032
Run: e.executePathCmd,
3133
}
32-
e.initRunConfiguration(pathCmd) // allow --config
34+
35+
fs := pathCmd.Flags()
36+
fs.SortFlags = false // sort them as they are defined here
37+
38+
initConfigFileFlagSet(fs, &e.cfg.Run)
39+
3340
cmd.AddCommand(pathCmd)
3441
}
3542

@@ -59,3 +66,8 @@ func (e *Executor) executePathCmd(_ *cobra.Command, _ []string) {
5966

6067
fmt.Println(usedConfigFile)
6168
}
69+
70+
func initConfigFileFlagSet(fs *pflag.FlagSet, cfg *config.Run) {
71+
fs.StringVarP(&cfg.Config, "config", "c", "", wh("Read config from file path `PATH`"))
72+
fs.BoolVar(&cfg.NoConfig, "no-config", false, wh("Don't read config file"))
73+
}

pkg/commands/linters.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package commands
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/fatih/color"
78
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
810

11+
"github.com/golangci/golangci-lint/pkg/config"
912
"github.com/golangci/golangci-lint/pkg/lint/linter"
1013
)
1114

@@ -17,8 +20,25 @@ func (e *Executor) initLinters() {
1720
ValidArgsFunction: cobra.NoFileCompletions,
1821
RunE: e.executeLinters,
1922
}
23+
24+
fs := e.lintersCmd.Flags()
25+
fs.SortFlags = false // sort them as they are defined here
26+
27+
initConfigFileFlagSet(fs, &e.cfg.Run)
28+
e.initLintersFlagSet(fs, &e.cfg.Linters)
29+
2030
e.rootCmd.AddCommand(e.lintersCmd)
21-
e.initRunConfiguration(e.lintersCmd)
31+
}
32+
33+
func (e *Executor) initLintersFlagSet(fs *pflag.FlagSet, cfg *config.Linters) {
34+
fs.StringSliceVarP(&cfg.Disable, "disable", "D", nil, wh("Disable specific linter"))
35+
fs.BoolVar(&cfg.DisableAll, "disable-all", false, wh("Disable all linters"))
36+
fs.StringSliceVarP(&cfg.Enable, "enable", "E", nil, wh("Enable specific linter"))
37+
fs.BoolVar(&cfg.EnableAll, "enable-all", false, wh("Enable all linters"))
38+
fs.BoolVar(&cfg.Fast, "fast", false, wh("Enable only fast linters from enabled linters set (first run won't be fast)"))
39+
fs.StringSliceVarP(&cfg.Presets, "presets", "p", nil,
40+
wh(fmt.Sprintf("Enable presets (%s) of linters. Run 'golangci-lint help linters' to see "+
41+
"them. This option implies option --disable-all", strings.Join(e.DBManager.AllPresets(), "|"))))
2242
}
2343

2444
// executeLinters runs the 'linters' CLI command, which displays the supported linters.
@@ -28,28 +48,23 @@ func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error {
2848
return fmt.Errorf("can't get enabled linters: %w", err)
2949
}
3050

31-
color.Green("Enabled by your configuration linters:\n")
3251
var enabledLinters []*linter.Config
33-
for _, lc := range enabledLintersMap {
34-
if lc.Internal {
35-
continue
36-
}
37-
38-
enabledLinters = append(enabledLinters, lc)
39-
}
40-
printLinterConfigs(enabledLinters)
41-
4252
var disabledLCs []*linter.Config
53+
4354
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
4455
if lc.Internal {
4556
continue
4657
}
4758

4859
if enabledLintersMap[lc.Name()] == nil {
4960
disabledLCs = append(disabledLCs, lc)
61+
} else {
62+
enabledLinters = append(enabledLinters, lc)
5063
}
5164
}
5265

66+
color.Green("Enabled by your configuration linters:\n")
67+
printLinterConfigs(enabledLinters)
5368
color.Red("\nDisabled by your configuration linters:\n")
5469
printLinterConfigs(disabledLCs)
5570

pkg/commands/run.go

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,17 @@ import (
2929

3030
const defaultFileMode = 0644
3131

32+
const defaultTimeout = time.Minute
33+
3234
const (
3335
// envFailOnWarnings value: "1"
3436
envFailOnWarnings = "FAIL_ON_WARNINGS"
3537
// envMemLogEvery value: "1"
3638
envMemLogEvery = "GL_MEM_LOG_EVERY"
3739
)
3840

39-
func getDefaultIssueExcludeHelp() string {
40-
parts := []string{color.GreenString("Use or not use default excludes:")}
41-
for _, ep := range config.DefaultExcludePatterns {
42-
parts = append(parts,
43-
fmt.Sprintf(" # %s %s: %s", ep.ID, ep.Linter, ep.Why),
44-
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
45-
"",
46-
)
47-
}
48-
return strings.Join(parts, "\n")
49-
}
50-
51-
func getDefaultDirectoryExcludeHelp() string {
52-
parts := []string{color.GreenString("Use or not use default excluded directories:")}
53-
for _, dir := range packages.StdExcludeDirRegexps {
54-
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(dir)))
55-
}
56-
parts = append(parts, "")
57-
return strings.Join(parts, "\n")
58-
}
59-
60-
func wh(text string) string {
61-
return color.GreenString(text)
62-
}
63-
64-
const defaultTimeout = time.Minute
65-
6641
//nolint:funlen,gomnd
67-
func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, isFinalInit bool) {
42+
func (e *Executor) initFlagSet(fs *pflag.FlagSet, cfg *config.Config, isFinalInit bool) {
6843
hideFlag := func(name string) {
6944
if err := fs.MarkHidden(name); err != nil {
7045
panic(err)
@@ -79,6 +54,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
7954
}
8055
}
8156

57+
// Config file config
58+
rc := &cfg.Run
59+
initConfigFileFlagSet(fs, rc)
60+
8261
// Output config
8362
oc := &cfg.Output
8463
fs.StringVar(&oc.Format, "out-format",
@@ -98,7 +77,6 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
9877
}
9978

10079
// Run config
101-
rc := &cfg.Run
10280
fs.StringVar(&rc.ModulesDownloadMode, "modules-download-mode", "",
10381
wh("Modules download mode. If not empty, passed as -mod=<mode> to go tools"))
10482
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
@@ -115,8 +93,6 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
11593
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
11694
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false,
11795
wh("Print avg and max memory usage of golangci-lint and total time"))
118-
fs.StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`"))
119-
fs.BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config"))
12096
fs.StringSliceVar(&rc.SkipDirs, "skip-dirs", nil, wh("Regexps of directories to skip"))
12197
fs.BoolVar(&rc.UseDefaultSkipDirs, "skip-dirs-use-default", true, getDefaultDirectoryExcludeHelp())
12298
fs.StringSliceVar(&rc.SkipFiles, "skip-files", nil, wh("Regexps of files to skip"))
@@ -200,15 +176,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
200176

201177
// Linters config
202178
lc := &cfg.Linters
203-
fs.StringSliceVarP(&lc.Enable, "enable", "E", nil, wh("Enable specific linter"))
204-
fs.StringSliceVarP(&lc.Disable, "disable", "D", nil, wh("Disable specific linter"))
205-
fs.BoolVar(&lc.EnableAll, "enable-all", false, wh("Enable all linters"))
206-
207-
fs.BoolVar(&lc.DisableAll, "disable-all", false, wh("Disable all linters"))
208-
fs.StringSliceVarP(&lc.Presets, "presets", "p", nil,
209-
wh(fmt.Sprintf("Enable presets (%s) of linters. Run 'golangci-lint help linters' to see "+
210-
"them. This option implies option --disable-all", strings.Join(m.AllPresets(), "|"))))
211-
fs.BoolVar(&lc.Fast, "fast", false, wh("Run only fast linters from enabled linters set (first run won't be fast)"))
179+
e.initLintersFlagSet(fs, lc)
212180

213181
// Issues config
214182
ic := &cfg.Issues
@@ -241,7 +209,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
241209
func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
242210
fs := cmd.Flags()
243211
fs.SortFlags = false // sort them as they are defined here
244-
initFlagSet(fs, e.cfg, e.DBManager, true)
212+
e.initFlagSet(fs, e.cfg, true)
245213
}
246214

247215
func (e *Executor) getConfigForCommandLine() (*config.Config, error) {
@@ -254,7 +222,7 @@ func (e *Executor) getConfigForCommandLine() (*config.Config, error) {
254222
// `changed` variable inside string slice vars will be shared.
255223
// Use another config variable here, not e.cfg, to not
256224
// affect main parsing by this parsing of only config option.
257-
initFlagSet(fs, &cfg, e.DBManager, false)
225+
e.initFlagSet(fs, &cfg, false)
258226
initVersionFlagSet(fs, &cfg)
259227

260228
// Parse max options, even force version option: don't want
@@ -639,3 +607,28 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
639607
logger.Infof("Execution took %s", time.Since(startedAt))
640608
close(done)
641609
}
610+
611+
func getDefaultIssueExcludeHelp() string {
612+
parts := []string{color.GreenString("Use or not use default excludes:")}
613+
for _, ep := range config.DefaultExcludePatterns {
614+
parts = append(parts,
615+
fmt.Sprintf(" # %s %s: %s", ep.ID, ep.Linter, ep.Why),
616+
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
617+
"",
618+
)
619+
}
620+
return strings.Join(parts, "\n")
621+
}
622+
623+
func getDefaultDirectoryExcludeHelp() string {
624+
parts := []string{color.GreenString("Use or not use default excluded directories:")}
625+
for _, dir := range packages.StdExcludeDirRegexps {
626+
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(dir)))
627+
}
628+
parts = append(parts, "")
629+
return strings.Join(parts, "\n")
630+
}
631+
632+
func wh(text string) string {
633+
return color.GreenString(text)
634+
}

test/testshared/runner.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,13 @@ func (b *RunnerBuilder) Runner() *Runner {
163163
b.tb.Fatal("--no-config and -c cannot be used at the same time")
164164
}
165165

166-
arguments := []string{
167-
"--internal-cmd-test",
168-
}
166+
var arguments []string
169167

170-
if b.allowParallelRunners {
171-
arguments = append(arguments, "--allow-parallel-runners")
168+
if b.command == "run" {
169+
arguments = append(arguments, "--internal-cmd-test")
170+
if b.allowParallelRunners {
171+
arguments = append(arguments, "--allow-parallel-runners")
172+
}
172173
}
173174

174175
if b.noConfig {

test/testshared/runner_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ func TestRunnerBuilder_Runner(t *testing.T) {
2828
},
2929
},
3030
{
31-
desc: "with command",
31+
desc: "with non run command",
3232
builder: NewRunnerBuilder(t).WithCommand("example"),
3333
expected: &Runner{
3434
env: []string(nil),
3535
command: "example",
36+
},
37+
},
38+
{
39+
desc: "with run command",
40+
builder: NewRunnerBuilder(t).WithCommand("run"),
41+
expected: &Runner{
42+
env: []string(nil),
43+
command: "run",
3644
args: []string{
3745
"--internal-cmd-test",
3846
"--allow-parallel-runners",

0 commit comments

Comments
 (0)