Skip to content

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

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

[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>
@lpcox lpcox marked this pull request as ready for review April 23, 2026 16:46
Copilot AI review requested due to automatic review settings April 23, 2026 16:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 refactors parts of the internal/cmd cobra CLI setup to improve UX and reduce completion boilerplate for the awmg command, aligning with recommendations from issue #4379.

Changes:

  • Add Args: cobra.NoArgs to the root command to reject accidental positional args with clearer cobra diagnostics.
  • Replace custom flag completion lambdas with MarkFlagFilename / MarkFlagDirname helpers.
  • Update tests to avoid calling preRun(nil, nil) by passing an empty *cobra.Command.
Show a summary per file
File Description
internal/cmd/root.go Enforces no positional args on the root command for improved UX.
internal/cmd/flags.go Simplifies file/dir flag completion registration using cobra helper APIs.
internal/cmd/root_test.go Makes preRun tests more robust by avoiding nil *cobra.Command.

Copilot's findings

Tip

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

Comments suppressed due to low confidence (1)

internal/cmd/flags.go:70

  • MarkFlagFilename/MarkFlagDirname (and RegisterFlagCompletionFunc below) return an error in cobra. Calling them without handling the return value will fail to compile and also hides cases where the flag name is wrong or not registered yet. Please capture and handle the errors (e.g., fail fast during init, or intentionally ignore via _ = ... with a comment explaining why).
	// File and directory completions using idiomatic cobra helpers
	cmd.MarkFlagFilename("config", "toml")
	cmd.MarkFlagDirname("log-dir")
	cmd.MarkFlagDirname("payload-dir")
	cmd.MarkFlagFilename("env", "env")

	// Enum completions for DIFC flags
	cmd.RegisterFlagCompletionFunc("guards-mode", cobra.FixedCompletions(
		[]string{"strict", "filter", "propagate"}, cobra.ShellCompDirectiveNoFileComp))
	cmd.RegisterFlagCompletionFunc("allowonly-min-integrity", cobra.FixedCompletions(
		[]string{"none", "unapproved", "approved", "merged"}, cobra.ShellCompDirectiveNoFileComp))
  • Files reviewed: 3/3 changed files
  • Comments generated: 0

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.

1 participant