From 01007b348918e67f805ebcf0f7ac5c7b9402e153 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 22:10:38 +0000 Subject: [PATCH 1/3] Initial plan From e7b6bf3b7bb5ef9b2b08d6c149122050e2ade951 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 22:14:39 +0000 Subject: [PATCH 2/3] refactor: wire version command RunE directly without InitConfig Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com> --- metadata/metadata.go | 46 +++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index f6644cd..90f17b5 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -20,14 +20,11 @@ package metadata import ( - "context" "encoding/json" "fmt" "os" "github.com/spf13/cobra" - - "github.com/dioad/cli" ) // Version is the application version, set via ldflags at build time. @@ -46,14 +43,14 @@ type BuildInfo struct { Date string } -type versionConfig struct { - JSON bool `mapstructure:"json"` -} - // NewVersionCommand returns a "version" cobra.Command that prints build information. // orgName and appName are used in the plain-text output line. +// +// Unlike commands built with cli.NewCommand, this command wires RunE directly and +// does not call InitConfig, so it produces no side effects such as reading config +// files, configuring logging, or starting a background WatchConfig goroutine. func NewVersionCommand(orgName, appName string, info BuildInfo) *cobra.Command { - cfg := &versionConfig{} + var jsonOutput bool cmd := &cobra.Command{ Use: "version", @@ -63,24 +60,25 @@ func NewVersionCommand(orgName, appName string, info BuildInfo) *cobra.Command { By default, this prints a single human-readable line with the application version. Use --json to print detailed version information (including version, commit, and date) as a JSON object.`, - Args: cobra.NoArgs, - } - - exec := func(_ context.Context, cfg *versionConfig) error { - if cfg.JSON { - details := map[string]string{ - "version": info.Version, - "commit": info.Commit, - "date": info.Date, + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + if jsonOutput { + details := map[string]string{ + "version": info.Version, + "commit": info.Commit, + "date": info.Date, + } + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(details) } - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - return enc.Encode(details) - } - fmt.Printf("%s %s %v\n", orgName, appName, info.Version) - return nil + fmt.Printf("%s %s %v\n", orgName, appName, info.Version) + return nil + }, } - return cli.NewCommand(cmd, exec, cfg) + cmd.Flags().BoolVar(&jsonOutput, "json", false, "Output version information as JSON") + + return cmd } From db29088e5fdd72ddb832780c386a336e103b8532 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 22:52:10 +0000 Subject: [PATCH 3/3] fix: use cmd.OutOrStdout() in RunE and add metadata tests Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com> --- metadata/metadata.go | 7 ++-- metadata/metadata_test.go | 72 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 metadata/metadata_test.go diff --git a/metadata/metadata.go b/metadata/metadata.go index 90f17b5..e54f437 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -22,7 +22,6 @@ package metadata import ( "encoding/json" "fmt" - "os" "github.com/spf13/cobra" ) @@ -61,19 +60,19 @@ By default, this prints a single human-readable line with the application versio Use --json to print detailed version information (including version, commit, and date) as a JSON object.`, Args: cobra.NoArgs, - RunE: func(_ *cobra.Command, _ []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { if jsonOutput { details := map[string]string{ "version": info.Version, "commit": info.Commit, "date": info.Date, } - enc := json.NewEncoder(os.Stdout) + enc := json.NewEncoder(cmd.OutOrStdout()) enc.SetIndent("", " ") return enc.Encode(details) } - fmt.Printf("%s %s %v\n", orgName, appName, info.Version) + fmt.Fprintf(cmd.OutOrStdout(), "%s %s %s\n", orgName, appName, info.Version) return nil }, } diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go new file mode 100644 index 0000000..5aef931 --- /dev/null +++ b/metadata/metadata_test.go @@ -0,0 +1,72 @@ +package metadata_test + +import ( + "bytes" + "encoding/json" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/dioad/cli/metadata" +) + +func TestNewVersionCommand_PlainText(t *testing.T) { + info := metadata.BuildInfo{ + Version: "1.2.3", + Commit: "abc1234", + Date: "2024-01-01", + } + + cmd := metadata.NewVersionCommand("myorg", "myapp", info) + + var buf bytes.Buffer + cmd.SetOut(&buf) + cmd.SetArgs([]string{}) + + err := cmd.Execute() + require.NoError(t, err) + + assert.Equal(t, "myorg myapp 1.2.3\n", buf.String()) +} + +func TestNewVersionCommand_JSON(t *testing.T) { + info := metadata.BuildInfo{ + Version: "1.2.3", + Commit: "abc1234", + Date: "2024-01-01", + } + + cmd := metadata.NewVersionCommand("myorg", "myapp", info) + + var buf bytes.Buffer + cmd.SetOut(&buf) + cmd.SetArgs([]string{"--json"}) + + err := cmd.Execute() + require.NoError(t, err) + + var got map[string]string + require.NoError(t, json.Unmarshal(buf.Bytes(), &got)) + assert.Equal(t, info.Version, got["version"]) + assert.Equal(t, info.Commit, got["commit"]) + assert.Equal(t, info.Date, got["date"]) +} + +func TestNewVersionCommand_NoInitConfig(t *testing.T) { + // This test verifies the command executes without requiring org/app context, + // confirming it bypasses CobraRunE / InitConfig entirely. + info := metadata.BuildInfo{Version: "0.1.0", Commit: "XX", Date: "1970-01-01"} + + cmd := metadata.NewVersionCommand("org", "app", info) + + var buf bytes.Buffer + cmd.SetOut(&buf) + cmd.SetArgs([]string{}) + + err := cmd.Execute() + require.NoError(t, err, "version command should run without context or config") + assert.True(t, strings.HasPrefix(buf.String(), "org app "), + "output should start with org and app name") +}