Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 2, 2024
1 parent da2bd78 commit 3bb4f79
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
12 changes: 11 additions & 1 deletion pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
)
Expand All @@ -29,9 +31,12 @@ func (e *Executor) initConfig() {
ValidArgsFunction: cobra.NoFileCompletions,
Run: e.executePathCmd,
}

fs := pathCmd.Flags()
fs.SortFlags = false // sort them as they are defined here
e.initConfigFileFlagSet(fs, &e.cfg.Run)

initConfigFileFlagSet(fs, &e.cfg.Run)

cmd.AddCommand(pathCmd)
}

Expand Down Expand Up @@ -61,3 +66,8 @@ func (e *Executor) executePathCmd(_ *cobra.Command, _ []string) {

fmt.Println(usedConfigFile)
}

func initConfigFileFlagSet(fs *pflag.FlagSet, cfg *config.Run) {
fs.StringVarP(&cfg.Config, "config", "c", "", wh("Read config from file path `PATH`"))
fs.BoolVar(&cfg.NoConfig, "no-config", false, wh("Don't read config file"))
}
28 changes: 20 additions & 8 deletions pkg/commands/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package commands

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)

Expand All @@ -17,13 +20,27 @@ func (e *Executor) initLinters() {
ValidArgsFunction: cobra.NoFileCompletions,
RunE: e.executeLinters,
}

fs := e.lintersCmd.Flags()
fs.SortFlags = false // sort them as they are defined here
e.initConfigFileFlagSet(fs, &e.cfg.Run)

initConfigFileFlagSet(fs, &e.cfg.Run)
e.initLintersFlagSet(fs, &e.cfg.Linters)

e.rootCmd.AddCommand(e.lintersCmd)
}

func (e *Executor) initLintersFlagSet(fs *pflag.FlagSet, cfg *config.Linters) {
fs.StringSliceVarP(&cfg.Disable, "disable", "D", nil, wh("Disable specific linter"))
fs.BoolVar(&cfg.DisableAll, "disable-all", false, wh("Disable all linters"))
fs.StringSliceVarP(&cfg.Enable, "enable", "E", nil, wh("Enable specific linter"))
fs.BoolVar(&cfg.EnableAll, "enable-all", false, wh("Enable all linters"))
fs.BoolVar(&cfg.Fast, "fast", false, wh("Enable only fast linters from enabled linters set (first run won't be fast)"))
fs.StringSliceVarP(&cfg.Presets, "presets", "p", nil,
wh(fmt.Sprintf("Enable presets (%s) of linters. Run 'golangci-lint help linters' to see "+
"them. This option implies option --disable-all", strings.Join(e.DBManager.AllPresets(), "|"))))
}

// executeLinters runs the 'linters' CLI command, which displays the supported linters.
func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error {
enabledLintersMap, err := e.EnabledLintersSet.GetEnabledLintersMap()
Expand All @@ -46,14 +63,9 @@ func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error {
}
}

enabledBy := "your configuration"
if e.cfg.Run.NoConfig {
enabledBy = "default"
}

color.Green("Enabled by %v linters:\n", enabledBy)
color.Green("Enabled by your configuration linters:\n")
printLinterConfigs(enabledLinters)
color.Red("\nDisabled by %v linters:\n", enabledBy)
color.Red("\nDisabled by your configuration linters:\n")
printLinterConfigs(disabledLCs)

return nil
Expand Down
72 changes: 28 additions & 44 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,15 @@ import (

const defaultFileMode = 0644

const defaultTimeout = time.Minute

const (
// envFailOnWarnings value: "1"
envFailOnWarnings = "FAIL_ON_WARNINGS"
// envMemLogEvery value: "1"
envMemLogEvery = "GL_MEM_LOG_EVERY"
)

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", ep.ID, ep.Linter, ep.Why),
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
"",
)
}
return strings.Join(parts, "\n")
}

func getDefaultDirectoryExcludeHelp() string {
parts := []string{color.GreenString("Use or not use default excluded directories:")}
for _, dir := range packages.StdExcludeDirRegexps {
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(dir)))
}
parts = append(parts, "")
return strings.Join(parts, "\n")
}

func wh(text string) string {
return color.GreenString(text)
}

const defaultTimeout = time.Minute

func (e *Executor) initConfigFileFlagSet(fs *pflag.FlagSet, cfg *config.Run) {
fs.StringVarP(&cfg.Config, "config", "c", "", wh("Read config from file path `PATH`"))
fs.BoolVar(&cfg.NoConfig, "no-config", false, wh("Don't read config file"))
}

func (e *Executor) initLintersFlagSet(fs *pflag.FlagSet, cfg *config.Linters) {
fs.StringSliceVarP(&cfg.Disable, "disable", "D", nil, wh("Disable specific linter"))
fs.BoolVar(&cfg.DisableAll, "disable-all", false, wh("Disable all linters"))
fs.StringSliceVarP(&cfg.Enable, "enable", "E", nil, wh("Enable specific linter"))
fs.BoolVar(&cfg.EnableAll, "enable-all", false, wh("Enable all linters"))
fs.BoolVar(&cfg.Fast, "fast", false, wh("Enable only fast linters from enabled linters set (first run won't be fast)"))
fs.StringSliceVarP(&cfg.Presets, "presets", "p", nil,
wh(fmt.Sprintf("Enable presets (%s) of linters. Run 'golangci-lint help linters' to see "+
"them. This option implies option --disable-all", strings.Join(e.DBManager.AllPresets(), "|"))))
}

//nolint:funlen,gomnd
func (e *Executor) initFlagSet(fs *pflag.FlagSet, cfg *config.Config, isFinalInit bool) {
hideFlag := func(name string) {
Expand All @@ -97,7 +56,7 @@ func (e *Executor) initFlagSet(fs *pflag.FlagSet, cfg *config.Config, isFinalIni

// Config file config
rc := &cfg.Run
e.initConfigFileFlagSet(fs, rc)
initConfigFileFlagSet(fs, rc)

// Output config
oc := &cfg.Output
Expand Down Expand Up @@ -648,3 +607,28 @@ func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log
logger.Infof("Execution took %s", time.Since(startedAt))
close(done)
}

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", ep.ID, ep.Linter, ep.Why),
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
"",
)
}
return strings.Join(parts, "\n")
}

func getDefaultDirectoryExcludeHelp() string {
parts := []string{color.GreenString("Use or not use default excluded directories:")}
for _, dir := range packages.StdExcludeDirRegexps {
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(dir)))
}
parts = append(parts, "")
return strings.Join(parts, "\n")
}

func wh(text string) string {
return color.GreenString(text)
}
2 changes: 1 addition & 1 deletion test/testshared/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (b *RunnerBuilder) Runner() *Runner {
b.tb.Fatal("--no-config and -c cannot be used at the same time")
}

arguments := []string{}
var arguments []string

if b.command == "run" {
arguments = append(arguments, "--internal-cmd-test")
Expand Down
1 change: 0 additions & 1 deletion test/testshared/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestRunnerBuilder_Runner(t *testing.T) {
expected: &Runner{
env: []string(nil),
command: "example",
args: []string{},
},
},
{
Expand Down

0 comments on commit 3bb4f79

Please sign in to comment.