Skip to content

feat: add an option to display config path as JSON #5431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions pkg/commands/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand All @@ -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

Expand Down Expand Up @@ -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,
)

Expand All @@ -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
Expand All @@ -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,
Expand Down
Loading