github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/commands/config.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/spf13/cobra" 8 "github.com/spf13/viper" 9 10 "github.com/elek/golangci-lint/pkg/exitcodes" 11 "github.com/elek/golangci-lint/pkg/fsutils" 12 ) 13 14 func (e *Executor) initConfig() { 15 cmd := &cobra.Command{ 16 Use: "config", 17 Short: "Config", 18 Run: func(cmd *cobra.Command, args []string) { 19 if len(args) != 0 { 20 e.log.Fatalf("Usage: golangci-lint config") 21 } 22 if err := cmd.Help(); err != nil { 23 e.log.Fatalf("Can't run help: %s", err) 24 } 25 }, 26 } 27 e.rootCmd.AddCommand(cmd) 28 29 pathCmd := &cobra.Command{ 30 Use: "path", 31 Short: "Print used config path", 32 Run: e.executePathCmd, 33 } 34 e.initRunConfiguration(pathCmd) // allow --config 35 cmd.AddCommand(pathCmd) 36 } 37 38 func (e *Executor) getUsedConfig() string { 39 usedConfigFile := viper.ConfigFileUsed() 40 if usedConfigFile == "" { 41 return "" 42 } 43 44 prettyUsedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "") 45 if err != nil { 46 e.log.Warnf("Can't pretty print config file path: %s", err) 47 return usedConfigFile 48 } 49 50 return prettyUsedConfigFile 51 } 52 53 func (e *Executor) executePathCmd(_ *cobra.Command, args []string) { 54 if len(args) != 0 { 55 e.log.Fatalf("Usage: golangci-lint config path") 56 } 57 58 usedConfigFile := e.getUsedConfig() 59 if usedConfigFile == "" { 60 e.log.Warnf("No config file detected") 61 os.Exit(exitcodes.NoConfigFileDetected) 62 } 63 64 fmt.Println(usedConfigFile) 65 os.Exit(0) 66 }