Repository navigation
metadata: wire version command RunE directly, bypassing InitConfig - #10
Conversation
Co-authored-by: patdowney <625132+patdowney@users.noreply.github.com>
| 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) |
There was a problem hiding this comment.
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 '_' .
There was a problem hiding this comment.
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.
| fmt.Printf("%s %s %v\n", orgName, appName, info.Version) | ||
| return nil |
There was a problem hiding this comment.
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.
| @@ -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{ | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
NewVersionCommandwas routing throughcli.NewCommand→CobraRunE→InitConfig, causing theversionsubcommand to read config files, configure logging, and potentially spin up a backgroundWatchConfiggoroutine — none of which are appropriate for a version-printing command.Changes
metadata/metadata.go: Replacecli.NewCommandwith a directcmd.RunEclosure; register--jsonviacmd.Flags().BoolVarinstead of going throughgpflag/mapstructureversionConfigstruct and drop thegithub.com/dioad/cliandcontextimports — the package now only depends oncobracmd.OutOrStdout()instead ofos.Stdout, respecting Cobra's output plumbing (SetOut/OutOrStdout) for easier output capture and redirection%sformat verb (instead of%v) for the version string in the plain-text output pathmetadata/metadata_test.go(new): Unit tests covering plain-text output, JSON output, and the no-InitConfigproperty💡 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.