add command 'golangci-lint config path'
This commit is contained in:
parent
cb5d1da986
commit
27752e81a7
51
pkg/commands/config.go
Normal file
51
pkg/commands/config.go
Normal 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)
|
||||||
|
}
|
@ -55,6 +55,7 @@ func NewExecutor(version, commit, date string) *Executor {
|
|||||||
e.initRun()
|
e.initRun()
|
||||||
e.initHelp()
|
e.initHelp()
|
||||||
e.initLinters()
|
e.initLinters()
|
||||||
|
e.initConfig()
|
||||||
|
|
||||||
// init e.cfg by values from config: flags parse will see these values
|
// 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
|
// like the default ones. It will overwrite them only if the same option
|
||||||
|
@ -22,7 +22,7 @@ func (e *Executor) initHelp() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
e.rootCmd.AddCommand(helpCmd)
|
e.rootCmd.SetHelpCommand(helpCmd)
|
||||||
|
|
||||||
lintersHelpCmd := &cobra.Command{
|
lintersHelpCmd := &cobra.Command{
|
||||||
Use: "linters",
|
Use: "linters",
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package exitcodes
|
package exitcodes
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Success = 0
|
Success = 0
|
||||||
IssuesFound = 1
|
IssuesFound = 1
|
||||||
WarningInTest = 2
|
WarningInTest = 2
|
||||||
Failure = 3
|
Failure = 3
|
||||||
Timeout = 4
|
Timeout = 4
|
||||||
NoGoFiles = 5
|
NoGoFiles = 5
|
||||||
|
NoConfigFileDetected = 6
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExitError struct {
|
type ExitError struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user