Skip to content

Commit 610cc04

Browse files
authored
feat: add an option to display config path as JSON (#5431)
1 parent 5a783ba commit 610cc04

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

pkg/commands/config.go

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package commands
22

33
import (
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+
1723
type 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

Comments
 (0)