Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
issue #85: extend linters command
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 3, 2023
1 parent 5aaf5a7 commit 6c9aa19
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
47 changes: 41 additions & 6 deletions pkg/commands/help.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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, ", "))
}
}
13 changes: 13 additions & 0 deletions pkg/commands/helper.go
Original file line number Diff line number Diff line change
@@ -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...)
}
17 changes: 8 additions & 9 deletions pkg/commands/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"

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

"github.com/golangci/golangci-lint/pkg/lint/linter"
Expand All @@ -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() {
Expand All @@ -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
}
1 change: 1 addition & 0 deletions pkg/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

const (
OutFormatJSON = "json"
OutFormatYAML = "yaml"
OutFormatLineNumber = "line-number"
OutFormatColoredLineNumber = "colored-line-number"
OutFormatTab = "tab"
Expand Down
26 changes: 14 additions & 12 deletions pkg/lint/linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6c9aa19

Please sign in to comment.