-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathconfig.go
68 lines (57 loc) · 1.52 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package commands
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
)
func (e *Executor) initConfig() {
cmd := &cobra.Command{
Use: "config",
Short: "Config",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
e.log.Fatalf("Usage: golangci-lint config")
}
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)
}
// getUsedConfig returns the resolved path to the golangci config file, or the empty string
// if no configuration could be found.
func (e *Executor) getUsedConfig() string {
usedConfigFile := viper.ConfigFileUsed()
if usedConfigFile == "" {
return ""
}
prettyUsedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "")
if err != nil {
e.log.Warnf("Can't pretty print config file path: %s", err)
return usedConfigFile
}
return prettyUsedConfigFile
}
func (e *Executor) executePathCmd(_ *cobra.Command, args []string) {
if len(args) != 0 {
e.log.Fatalf("Usage: golangci-lint config path")
}
usedConfigFile := e.getUsedConfig()
if usedConfigFile == "" {
e.log.Warnf("No config file detected")
os.Exit(exitcodes.NoConfigFileDetected)
}
fmt.Println(usedConfigFile)
os.Exit(exitcodes.Success)
}