Apply cobra best practices for testability and conventions#8347
Conversation
- Use cmd.ErrOrStderr()/cmd.OutOrStdout() instead of direct os.Stderr/os.Stdout references in root.go (Execute), completion.go, output.go, and proxy.go for improved testability - Replace manual RegisterFlagCompletionFunc with MarkFlagDirname() for directory flags (log-dir, payload-dir, wasm-cache-dir, tls-dir) per cobra conventions - Add SetFlagErrorFunc on root command for user-friendly flag parse error messages that include usage hints - Set SortFlags = false on root command's Flags and PersistentFlags to preserve flag registration order in help output - Update tests to verify MarkFlagDirname annotations instead of completion funcs Fixes #8327 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Updates the internal/cmd/ Cobra-based CLI wiring to follow maintainer-recommended patterns that improve testability and help/UX consistency, without changing intended runtime behavior.
Changes:
- Route CLI output through Cobra’s configured writers (
cmd.OutOrStdout()/cmd.ErrOrStderr()) instead of directos.Stdout/os.Stderr. - Replace manual “directory-only” completion functions with
MarkFlagDirname()annotations and preserve declared flag ordering in help output. - Add a
SetFlagErrorFuncto append a usage hint to flag-parse errors.
Show a summary per file
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 6/6 changed files
- Comments generated: 1
- Review effort level: Low
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot fix the failing ci check https://github.com/github/gh-aw-mcpg/actions/runs/28448349690/job/84303548609?pr=8347 |
The lint job was failing because |

Summary
Applies cobra maintainer-recommended best practices to the CLI code in
internal/cmd/, improving testability and following conventions without changing any runtime behavior.Fixes #8327
Changes
1.
cmd.ErrOrStderr()/cmd.OutOrStdout()over directos.Stderr/os.StdoutCobra provides
cmd.ErrOrStderr()andcmd.OutOrStdout()methods that return the command's configured output writers, defaulting toos.Stderr/os.Stdout. Using these instead of directosreferences allows tests to capture and verify output by callingcmd.SetOut()/cmd.SetErr()without monkey-patching global state.Files changed:
Execute()now usesrootCmd.ErrOrStderr()for error outputcmd.OutOrStdout()instead ofos.StdoutwriteGatewayConfigToStdout()accepts*cobra.Commandand usescmd.OutOrStdout()cmd.ErrOrStderr()instead ofos.Stderr2.
MarkFlagDirname()for directory flagsReplaces manual
RegisterFlagCompletionFuncclosures that returnedcobra.ShellCompDirectiveFilterDirswith cobra's built-inMarkFlagDirname(), which is the idiomatic way to indicate a flag accepts directory paths.Flags updated:
--log-dir,--payload-dir,--wasm-cache-dir--log-dir,--wasm-cache-dir,--tls-dir3.
SortFlags = falseon root command FlagSetSets
SortFlags = falseon bothFlags()andPersistentFlags()to preserve the intentional flag registration order in help output, matching the existingcobra.EnableCommandSorting = falsefor commands.4.
SetFlagErrorFuncfor user-friendly flag parse error messagesAdds a custom flag error function that appends a
See '<command> --help' for usagehint to flag parse errors, making it easier for users to recover from typos or missing values.Testing
flags_test.goto verifyMarkFlagDirnameannotations (viacobra.BashCompSubdirsInDir) instead of checking for completion functionsmake agent-finished✓