github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/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/chenfeining/golangci-lint/pkg/exitcodes" 11 "github.com/chenfeining/golangci-lint/pkg/fsutils" 12 ) 13 14 func (e *Executor) initConfig() { 15 cmd := &cobra.Command{ 16 Use: "config", 17 Short: "Config", 18 Args: cobra.NoArgs, 19 RunE: func(cmd *cobra.Command, _ []string) error { 20 return cmd.Help() 21 }, 22 } 23 e.rootCmd.AddCommand(cmd) 24 25 pathCmd := &cobra.Command{ 26 Use: "path", 27 Short: "Print used config path", 28 Args: cobra.NoArgs, 29 ValidArgsFunction: cobra.NoFileCompletions, 30 Run: e.executePathCmd, 31 } 32 e.initRunConfiguration(pathCmd) // allow --config 33 cmd.AddCommand(pathCmd) 34 } 35 36 // getUsedConfig returns the resolved path to the golangci config file, or the empty string 37 // if no configuration could be found. 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, _ []string) { 54 usedConfigFile := e.getUsedConfig() 55 if usedConfigFile == "" { 56 e.log.Warnf("No config file detected") 57 os.Exit(exitcodes.NoConfigFileDetected) 58 } 59 60 fmt.Println(usedConfigFile) 61 }