forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
149 lines (117 loc) · 3.5 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package commands
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type pathOptions struct {
JSON bool
}
type configCommand struct {
viper *viper.Viper
cmd *cobra.Command
opts config.LoaderOptions
verifyOpts verifyOptions
pathOpts pathOptions
buildInfo BuildInfo
log logutils.Log
}
func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
c := &configCommand{
viper: viper.New(),
log: log,
buildInfo: info,
}
configCmd := &cobra.Command{
Use: "config",
Short: "Config file information",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmd.Help()
},
PersistentPreRunE: c.preRunE,
}
verifyCommand := &cobra.Command{
Use: "verify",
Short: "Verify configuration against JSON schema",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.executeVerify,
SilenceUsage: true,
SilenceErrors: true,
}
pathCommand := &cobra.Command{
Use: "path",
Short: "Print used config path",
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: c.executePath,
}
configCmd.AddCommand(
pathCommand,
verifyCommand,
)
flagSet := configCmd.PersistentFlags()
flagSet.SortFlags = false // sort them as they are defined here
setupConfigFileFlagSet(flagSet, &c.opts)
// ex: --schema jsonschema/golangci.next.jsonschema.json
verifyFlagSet := verifyCommand.Flags()
verifyFlagSet.StringVar(&c.verifyOpts.schemaURL, "schema", "", color.GreenString("JSON schema URL"))
_ = verifyFlagSet.MarkHidden("schema")
pathFlagSet := pathCommand.Flags()
pathFlagSet.BoolVar(&c.pathOpts.JSON, "json", false, color.GreenString("Display as JSON"))
c.cmd = configCmd
return c
}
func (c *configCommand) preRunE(cmd *cobra.Command, args []string) error {
// The command doesn't depend on the real configuration.
// It only needs to know the path of the configuration file.
cfg := config.NewDefault()
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts, cfg, args)
err := loader.Load(config.LoadOptions{})
if err != nil {
return fmt.Errorf("can't load config: %w", err)
}
return nil
}
func (c *configCommand) executePath(cmd *cobra.Command, _ []string) error {
usedConfigFile := c.getUsedConfig()
if c.pathOpts.JSON {
abs, err := filepath.Abs(usedConfigFile)
if err != nil {
return err
}
return json.NewEncoder(cmd.OutOrStdout()).Encode(map[string]string{
"path": usedConfigFile,
"absolutePath": abs,
})
}
if usedConfigFile == "" {
c.log.Warnf("No config file detected")
os.Exit(exitcodes.NoConfigFileDetected)
}
cmd.Println(usedConfigFile)
return nil
}
// getUsedConfig returns the resolved path to the golangci config file,
// or the empty string if no configuration could be found.
func (c *configCommand) getUsedConfig() string {
usedConfigFile := c.viper.ConfigFileUsed()
if usedConfigFile == "" {
return ""
}
prettyUsedConfigFile, err := fsutils.ShortestRelPath(usedConfigFile, "")
if err != nil {
c.log.Warnf("Can't pretty print config file path: %s", err)
return usedConfigFile
}
return prettyUsedConfigFile
}