Skip to content

[Repo Assist] refactor(cmd): cobra UX improvements — NoArgs, MarkFlagFilename, preRun test fix#4395

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/improve-cobra-cli-ux-9ec28405ff7d97f0
Draft

[Repo Assist] refactor(cmd): cobra UX improvements — NoArgs, MarkFlagFilename, preRun test fix#4395
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/improve-cobra-cli-ux-9ec28405ff7d97f0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Relates to #4379 (cobra module review — items P1, P2, P3)

Changes

1. Args: cobra.NoArgs on root command (root.go)

Without this, typing awmg config.toml (a common mistake) produces:

MCPG Error: required flag(s) "config" not set

With cobra.NoArgs, cobra gives a clear diagnostic before any validation runs:

Error: unknown command "config.toml" for "awmg"

This is a one-line change; it cannot affect normal usage (the root command runs with RunE: run, which takes no positional arguments).

2. Simplify file/directory completions (flags.go)

Replace four verbose RegisterFlagCompletionFunc lambdas with idiomatic cobra shorthands:

Before After
RegisterFlagCompletionFunc("config", func(...) { return []string{"toml"}, FilterFileExt }) MarkFlagFilename("config", "toml")
RegisterFlagCompletionFunc("log-dir", func(...) { return nil, FilterDirs }) MarkFlagDirname("log-dir")
RegisterFlagCompletionFunc("payload-dir", func(...) { return nil, FilterDirs }) MarkFlagDirname("payload-dir")
RegisterFlagCompletionFunc("env", func(...) { return []string{"env"}, FilterFileExt }) MarkFlagFilename("env", "env")

Shell-completion behaviour is identical; the delta is −20 lines of boilerplate. MarkFlagFilename/MarkFlagDirname have been available since cobra v1.5; this project uses v1.10.2.

3. Fix fragile preRun(nil, nil) in tests (root_test.go)

Nine test cases called preRun(nil, nil). This works today because preRun never dereferences cmd. Any future addition that calls cmd.Context(), cmd.Flags(), or any other method will panic at nil. Replacing with preRun(&cobra.Command{}, nil) eliminates the footgun with zero test-logic change.

Test Status

⚠️ Infrastructure limitation: proxy.golang.org is blocked in this CI environment and Go 1.25.0 toolchain cannot be downloaded. This is a pre-existing issue affecting all Go builds in this environment.

The changes are:

  • root.go: one struct-field addition (Args: cobra.NoArgs)
  • flags.go: method call substitutions with identical semantics
  • root_test.go: nil&cobra.Command{} in 9 call sites (no logic change)

All three are syntactically straightforward refactors with no logic changes.

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Repo Assist · ● 5.9M ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

…un test fix

Implements three quick-win improvements identified in issue #4379 (cobra module
review):

1. Add Args: cobra.NoArgs to root command: positional arguments are not
   supported by awmg; without this, 'awmg config.toml' silently falls through
   to a confusing 'required flag(s) not set' error instead of a clear
   'unknown command' message.

2. Replace verbose RegisterFlagCompletionFunc callbacks with idiomatic cobra
   helpers: MarkFlagFilename("config", "toml"), MarkFlagDirname("log-dir"),
   MarkFlagDirname("payload-dir"), MarkFlagFilename("env", "env").
   Reduces ~20 lines of boilerplate to 4 lines with identical shell-completion
   behaviour.

3. Fix fragile preRun(nil, nil) in root_test.go: replace all 9 occurrences with
   preRun(&cobra.Command{}, nil). The current code is safe because preRun does
   not dereference cmd, but any future use of cmd.Context(), cmd.Flags(), etc.
   would cause a nil pointer panic. Using a real (empty) Command value prevents
   that footgun.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants