add command 'golangci-lint config path'

This commit is contained in:
Denis Isaev 2018-11-18 11:18:26 +03:00 committed by Isaev Denis
parent cb5d1da986
commit 27752e81a7
4 changed files with 60 additions and 7 deletions

51
pkg/commands/config.go Normal file
View File

@ -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)
}

View File

@ -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

View File

@ -22,7 +22,7 @@ func (e *Executor) initHelp() {
}
},
}
e.rootCmd.AddCommand(helpCmd)
e.rootCmd.SetHelpCommand(helpCmd)
lintersHelpCmd := &cobra.Command{
Use: "linters",

View File

@ -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 {