golangci-lint/pkg/commands/linters.go

108 lines
2.5 KiB
Go
Raw Permalink Normal View History

package commands
import (
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
2024-02-27 00:03:48 +01:00
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/lint/linter"
2024-02-23 20:38:42 +01:00
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
2024-02-27 00:03:48 +01:00
"github.com/golangci/golangci-lint/pkg/logutils"
)
2024-02-27 00:03:48 +01:00
type lintersOptions struct {
config.LoaderOptions
}
type lintersCommand struct {
viper *viper.Viper
cmd *cobra.Command
opts lintersOptions
cfg *config.Config
log logutils.Log
2024-03-02 21:43:28 +01:00
dbManager *lintersdb.Manager
2024-02-27 00:03:48 +01:00
}
func newLintersCommand(logger logutils.Log) *lintersCommand {
2024-02-27 00:03:48 +01:00
c := &lintersCommand{
viper: viper.New(),
cfg: config.NewDefault(),
2024-02-27 00:03:48 +01:00
log: logger,
}
2024-02-23 20:38:42 +01:00
lintersCmd := &cobra.Command{
Use: "linters",
Short: "List current linters configuration",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
2024-02-27 00:03:48 +01:00
RunE: c.execute,
PreRunE: c.preRunE,
SilenceUsage: true,
}
2024-02-23 20:38:42 +01:00
fs := lintersCmd.Flags()
fs.SortFlags = false // sort them as they are defined here
2024-02-27 00:03:48 +01:00
setupConfigFileFlagSet(fs, &c.opts.LoaderOptions)
setupLintersFlagSet(c.viper, fs)
2024-02-27 00:03:48 +01:00
c.cmd = lintersCmd
2024-02-27 00:03:48 +01:00
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)
2024-02-27 00:03:48 +01:00
err := loader.Load(config.LoadOptions{Validation: true})
if err != nil {
2024-02-27 00:03:48 +01:00
return fmt.Errorf("can't load config: %w", err)
}
2024-03-02 21:43:28 +01:00
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
lintersdb.NewLinterBuilder(), lintersdb.NewPluginModuleBuilder(c.log), lintersdb.NewPluginGoBuilder(c.log))
2024-03-02 21:43:28 +01:00
if err != nil {
return err
}
c.dbManager = dbManager
2024-02-27 00:03:48 +01:00
return nil
}
func (c *lintersCommand) execute(_ *cobra.Command, _ []string) error {
2024-03-02 21:43:28 +01:00
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
2024-02-27 00:03:48 +01:00
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")
2024-02-27 00:03:48 +01:00
printLinters(enabledLinters)
color.Red("\nDisabled by your configuration linters:\n")
2024-02-27 00:03:48 +01:00
printLinters(disabledLCs)
return nil
}