From e96a36a066919284e70e279b9d2d387fd765a3dc Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 15 Feb 2025 04:52:57 +0100 Subject: [PATCH] feat: display config path as JSON --- pkg/commands/config.go | 43 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/pkg/commands/config.go b/pkg/commands/config.go index 935ec5e8643c..2d25545779d7 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -1,8 +1,10 @@ package commands import ( + "encoding/json" "fmt" "os" + "path/filepath" "github.com/fatih/color" "github.com/spf13/cobra" @@ -14,12 +16,17 @@ import ( "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 @@ -53,14 +60,16 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand { SilenceErrors: true, } + pathCommand := &cobra.Command{ + Use: "path", + Short: "Print used config path", + Args: cobra.NoArgs, + ValidArgsFunction: cobra.NoFileCompletions, + RunE: c.executePath, + } + configCmd.AddCommand( - &cobra.Command{ - Use: "path", - Short: "Print used config path", - Args: cobra.NoArgs, - ValidArgsFunction: cobra.NoFileCompletions, - Run: c.executePath, - }, + pathCommand, verifyCommand, ) @@ -74,6 +83,9 @@ func newConfigCommand(log logutils.Log, info BuildInfo) *configCommand { 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 @@ -94,14 +106,29 @@ func (c *configCommand) preRunE(cmd *cobra.Command, args []string) error { return nil } -func (c *configCommand) executePath(cmd *cobra.Command, _ []string) { +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,