forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinters.go
136 lines (102 loc) · 3.12 KB
/
linters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package commands
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goformatters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type lintersHelp struct {
Enabled []linterHelp
Disabled []linterHelp
}
type lintersOptions struct {
config.LoaderOptions
JSON bool
}
type lintersCommand struct {
viper *viper.Viper
cmd *cobra.Command
opts lintersOptions
cfg *config.Config
log logutils.Log
dbManager *lintersdb.Manager
}
func newLintersCommand(logger logutils.Log) *lintersCommand {
c := &lintersCommand{
viper: viper.New(),
cfg: config.NewDefault(),
log: logger,
}
lintersCmd := &cobra.Command{
Use: "linters",
Short: "List current linters configuration",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.execute,
PreRunE: c.preRunE,
SilenceUsage: true,
}
fs := lintersCmd.Flags()
fs.SortFlags = false // sort them as they are defined here
setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)
setupLintersFlagSet(c.viper, fs)
fs.BoolVar(&c.opts.JSON, "json", false, color.GreenString("Display as JSON"))
c.cmd = lintersCmd
return c
}
func (c *lintersCommand) preRunE(cmd *cobra.Command, args []string) error {
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)
err := loader.Load(config.LoadOptions{Validation: true})
if err != nil {
return fmt.Errorf("can't load config: %w", err)
}
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
if err != nil {
return err
}
c.dbManager = dbManager
return nil
}
func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
enabledLintersMap, err := c.dbManager.GetEnabledLintersMap()
if err != nil {
return fmt.Errorf("can't get enabled linters: %w", err)
}
var enabledLinters []*linter.Config
var disabledLinters []*linter.Config
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
if lc.Internal {
continue
}
if goformatters.IsFormatter(lc.Name()) {
continue
}
if enabledLintersMap[lc.Name()] == nil {
disabledLinters = append(disabledLinters, lc)
} else {
enabledLinters = append(enabledLinters, lc)
}
}
if c.opts.JSON {
formatters := lintersHelp{}
for _, lc := range enabledLinters {
formatters.Enabled = append(formatters.Enabled, newLinterHelp(lc))
}
for _, lc := range disabledLinters {
formatters.Disabled = append(formatters.Disabled, newLinterHelp(lc))
}
return json.NewEncoder(c.cmd.OutOrStdout()).Encode(formatters)
}
color.Green("Enabled by your configuration linters:\n")
printLinters(enabledLinters)
color.Red("\nDisabled by your configuration linters:\n")
printLinters(disabledLinters)
return nil
}