From 069e66aa91eb1f1788941d0f1722ab515c41f7a4 Mon Sep 17 00:00:00 2001 From: Denis Isaev Date: Sun, 29 Jul 2018 19:33:24 +0300 Subject: [PATCH] Closes #75: golangci-lint linters now prints current linters configuration --- README.md | 11 ++++-- README.tmpl.md | 11 ++++-- pkg/commands/executor.go | 1 + pkg/commands/help.go | 68 ++++++++++++++++++++++++++++++++++++++ pkg/commands/linters.go | 48 ++++++++++++--------------- pkg/commands/run.go | 26 +++++++++------ scripts/gen_readme/main.go | 2 +- 7 files changed, 123 insertions(+), 44 deletions(-) create mode 100644 pkg/commands/help.go diff --git a/README.md b/README.md index a82d08d9..e9b6ca35 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/. GolangCI-Lint can be used with zero configuration. By default the following linters are enabled: ``` -$ golangci-lint linters +$ golangci-lint help linters Enabled by default linters: govet: Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false] errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false] @@ -109,7 +109,7 @@ typecheck: Like the front-end of a Go compiler, parses and type-checks Go code [ and the following linters are disabled by default: ``` -$ golangci-lint linters +$ golangci-lint help linters ... Disabled by default linters: golint: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: true] @@ -282,7 +282,7 @@ The following great projects use golangci-lint: # Supported Linters To see a list of supported linters and which linters are enabled/disabled: ``` -golangci-lint linters +golangci-lint help linters ``` ## Enabled By Default Linters @@ -321,6 +321,11 @@ The config file has lower priority than command-line options. If the same bool/s and in the config file, the option from command-line will be used. Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file. +To see a list of enabled by your configuration linters: +``` +golangci-lint linters +``` + ## Command-Line Options ``` golangci-lint run -h diff --git a/README.tmpl.md b/README.tmpl.md index a221d64d..b930799b 100644 --- a/README.tmpl.md +++ b/README.tmpl.md @@ -92,13 +92,13 @@ Directories are NOT analyzed recursively. To analyze them recursively append `/. GolangCI-Lint can be used with zero configuration. By default the following linters are enabled: ``` -$ golangci-lint linters +$ golangci-lint help linters {{.LintersCommandOutputEnabledOnly}} ``` and the following linters are disabled by default: ``` -$ golangci-lint linters +$ golangci-lint help linters ... {{.LintersCommandOutputDisabledOnly}} ``` @@ -255,7 +255,7 @@ The following great projects use golangci-lint: # Supported Linters To see a list of supported linters and which linters are enabled/disabled: ``` -golangci-lint linters +golangci-lint help linters ``` ## Enabled By Default Linters @@ -269,6 +269,11 @@ The config file has lower priority than command-line options. If the same bool/s and in the config file, the option from command-line will be used. Slice options (e.g. list of enabled/disabled linters) are combined from the command-line and config file. +To see a list of enabled by your configuration linters: +``` +golangci-lint linters +``` + ## Command-Line Options ``` golangci-lint run -h diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index a424336d..b54aad26 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -33,6 +33,7 @@ func NewExecutor(version, commit, date string) *Executor { e.initRoot() e.initRun() + e.initHelp() e.initLinters() return e diff --git a/pkg/commands/help.go b/pkg/commands/help.go new file mode 100644 index 00000000..61357748 --- /dev/null +++ b/pkg/commands/help.go @@ -0,0 +1,68 @@ +package commands + +import ( + "fmt" + "os" + "strings" + + "github.com/fatih/color" + "github.com/golangci/golangci-lint/pkg/lint/linter" + "github.com/golangci/golangci-lint/pkg/lint/lintersdb" + "github.com/golangci/golangci-lint/pkg/logutils" + "github.com/spf13/cobra" +) + +func (e *Executor) initHelp() { + helpCmd := &cobra.Command{ + Use: "help", + Short: "Help", + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + e.log.Fatalf("Can't run help: %s", err) + } + }, + } + e.rootCmd.AddCommand(helpCmd) + + lintersHelpCmd := &cobra.Command{ + Use: "linters", + Short: "Help about linters", + Run: e.executeLintersHelp, + } + helpCmd.AddCommand(lintersHelpCmd) +} + +func printLinterConfigs(lcs []linter.Config) { + for _, lc := range lcs { + fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()), + lc.Linter.Desc(), !lc.DoesFullImport) + } +} + +func (e Executor) executeLintersHelp(cmd *cobra.Command, args []string) { + var enabledLCs, disabledLCs []linter.Config + for _, lc := range lintersdb.GetAllSupportedLinterConfigs() { + if lc.EnabledByDefault { + enabledLCs = append(enabledLCs, lc) + } else { + disabledLCs = append(disabledLCs, lc) + } + } + + color.Green("Enabled by default linters:\n") + printLinterConfigs(enabledLCs) + color.Red("\nDisabled by default linters:\n") + printLinterConfigs(disabledLCs) + + color.Green("\nLinters presets:") + for _, p := range lintersdb.AllPresets() { + linters := lintersdb.GetAllLinterConfigsForPreset(p) + linterNames := []string{} + for _, lc := range linters { + linterNames = append(linterNames, lc.Linter.Name()) + } + fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", ")) + } + + os.Exit(0) +} diff --git a/pkg/commands/linters.go b/pkg/commands/linters.go index f63b2553..f559d973 100644 --- a/pkg/commands/linters.go +++ b/pkg/commands/linters.go @@ -1,57 +1,53 @@ package commands import ( - "fmt" + "log" "os" - "strings" "github.com/fatih/color" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/lintersdb" - "github.com/golangci/golangci-lint/pkg/logutils" "github.com/spf13/cobra" ) func (e *Executor) initLinters() { - var lintersCmd = &cobra.Command{ + lintersCmd := &cobra.Command{ Use: "linters", - Short: "List linters", + Short: "List current linters configuration", Run: e.executeLinters, } e.rootCmd.AddCommand(lintersCmd) + e.initRunConfiguration(lintersCmd) } -func printLinterConfigs(lcs []linter.Config) { - for _, lc := range lcs { - fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()), - lc.Linter.Desc(), !lc.DoesFullImport) +func IsLinterInConfigsList(name string, linters []linter.Config) bool { + for _, linter := range linters { + if linter.Linter.Name() == name { + return true + } } + + return false } func (e Executor) executeLinters(cmd *cobra.Command, args []string) { - var enabledLCs, disabledLCs []linter.Config + enabledLCs, err := lintersdb.GetEnabledLinters(e.cfg, e.log.Child("lintersdb")) + if err != nil { + log.Fatalf("Can't get enabled linters: %s", err) + } + + color.Green("Enabled by your configuration linters:\n") + printLinterConfigs(enabledLCs) + + var disabledLCs []linter.Config for _, lc := range lintersdb.GetAllSupportedLinterConfigs() { - if lc.EnabledByDefault { - enabledLCs = append(enabledLCs, lc) - } else { + if !IsLinterInConfigsList(lc.Linter.Name(), enabledLCs) { disabledLCs = append(disabledLCs, lc) } } - color.Green("Enabled by default linters:\n") - printLinterConfigs(enabledLCs) - color.Red("\nDisabled by default linters:\n") + color.Red("\nDisabled by your configuration linters:\n") printLinterConfigs(disabledLCs) - color.Green("\nLinters presets:") - for _, p := range lintersdb.AllPresets() { - linters := lintersdb.GetAllLinterConfigsForPreset(p) - linterNames := []string{} - for _, lc := range linters { - linterNames = append(linterNames, lc.Linter.Name()) - } - fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", ")) - } - os.Exit(0) } diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 211776dc..5d6d4233 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -161,17 +161,8 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) { } -func (e *Executor) initRun() { - var runCmd = &cobra.Command{ - Use: "run", - Short: welcomeMessage, - Run: e.executeRun, - } - e.rootCmd.AddCommand(runCmd) - - runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals - - fs := runCmd.Flags() +func (e *Executor) initRunConfiguration(cmd *cobra.Command) { + fs := cmd.Flags() fs.SortFlags = false // sort them as they are defined here initFlagSet(fs, e.cfg) @@ -199,6 +190,19 @@ func (e *Executor) initRun() { fixSlicesFlags(fs) } +func (e *Executor) initRun() { + var runCmd = &cobra.Command{ + Use: "run", + Short: welcomeMessage, + Run: e.executeRun, + } + e.rootCmd.AddCommand(runCmd) + + runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals + + e.initRunConfiguration(runCmd) +} + func fixSlicesFlags(fs *pflag.FlagSet) { // It's a dirty hack to set flag.Changed to true for every string slice flag. // It's necessary to merge config and command-line slices: otherwise command-line diff --git a/scripts/gen_readme/main.go b/scripts/gen_readme/main.go index 32586700..a11e1e3d 100644 --- a/scripts/gen_readme/main.go +++ b/scripts/gen_readme/main.go @@ -57,7 +57,7 @@ func buildTemplateContext() (map[string]interface{}, error) { return nil, fmt.Errorf("can't run go install: %s", err) } - lintersOut, err := exec.Command("golangci-lint", "linters").Output() + lintersOut, err := exec.Command("golangci-lint", "help", "linters").Output() if err != nil { return nil, fmt.Errorf("can't run linters cmd: %s", err) }