From 6c9aa196190fec2cf1d819332804656537257124 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Wed, 22 Mar 2023 21:19:14 +0300 Subject: [PATCH] issue #85: extend linters command --- pkg/commands/help.go | 47 ++++++++++++++++++++++++++++++++++----- pkg/commands/helper.go | 13 +++++++++++ pkg/commands/linters.go | 17 +++++++------- pkg/config/output.go | 1 + pkg/lint/linter/config.go | 26 ++++++++++++---------- 5 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 pkg/commands/helper.go diff --git a/pkg/commands/help.go b/pkg/commands/help.go index a06d508f27c2..0c2261c30b28 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -1,13 +1,16 @@ package commands import ( + "encoding/json" "fmt" "sort" "strings" "github.com/fatih/color" "github.com/spf13/cobra" + "gopkg.in/yaml.v3" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/logutils" ) @@ -33,10 +36,39 @@ func (e *Executor) initHelp() { helpCmd.AddCommand(lintersHelpCmd) } -func printLinterConfigs(lcs []*linter.Config) { +func printLinterConfigs(lcs []*linter.Config, format ...string) { sort.Slice(lcs, func(i, j int) bool { return lcs[i].Name() < lcs[j].Name() }) + + var f string + if len(format) > 0 { + f = format[0] + + type l struct { + Name string `json:"name" yaml:"name"` + Desc string `json:"desc" yaml:"desc"` + *linter.Config `json:",inline" yaml:",inline"` + } + converted := make([]l, 0, len(lcs)) + for _, lc := range lcs { + converted = append(converted, l{ + Name: lc.Name(), + Desc: lc.Linter.Desc(), + Config: lc, + }) + } + + switch f { + case config.OutFormatJSON: + _ = json.NewEncoder(logutils.StdOut).Encode(converted) + return + case config.OutFormatYAML: + _ = yaml.NewEncoder(logutils.StdOut).Encode(converted) + return + } + } + for _, lc := range lcs { altNamesStr := "" if len(lc.AlternativeNames) != 0 { @@ -55,7 +87,8 @@ func printLinterConfigs(lcs []*linter.Config) { deprecatedMark = " [" + color.RedString("deprecated") + "]" } - fmt.Fprintf(logutils.StdOut, "%s%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()), + _, _ = fmt.Fprintf(logutils.StdOut, "%s%s%s: %s [fast: %t, auto-fix: %t]\n", + color.YellowString(lc.Name()), altNamesStr, deprecatedMark, linterDescription, !lc.IsSlowLinter(), lc.CanAutoFix) } } @@ -74,12 +107,13 @@ func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) { } } - color.Green("Enabled by default linters:\n") + green("Enabled by default linters:\n") printLinterConfigs(enabledLCs) - color.Red("\nDisabled by default linters:\n") + + red("\nDisabled by default linters:\n") printLinterConfigs(disabledLCs) - color.Green("\nLinters presets:") + green("\nLinters presets:") for _, p := range e.DBManager.AllPresets() { linters := e.DBManager.GetAllLinterConfigsForPreset(p) var linterNames []string @@ -91,6 +125,7 @@ func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) { linterNames = append(linterNames, lc.Name()) } sort.Strings(linterNames) - fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", ")) + _, _ = fmt.Fprintf(logutils.StdOut, "%s: %s\n", + color.YellowString(p), strings.Join(linterNames, ", ")) } } diff --git a/pkg/commands/helper.go b/pkg/commands/helper.go new file mode 100644 index 000000000000..927d1a84642c --- /dev/null +++ b/pkg/commands/helper.go @@ -0,0 +1,13 @@ +package commands + +import "github.com/fatih/color" + +// bad api and abstraction + +func green(format string, args ...interface{}) { + _, _ = color.New(color.FgGreen).Fprintf(color.Error, format, args...) +} + +func red(format string, args ...interface{}) { + _, _ = color.New(color.FgRed).Fprintf(color.Error, format, args...) +} diff --git a/pkg/commands/linters.go b/pkg/commands/linters.go index 292713ec9002..847714c09f48 100644 --- a/pkg/commands/linters.go +++ b/pkg/commands/linters.go @@ -3,7 +3,6 @@ package commands import ( "fmt" - "github.com/fatih/color" "github.com/spf13/cobra" "github.com/golangci/golangci-lint/pkg/lint/linter" @@ -28,16 +27,16 @@ func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error { return fmt.Errorf("can't get enabled linters: %w", err) } - color.Green("Enabled by your configuration linters:\n") - var enabledLinters []*linter.Config - for _, lc := range enabledLintersMap { - if lc.Internal { + green("Enabled by your configuration linters:\n") + enabledLinters := make([]*linter.Config, 0, len(enabledLintersMap)) + for _, l := range enabledLintersMap { + if l.Internal { continue } - enabledLinters = append(enabledLinters, lc) + enabledLinters = append(enabledLinters, l) } - printLinterConfigs(enabledLinters) + printLinterConfigs(enabledLinters, e.cfg.Output.Format) var disabledLCs []*linter.Config for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { @@ -50,8 +49,8 @@ func (e *Executor) executeLinters(_ *cobra.Command, _ []string) error { } } - color.Red("\nDisabled by your configuration linters:\n") - printLinterConfigs(disabledLCs) + red("\nDisabled by your configuration linters:\n") + printLinterConfigs(disabledLCs, e.cfg.Output.Format) return nil } diff --git a/pkg/config/output.go b/pkg/config/output.go index e8726392055d..bef3472ff621 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -2,6 +2,7 @@ package config const ( OutFormatJSON = "json" + OutFormatYAML = "yaml" OutFormatLineNumber = "line-number" OutFormatColoredLineNumber = "colored-line-number" OutFormatTab = "tab" diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index c911b5613daf..572a3b69ef56 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -33,22 +33,24 @@ type Deprecation struct { } type Config struct { - Linter Linter - EnabledByDefault bool + Linter Linter `json:"-" yaml:"-"` + EnabledByDefault bool `json:"default" yaml:"default"` - LoadMode packages.LoadMode + LoadMode packages.LoadMode `json:"-" yaml:"-"` - InPresets []string - AlternativeNames []string + InPresets []string `json:"presets,omitempty" yaml:"presets,omitempty"` + AlternativeNames []string `json:"alternative,omitempty" yaml:"alternative,omitempty"` - OriginalURL string // URL of original (not forked) repo, needed for autogenerated README - Internal bool // Internal linters cannot be disabled (ex: typecheck). - CanAutoFix bool - IsSlow bool - DoesChangeTypes bool + // URL of original (not forked) repo, needed for autogenerated README + OriginalURL string `json:"origin" yaml:"origin"` + // Internal linters cannot be disabled (ex: typecheck). + Internal bool `json:"internal" yaml:"internal"` + CanAutoFix bool `json:"auto-fix" yaml:"auto-fix"` + IsSlow bool `json:"slow" yaml:"slow"` + DoesChangeTypes bool `json:"change-types" yaml:"change-types"` - Since string - Deprecation *Deprecation + Since string `json:"since,omitempty" yaml:"since,omitempty"` + Deprecation *Deprecation `json:"deprecation,omitempty" yaml:"deprecation,omitempty"` } func (lc *Config) WithEnabledByDefault() *Config {