Skip to content

feat(cmd): cobra improvements from go-fan module review#7630

Merged
lpcox merged 2 commits into
mainfrom
copilot/go-fan-review-cobra-module
Jun 16, 2026
Merged

feat(cmd): cobra improvements from go-fan module review#7630
lpcox merged 2 commits into
mainfrom
copilot/go-fan-review-cobra-module

Conversation

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Implements the cobra improvement recommendations from the go-fan module review: declarative flag constraints, corrected shell completion, command ordering, and a configurable shutdown timeout.

TLS flag co-requirement (flags_tls.go)

  • Added cmd.MarkFlagsRequiredTogether("tls-cert", "tls-key") — cobra now rejects mismatched CLI flags at parse time with a consistent error message
  • Manual hasCert != hasKey check in run() is keptMarkFlagsRequiredTogether only fires when flags are explicitly changed on the command line; it does not cover MCP_GATEWAY_TLS_CERT/MCP_GATEWAY_TLS_KEY env-var defaults
// flags_tls.go — constraint co-located with declaration
cmd.MarkFlagsRequiredTogether("tls-cert", "tls-key")

New flags_tls_test.go covers cert-only, key-only, both, and neither cases.

--env completion fix (flags.go)

  • MarkFlagFilename("env", "env")MarkFlagFilename("env"): was filtering to *.env files, missing the canonical .env file (which has no extension)

Command sorting (root.go)

  • cobra.EnableCommandSorting = false — preserves the intentional AddGroup registration order within each group (cobra was overriding it alphabetically)
  • rootCmd.CompletionOptions.DisableDefaultCmd = true — makes intent explicit; a custom completion command is already provided

SilenceErrors/SilenceUsage comments (root.go)

  • Added comments clarifying cobra's semantics: SilenceErrors is read from the root command for all subcommands; SilenceUsage is read from the specific subcommand

Configurable shutdown timeout (flags_serve.go, serve.go)

  • Replaces hardcoded const httpServerShutdownTimeout = 5 * time.Second with a --shutdown-timeout flag (default 5s) and MCP_GATEWAY_SHUTDOWN_TIMEOUT env-var override, following the existing FlagRegistrar pattern
  • Applied to both the root run() and proxy runProxy() paths

GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:06 View session
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:08
- Add MarkFlagsRequiredTogether("tls-cert","tls-key") in flags_tls.go;
  keep manual env-var check in run() for non-CLI defaults
- Add flags_tls_test.go with cobra co-requirement tests
- Fix --env completion to show all files (no .env extension filter)
- Add cobra.EnableCommandSorting = false to preserve group ordering
- Add rootCmd.CompletionOptions.DisableDefaultCmd = true
- Improve SilenceErrors/SilenceUsage comments (cobra per-cmd vs root semantics)
- Add --shutdown-timeout flag (MCP_GATEWAY_SHUTDOWN_TIMEOUT, default 5s)
  used by both root and proxy serveAndWait calls

Closes #7619
Copilot AI changed the title [WIP] Review go module spf13/cobra feat(cmd): cobra improvements from go-fan module review Jun 16, 2026
Copilot finished work on behalf of lpcox June 16, 2026 17:18
Copilot AI requested a review from lpcox June 16, 2026 17:18
@lpcox lpcox marked this pull request as ready for review June 16, 2026 17:20
Copilot AI review requested due to automatic review settings June 16, 2026 17:20
GitHub Advanced Security started work on behalf of lpcox June 16, 2026 17:21 View session
@lpcox lpcox merged commit b1a0af2 into main Jun 16, 2026
36 checks passed
GitHub Advanced Security finished work on behalf of lpcox June 16, 2026 17:22
@lpcox lpcox deleted the copilot/go-fan-review-cobra-module branch June 16, 2026 17: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.

Pull request overview

This PR implements several CLI usability and correctness improvements in the internal/cmd Cobra-based CLI layer, based on feedback from the go-fan module review. It focuses on making flag constraints more declarative, improving shell completion behavior, preserving intended command ordering, and introducing a configurable HTTP graceful shutdown timeout used by both gateway and proxy server paths.

Changes:

  • Enforced --tls-cert/--tls-key co-requirement at Cobra parse time (while retaining runtime validation for env-var defaults) and added focused tests for the constraint.
  • Fixed --env shell completion to allow the canonical .env filename (no extension) and updated tests accordingly.
  • Introduced a --shutdown-timeout flag with MCP_GATEWAY_SHUTDOWN_TIMEOUT env-var defaulting, and wired it into both root and proxy serve/shutdown flows.
Show a summary per file
File Description
internal/cmd/serve.go Removes the hardcoded shutdown timeout constant in favor of a configurable timeout passed into serveAndWait.
internal/cmd/root.go Adds Cobra behavior comments, disables command sorting to preserve registration order, disables default completion cmd, and uses shutdownTimeout for graceful shutdown.
internal/cmd/proxy.go Uses the shared shutdownTimeout when running the proxy server via serveAndWait.
internal/cmd/flags.go Fixes --env filename completion to show all files (including .env with no extension).
internal/cmd/flags_tls.go Adds MarkFlagsRequiredTogether("tls-cert","tls-key") to enforce TLS flag pairing at parse time.
internal/cmd/flags_tls_test.go Adds tests validating Cobra’s required-together TLS behavior and verifies TLS flags are registered.
internal/cmd/flags_test.go Updates completion tests to expect no extension filter for --env.
internal/cmd/flags_serve.go Adds the new --shutdown-timeout flag (with env-var default) backing shutdownTimeout.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 8/8 changed files
  • Comments generated: 4


func init() {
RegisterFlag(func(cmd *cobra.Command) {
cmd.Flags().DurationVar(
Comment thread internal/cmd/root.go
Comment on lines 463 to +467
if err := serveAndWait(
ctx,
cancel,
httpServer,
httpServerShutdownTimeout,
shutdownTimeout,
Comment thread internal/cmd/proxy.go
Comment on lines 268 to +272
err = serveAndWait(
ctx,
cancel,
httpServer,
httpServerShutdownTimeout,
shutdownTimeout,
Comment thread internal/cmd/flags_tls.go
Comment on lines +24 to +28
// Cert and key must always be provided together when set via CLI flags.
// Note: env-var defaults (MCP_GATEWAY_TLS_CERT/MCP_GATEWAY_TLS_KEY) are
// validated at runtime in run() since MarkFlagsRequiredTogether only fires
// when flags are explicitly changed on the command line.
cmd.MarkFlagsRequiredTogether("tls-cert", "tls-key")
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