11package commands
22
33import (
4+ "encoding/json"
45 "fmt"
56 "os"
7+ "path/filepath"
68
79 "github.com/fatih/color"
810 "github.com/spf13/cobra"
@@ -14,12 +16,17 @@ import (
1416 "github.com/golangci/golangci-lint/pkg/logutils"
1517)
1618
19+ type pathOptions struct {
20+ JSON bool
21+ }
22+
1723type configCommand struct {
1824 viper * viper.Viper
1925 cmd * cobra.Command
2026
2127 opts config.LoaderOptions
2228 verifyOpts verifyOptions
29+ pathOpts pathOptions
2330
2431 buildInfo BuildInfo
2532
@@ -53,14 +60,16 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
5360 SilenceErrors : true ,
5461 }
5562
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+
5671 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 ,
6473 verifyCommand ,
6574 )
6675
@@ -74,6 +83,9 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand {
7483 verifyFlagSet .StringVar (& c .verifyOpts .schemaURL , "schema" , "" , color .GreenString ("JSON schema URL" ))
7584 _ = verifyFlagSet .MarkHidden ("schema" )
7685
86+ pathFlagSet := pathCommand .Flags ()
87+ pathFlagSet .BoolVar (& c .pathOpts .JSON , "json" , false , color .GreenString ("Display as JSON" ))
88+
7789 c .cmd = configCmd
7890
7991 return c
@@ -94,14 +106,29 @@ func (c *configCommand) preRunE(cmd *cobra.Command, args []string) error {
94106 return nil
95107}
96108
97- func (c * configCommand ) executePath (cmd * cobra.Command , _ []string ) {
109+ func (c * configCommand ) executePath (cmd * cobra.Command , _ []string ) error {
98110 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+
99124 if usedConfigFile == "" {
100125 c .log .Warnf ("No config file detected" )
101126 os .Exit (exitcodes .NoConfigFileDetected )
102127 }
103128
104129 cmd .Println (usedConfigFile )
130+
131+ return nil
105132}
106133
107134// getUsedConfig returns the resolved path to the golangci config file,
0 commit comments