-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
linters.go
113 lines (86 loc) · 2.84 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
package commands
import (
"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/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type lintersOptions struct {
config.LoaderOptions
}
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)
c.cmd = lintersCmd
return c
}
func (c *lintersCommand) preRunE(cmd *cobra.Command, _ []string) error {
// Hack to hide deprecation messages related to `--skip-dirs-use-default`:
// Flags are not bound then the default values, defined only through flags, are not applied.
// In this command, linters information are the only requirements, i.e. it don't need flag values.
//
// TODO(ldez) add an option (check deprecation) to `Loader.Load()` but this require a dedicated PR.
c.cfg.Run.UseDefaultSkipDirs = true
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg)
if err := loader.Load(); 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 disabledLCs []*linter.Config
for _, lc := range c.dbManager.GetAllSupportedLinterConfigs() {
if lc.Internal {
continue
}
if enabledLintersMap[lc.Name()] == nil {
disabledLCs = append(disabledLCs, lc)
} else {
enabledLinters = append(enabledLinters, lc)
}
}
color.Green("Enabled by your configuration linters:\n")
printLinters(enabledLinters)
color.Red("\nDisabled by your configuration linters:\n")
printLinters(disabledLCs)
return nil
}