Skip to content

metadata: wire version command RunE directly, bypassing InitConfig - #10

Merged
patdowney merged 4 commits into
feat/cli-metadatafrom
copilot/sub-pr-8-again
Mar 11, 2026
Merged

patdowney merged 4 commits into
feat/cli-metadatafrom
copilot/sub-pr-8-again

Conversation

Copilot AI commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

NewVersionCommand was routing through cli.NewCommand → CobraRunE → InitConfig, causing the version subcommand to read config files, configure logging, and potentially spin up a background WatchConfig goroutine — none of which are appropriate for a version-printing command.

Changes

  • metadata/metadata.go: Replace cli.NewCommand with a direct cmd.RunE closure; register --json via cmd.Flags().BoolVar instead of going through gpflag/mapstructure
  • Remove versionConfig struct and drop the github.com/dioad/cli and context imports — the package now only depends on cobra
  • Both output paths (plain-text and JSON) now write via cmd.OutOrStdout() instead of os.Stdout, respecting Cobra's output plumbing (SetOut/OutOrStdout) for easier output capture and redirection
  • Use %s format verb (instead of %v) for the version string in the plain-text output path
  • metadata/metadata_test.go (new): Unit tests covering plain-text output, JSON output, and the no-InitConfig property
// Before: routed through cli.NewCommand → CobraRunE → InitConfig
return cli.NewCommand(cmd, exec, cfg)

// After: RunE wired directly, no side effects, output via cmd.OutOrStdout()
cmd.Flags().BoolVar(&jsonOutput, "json", false, "Output version information as JSON")
cmd.RunE = func(cmd *cobra.Command, _ []string) error {
    fmt.Fprintf(cmd.OutOrStdout(), "%s %s %s\n", orgName, appName, info.Version)
    ...
}
return cmd

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com>
Copilot AI changed the title [WIP] [WIP] Address feedback on metadata package for build-time versioning metadata: wire version command RunE directly, bypassing InitConfig Mar 11, 2026
@patdowney
patdowney marked this pull request as ready for review March 11, 2026 22:22
Copilot AI review requested due to automatic review settings March 11, 2026 22:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Comment thread metadata/metadata.go Outdated
Comment on lines +64 to +73
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)

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RunE currently discards the cobra.Command parameter and writes JSON directly to os.Stdout. This bypasses Cobra’s output plumbing (SetOut/SetErr, OutOrStdout), making output capture/redirection and testing harder. Consider using cmd.OutOrStdout() (and cmd.OutOrStdout()/cmd.Print APIs) instead of os.Stdout here, and keep the cmd parameter rather than naming it '_' .

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in db29088. Both output paths now write via cmd.OutOrStdout() instead of os.Stdout, the cmd *cobra.Command parameter is kept (not discarded with _), and %v is changed to %s for the version string. A new test file metadata/metadata_test.go covers plain-text output, JSON output, and the no-InitConfig property.

Comment thread metadata/metadata.go Outdated
Comment on lines +76 to +77
fmt.Printf("%s %s %v\n", orgName, appName, info.Version)
return nil

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plain-text output path uses fmt.Printf (global stdout) and formats info.Version with %v even though it’s a string. For consistency with Cobra and easier output capture, prefer writing via the command’s configured writer (e.g., OutOrStdout) and use %s for the version string.

Copilot uses AI. Check for mistakes.
Comment thread metadata/metadata.go
Comment on lines 52 to +66
@@ -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{

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewVersionCommand’s behavior was reworked (no cli.NewCommand wrapper, new flag wiring, different execution path) but there are no tests covering the default output vs --json output. Adding a small unit test that executes the command with/without --json and asserts on the emitted output would help prevent regressions.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were already added in db29088: TestNewVersionCommand_PlainText (default output), TestNewVersionCommand_JSON (--json output), and TestNewVersionCommand_NoInitConfig (verifies no context/config required). All three pass.

Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com>
@patdowney
patdowney merged commit b33cee9 into feat/cli-metadata Mar 11, 2026
@patdowney
patdowney deleted the copilot/sub-pr-8-again branch March 11, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants