1
1
package commands
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"os"
7
+ "path/filepath"
6
8
7
9
"github.com/fatih/color"
8
10
"github.com/spf13/cobra"
@@ -14,12 +16,17 @@ import (
14
16
"github.com/golangci/golangci-lint/pkg/logutils"
15
17
)
16
18
19
+ type pathOptions struct {
20
+ JSON bool
21
+ }
22
+
17
23
type configCommand struct {
18
24
viper * viper.Viper
19
25
cmd * cobra.Command
20
26
21
27
opts config.LoaderOptions
22
28
verifyOpts verifyOptions
29
+ pathOpts pathOptions
23
30
24
31
buildInfo BuildInfo
25
32
@@ -53,14 +60,16 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
53
60
SilenceErrors : true ,
54
61
}
55
62
63
+ pathCommand := & cobra.Command {
64
+ Use : "path" ,
65
+ Short : "Print used config path" ,
66
+ Args : cobra .NoArgs ,
67
+ ValidArgsFunction : cobra .NoFileCompletions ,
68
+ RunE : c .executePath ,
69
+ }
70
+
56
71
configCmd .AddCommand (
57
- & cobra.Command {
58
- Use : "path" ,
59
- Short : "Print used config path" ,
60
- Args : cobra .NoArgs ,
61
- ValidArgsFunction : cobra .NoFileCompletions ,
62
- Run : c .executePath ,
63
- },
72
+ pathCommand ,
64
73
verifyCommand ,
65
74
)
66
75
@@ -74,6 +83,9 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
74
83
verifyFlagSet .StringVar (& c .verifyOpts .schemaURL , "schema" , "" , color .GreenString ("JSON schema URL" ))
75
84
_ = verifyFlagSet .MarkHidden ("schema" )
76
85
86
+ pathFlagSet := pathCommand .Flags ()
87
+ pathFlagSet .BoolVar (& c .pathOpts .JSON , "json" , false , color .GreenString ("Display as JSON" ))
88
+
77
89
c .cmd = configCmd
78
90
79
91
return c
@@ -94,14 +106,29 @@ func (c *configCommand) preRunE(cmd *cobra.Command, args []string) error {
94
106
return nil
95
107
}
96
108
97
- func (c * configCommand ) executePath (cmd * cobra.Command , _ []string ) {
109
+ func (c * configCommand ) executePath (cmd * cobra.Command , _ []string ) error {
98
110
usedConfigFile := c .getUsedConfig ()
111
+
112
+ if c .pathOpts .JSON {
113
+ abs , err := filepath .Abs (usedConfigFile )
114
+ if err != nil {
115
+ return err
116
+ }
117
+
118
+ return json .NewEncoder (cmd .OutOrStdout ()).Encode (map [string ]string {
119
+ "path" : usedConfigFile ,
120
+ "absolutePath" : abs ,
121
+ })
122
+ }
123
+
99
124
if usedConfigFile == "" {
100
125
c .log .Warnf ("No config file detected" )
101
126
os .Exit (exitcodes .NoConfigFileDetected )
102
127
}
103
128
104
129
cmd .Println (usedConfigFile )
130
+
131
+ return nil
105
132
}
106
133
107
134
// getUsedConfig returns the resolved path to the golangci config file,
0 commit comments