From 505daf72e9ed70444df7544e0a29d9e4b20efdc1 Mon Sep 17 00:00:00 2001 From: Jon Gallant <2163001+jongio@users.noreply.github.com> Date: Fri, 24 Jul 2026 14:32:55 -0700 Subject: [PATCH] config: add JSON path output Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 340b4dd3-b4b3-4f1a-9163-66b16d96fa81 --- cmd/config.go | 22 ++++++++++++++++++++-- cmd/config_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index ad37831..c2bd9e0 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "fmt" "io" "os" @@ -63,14 +64,31 @@ func newConfigCheckCmdWithKeymap(load configLoadFunc, path configPathFunc, loadK const configPathCommandName = "path" func newConfigPathCmd(path configPathFunc, dataPath dataPathFunc) *cobra.Command { - return &cobra.Command{ + var asJSON bool + pathCmd := &cobra.Command{ Use: configPathCommandName, Short: "Print resolved grut config and data paths", RunE: func(cmd *cobra.Command, args []string) error { - _, err := fmt.Fprintf(cmd.OutOrStdout(), "Config: %s\nData: %s\n", path(), dataPath()) + paths := configPaths{ + Config: path(), + Data: dataPath(), + } + if asJSON { + enc := json.NewEncoder(cmd.OutOrStdout()) + enc.SetIndent("", " ") + return enc.Encode(paths) + } + _, err := fmt.Fprintf(cmd.OutOrStdout(), "Config: %s\nData: %s\n", paths.Config, paths.Data) return err }, } + pathCmd.Flags().BoolVar(&asJSON, "json", false, "Print resolved paths as JSON") + return pathCmd +} + +type configPaths struct { + Config string `json:"config"` + Data string `json:"data"` } const defaultKeybindingScheme = "default" diff --git a/cmd/config_test.go b/cmd/config_test.go index 1758436..537d922 100644 --- a/cmd/config_test.go +++ b/cmd/config_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "encoding/json" "errors" "os" "path/filepath" @@ -113,6 +114,24 @@ func TestConfigPathPrintsResolvedPaths(t *testing.T) { } } +func TestConfigPathPrintsJSON(t *testing.T) { + cmd := newConfigPathCmd( + func() string { return windowsConfigPath }, + func() string { return windowsDataPath }, + ) + cmd.SetArgs([]string{"--json"}) + var out bytes.Buffer + cmd.SetOut(&out) + + err := cmd.Execute() + + require.NoError(t, err) + var paths configPaths + require.NoError(t, json.Unmarshal(out.Bytes(), &paths)) + assert.Equal(t, windowsConfigPath, paths.Config) + assert.Equal(t, windowsDataPath, paths.Data) +} + func TestRootRegistersConfigCheck(t *testing.T) { root, cleanup := newRootCommand() defer cleanup() @@ -136,6 +155,7 @@ func TestRootRegistersConfigPath(t *testing.T) { require.NoError(t, err) require.NotNil(t, pathCmd) assert.Equal(t, configPathCommandName, pathCmd.Name()) + require.NotNil(t, pathCmd.Flags().Lookup("json"), "--json flag should be registered") } func TestRootRegistersConfigGet(t *testing.T) {