golangci-lint/pkg/commands/config.go

127 lines
3.3 KiB
Go
Raw Normal View History

package commands
import (
"fmt"
"os"
2024-03-19 21:35:21 +01:00
"github.com/fatih/color"
2019-09-27 20:58:49 -04:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
2024-02-27 00:03:48 +01:00
"github.com/golangci/golangci-lint/pkg/logutils"
)
2024-02-27 00:03:48 +01:00
type configCommand struct {
viper *viper.Viper
cmd *cobra.Command
2024-03-19 21:35:21 +01:00
opts config.LoaderOptions
verifyOpts verifyOptions
buildInfo BuildInfo
2024-02-27 00:03:48 +01:00
log logutils.Log
}
2024-03-19 21:35:21 +01:00
func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
2024-02-27 00:03:48 +01:00
c := &configCommand{
2024-03-19 21:35:21 +01:00
viper: viper.New(),
log: log,
buildInfo: info,
2024-02-27 00:03:48 +01:00
}
2024-02-23 20:38:42 +01:00
configCmd := &cobra.Command{
Use: "config",
Short: "Config file information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
2024-03-19 21:35:21 +01:00
PersistentPreRunE: c.preRunE,
}
verifyCommand := &cobra.Command{
Use: "verify",
Short: "Verify configuration against JSON schema",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.executeVerify,
}
2024-02-27 00:03:48 +01:00
configCmd.AddCommand(
&cobra.Command{
Use: "path",
Short: "Print used config path",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
2024-03-19 21:35:21 +01:00
Run: c.executePath,
2024-02-27 00:03:48 +01:00
},
2024-03-19 21:35:21 +01:00
verifyCommand,
2024-02-27 00:03:48 +01:00
)
2024-03-19 21:35:21 +01:00
flagSet := configCmd.PersistentFlags()
flagSet.SortFlags = false // sort them as they are defined here
setupConfigFileFlagSet(flagSet, &c.opts)
// ex: --schema jsonschema/golangci.next.jsonschema.json
verifyFlagSet := verifyCommand.Flags()
verifyFlagSet.StringVar(&c.verifyOpts.schemaURL, "schema", "", color.GreenString("JSON schema URL"))
_ = verifyFlagSet.MarkHidden("schema")
2024-02-27 00:03:48 +01:00
c.cmd = configCmd
2024-02-27 00:03:48 +01:00
return c
2024-02-23 20:38:42 +01:00
}
2024-02-27 00:03:48 +01:00
func (c *configCommand) preRunE(cmd *cobra.Command, _ []string) error {
// The command doesn't depend on the real configuration.
// It only needs to know the path of the configuration file.
2024-03-19 21:35:21 +01:00
cfg := config.NewDefault()
// 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, file path and file 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.
cfg.Run.UseDefaultSkipDirs = true
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts, cfg)
2024-02-27 00:03:48 +01:00
if err := loader.Load(); err != nil {
return fmt.Errorf("can't load config: %w", err)
}
return nil
}
2024-03-19 21:35:21 +01:00
func (c *configCommand) executePath(cmd *cobra.Command, _ []string) {
2024-02-27 00:03:48 +01:00
usedConfigFile := c.getUsedConfig()
2024-02-23 20:38:42 +01:00
if usedConfigFile == "" {
2024-02-27 00:03:48 +01:00
c.log.Warnf("No config file detected")
2024-02-23 20:38:42 +01:00
os.Exit(exitcodes.NoConfigFileDetected)
}
2024-03-19 21:35:21 +01:00
cmd.Println(usedConfigFile)
}
2024-02-23 20:38:42 +01:00
// getUsedConfig returns the resolved path to the golangci config file,
// or the empty string if no configuration could be found.
2024-02-27 00:03:48 +01:00
func (c *configCommand) getUsedConfig() string {
usedConfigFile := c.viper.ConfigFileUsed()
if usedConfigFile == "" {
return ""
}
prettyUsedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "")
if err != nil {
2024-02-27 00:03:48 +01:00
c.log.Warnf("Can't pretty print config file path: %s", err)
return usedConfigFile
}
return prettyUsedConfigFile
}