From d35ff7776b5445de1e911afb1570e7bd938cfc68 Mon Sep 17 00:00:00 2001 From: Pat Downey Date: Wed, 11 Mar 2026 21:54:38 +0000 Subject: [PATCH 1/9] feat: add `metadata` package for build-time versioning - Introduced `metadata` package to provide build-time information for CLI applications. - Added `Version`, `Commit`, and `Date` variables set via ldflags at build time. - Implemented `NewVersionCommand` to display version details in plain-text or JSON format. --- metadata/metadata.go | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 metadata/metadata.go diff --git a/metadata/metadata.go b/metadata/metadata.go new file mode 100644 index 0000000..b0d71b5 --- /dev/null +++ b/metadata/metadata.go @@ -0,0 +1,81 @@ +// Package metadata provides build-time information for CLI applications. +// +// Services set their build variables via ldflags at build time: +// +// go build -ldflags "\ +// -X github.com/dioad/cli/metadata.Version=$(VERSION) \ +// -X github.com/dioad/cli/metadata.Commit=$(COMMIT) \ +// -X github.com/dioad/cli/metadata.Date=$(DATE)" \ +// ./... +// +// Then pass a BuildInfo to NewVersionCommand: +// +// cmd.AddCommand( +// metadata.NewVersionCommand(OrgName, AppName, metadata.BuildInfo{ +// Version: metadata.Version, +// Commit: metadata.Commit, +// Date: metadata.Date, +// }), +// ) +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. +var Version = "local" + +// Commit is the git commit SHA, set via ldflags at build time. +var Commit = "XX" + +// Date is the build date, set via ldflags at build time. +var Date = "1970-01-01" + +// BuildInfo holds version information populated at build time. +type BuildInfo struct { + Version string + Commit string + 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. +func NewVersionCommand(orgName, appName string, info BuildInfo) *cobra.Command { + cfg := &versionConfig{} + + cmd := &cobra.Command{ + Use: "version", + Short: "Shows version information", + Long: `Shows detailed version information.`, + } + + exec := func(_ context.Context, cfg *versionConfig) error { + if cfg.JSON { + details := map[string]string{ + "version": info.Version, + "commit": info.Commit, + "date": info.Date, + } + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(details) + } + + fmt.Printf("%s %s %v\n", orgName, appName, info.Version) + return nil + } + + return cli.NewCommand(cmd, exec, cfg) +} From 1892ae24af6006257aeca575f1211f947aa6ee85 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 22:04:44 +0000 Subject: [PATCH 2/9] Initial plan From 7112254eae8dcbfdd71b4323be3196dafe02e8de Mon Sep 17 00:00:00 2001 From: Pat Downey Date: Wed, 11 Mar 2026 22:04:49 +0000 Subject: [PATCH 3/9] Update metadata/metadata.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- metadata/metadata.go | 1 + 1 file changed, 1 insertion(+) diff --git a/metadata/metadata.go b/metadata/metadata.go index b0d71b5..6567872 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -59,6 +59,7 @@ func NewVersionCommand(orgName, appName string, info BuildInfo) *cobra.Command { Use: "version", Short: "Shows version information", Long: `Shows detailed version information.`, + Args: cobra.NoArgs, } exec := func(_ context.Context, cfg *versionConfig) error { From 189a429899f168326bd5db66bb683397b7f5008f Mon Sep 17 00:00:00 2001 From: Pat Downey Date: Wed, 11 Mar 2026 22:05:17 +0000 Subject: [PATCH 4/9] Update metadata/metadata.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- metadata/metadata.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 6567872..f6644cd 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -57,8 +57,12 @@ func NewVersionCommand(orgName, appName string, info BuildInfo) *cobra.Command { cmd := &cobra.Command{ Use: "version", - Short: "Shows version information", - Long: `Shows detailed version information.`, + Short: "Show version information (use --json for detailed output)", + Long: `Show version information. + +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, } From f9f30ea2c31008c0eff3dc556b099b9ce2f6f71e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 22:08:49 +0000 Subject: [PATCH 5/9] test: add tests for metadata package version command Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com> --- metadata/metadata_test.go | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 metadata/metadata_test.go diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go new file mode 100644 index 0000000..3e38f06 --- /dev/null +++ b/metadata/metadata_test.go @@ -0,0 +1,113 @@ +package metadata_test + +import ( + "bytes" + "context" + "encoding/json" + "io" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/dioad/cli" + "github.com/dioad/cli/metadata" +) + +// captureStdout redirects os.Stdout to a pipe, calls f, then returns +// everything written to the pipe as a string. +func captureStdout(t *testing.T, f func()) string { + t.Helper() + + r, w, err := os.Pipe() + require.NoError(t, err) + + origStdout := os.Stdout + os.Stdout = w + + f() + + os.Stdout = origStdout + w.Close() + + var buf bytes.Buffer + _, err = io.Copy(&buf, r) + require.NoError(t, err) + r.Close() + + return buf.String() +} + +func newTestContext() context.Context { + return cli.Context( + context.Background(), + cli.SetOrgName("testorg"), + cli.SetAppName("testapp"), + ) +} + +// TestNewVersionCommand_FlagRegistered verifies that the --json flag is +// registered on the command returned by NewVersionCommand. +func TestNewVersionCommand_FlagRegistered(t *testing.T) { + info := metadata.BuildInfo{ + Version: "1.2.3", + Commit: "abc1234", + Date: "2024-01-01", + } + + cmd := metadata.NewVersionCommand("myorg", "myapp", info) + + jsonFlag := cmd.Flags().Lookup("json") + require.NotNil(t, jsonFlag, "--json flag must be registered on the version command") + assert.Equal(t, "false", jsonFlag.DefValue, "--json flag default should be false") +} + +// TestNewVersionCommand_PlainOutput verifies that the command prints a single +// "orgName appName version" line when --json is not set. +func TestNewVersionCommand_PlainOutput(t *testing.T) { + info := metadata.BuildInfo{ + Version: "1.2.3", + Commit: "abc1234", + Date: "2024-01-01", + } + + cmd := metadata.NewVersionCommand("myorg", "myapp", info) + cmd.SetArgs([]string{}) + cmd.SilenceUsage = true + + var execErr error + got := captureStdout(t, func() { + execErr = cmd.ExecuteContext(newTestContext()) + }) + + require.NoError(t, execErr) + assert.Equal(t, "myorg myapp 1.2.3\n", got) +} + +// TestNewVersionCommand_JSONOutput verifies that the command prints valid JSON +// containing version, commit, and date fields when --json is passed. +func TestNewVersionCommand_JSONOutput(t *testing.T) { + info := metadata.BuildInfo{ + Version: "1.2.3", + Commit: "abc1234", + Date: "2024-01-01", + } + + cmd := metadata.NewVersionCommand("myorg", "myapp", info) + cmd.SetArgs([]string{"--json"}) + cmd.SilenceUsage = true + + var execErr error + got := captureStdout(t, func() { + execErr = cmd.ExecuteContext(newTestContext()) + }) + + require.NoError(t, execErr) + + var result map[string]string + require.NoError(t, json.Unmarshal([]byte(got), &result), "output should be valid JSON") + assert.Equal(t, info.Version, result["version"]) + assert.Equal(t, info.Commit, result["commit"]) + assert.Equal(t, info.Date, result["date"]) +} 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 6/9] 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 7/9] 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 f3b0f74202a35c23c18b9f6e813513324aa35c02 Mon Sep 17 00:00:00 2001 From: Pat Downey Date: Wed, 11 Mar 2026 22:22:58 +0000 Subject: [PATCH 8/9] Update metadata/metadata_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- metadata/metadata_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 3e38f06..5b7c791 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -24,11 +24,12 @@ func captureStdout(t *testing.T, f func()) string { require.NoError(t, err) origStdout := os.Stdout + defer func() { + os.Stdout = origStdout + }() os.Stdout = w f() - - os.Stdout = origStdout w.Close() var buf bytes.Buffer 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 9/9] 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") +}