diff --git a/pkg/commands/config.go b/pkg/commands/config.go new file mode 100644 index 00000000..7e4915dd --- /dev/null +++ b/pkg/commands/config.go @@ -0,0 +1,51 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/spf13/viper" + + "github.com/golangci/golangci-lint/pkg/exitcodes" + "github.com/golangci/golangci-lint/pkg/fsutils" + + "github.com/spf13/cobra" +) + +func (e *Executor) initConfig() { + cmd := &cobra.Command{ + Use: "config", + Short: "Config", + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + e.log.Fatalf("Can't run help: %s", err) + } + }, + } + e.rootCmd.AddCommand(cmd) + + pathCmd := &cobra.Command{ + Use: "path", + Short: "Print used config path", + Run: e.executePathCmd, + } + e.initRunConfiguration(pathCmd) // allow --config + cmd.AddCommand(pathCmd) + +} + +func (e Executor) executePathCmd(cmd *cobra.Command, args []string) { + usedConfigFile := viper.ConfigFileUsed() + if usedConfigFile == "" { + e.log.Warnf("No config file detected") + os.Exit(exitcodes.NoConfigFileDetected) + } + + usedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "") + if err != nil { + e.log.Warnf("Can't pretty print config file path: %s", err) + } + + fmt.Println(usedConfigFile) + os.Exit(0) +} diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index f6498467..4da64e3b 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -55,6 +55,7 @@ func NewExecutor(version, commit, date string) *Executor { e.initRun() e.initHelp() e.initLinters() + e.initConfig() // init e.cfg by values from config: flags parse will see these values // like the default ones. It will overwrite them only if the same option diff --git a/pkg/commands/help.go b/pkg/commands/help.go index c0884a7a..9fb2a877 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -22,7 +22,7 @@ func (e *Executor) initHelp() { } }, } - e.rootCmd.AddCommand(helpCmd) + e.rootCmd.SetHelpCommand(helpCmd) lintersHelpCmd := &cobra.Command{ Use: "linters", diff --git a/pkg/exitcodes/exitcodes.go b/pkg/exitcodes/exitcodes.go index 62cb12b9..82bd2cdc 100644 --- a/pkg/exitcodes/exitcodes.go +++ b/pkg/exitcodes/exitcodes.go @@ -1,12 +1,13 @@ package exitcodes const ( - Success = 0 - IssuesFound = 1 - WarningInTest = 2 - Failure = 3 - Timeout = 4 - NoGoFiles = 5 + Success = 0 + IssuesFound = 1 + WarningInTest = 2 + Failure = 3 + Timeout = 4 + NoGoFiles = 5 + NoConfigFileDetected = 6 ) type ExitError struct {