Can you use the golang cli skill to review the cli before the v1 release?
- User
Can you use the golang cli skill to review the cli before the v1 release?
Assistant
Interesting findings already. Let me check a few more of the skill's review checkpoints.
Assistant
All tests pass. Let me check a couple of remaining review checkpoints before writing up.
Assistant
- User
yes do that. And perhaps also 5?
Assistant
On it. Let me look at the exact spots to change.
Assistant
Now let me verify cobra's
FlagErrorFuncinheritance before writing the fix, and check the test assertions on the error texts I'm touching.Assistant
No text assertions on that error. Now making all the edits.
Assistant
Now wrap the
Argsvalidators across all command files:Assistant
All 22 validators wrapped. Now findings 2–4:
Assistant
Now finding 5 — README exit-code contract. Let me see the current intro of that section:
Assistant
Now build, vet, staticcheck, and run the full test suite:
Assistant
The regex left an unbalanced paren. Fixing:
Assistant
Extra closing paren — the first regex had already closed the call. Fixing:
Assistant
All lint findings cleared (gofmt skips those two files because they're Go template source, not compilable Go — reverted). Now the full test suite:
Assistant
Cobra's lifecycle runs required-flag validation after
PersistentPreRunE, butFind(flag parsing, arg validation) runs before it — so wrapping at PreRun covers required flags but not parse-time errors. The correct seam isExecute(): wrap once between cobra andexitCode.Assistant
Now check how tests invoke commands — many call
rootCmd.Execute()directly, bypassing the new wrapping:Assistant
The wrapping must live inside cobra's per-command flow (required-flag validation happens mid-
execute, beforeRunE). Cleanest hook: a small middleware on each command'sRunE.Assistant
Double-closed. Reverting the over-eager regex:
Assistant
The required-flag error is untyped —
PersistentPreRunEisn't the seam (cobra runs only the innermost one, and any command defining its own shadows the root's). The root itself needs to be the wrapper:Assistant
PersistentPreRunEruns after required-flag validation in this cobra version — the wrap never fires. Let me find the real execution order:Assistant
usageArgsdoesn't fire either — test-global mutation from other tests in the package. Two places need the fix: makeusageArgsre-entrant (idempotent wrapper), and cover required-flag validation insideusageRunE(which runs after PreRun, before RunE — cobra's flag-hook seam can't reachValidateRequiredFlags, so the RunE wrap is the only one that covers everything).Assistant
Wait — the
errArgsWrappedsentinel is dead weight;errors.Asfor*usageErroralready makes it idempotent. Simplifying:Assistant
Now apply
usageRunEto every subcommand'sRunE:Assistant
My first regex captured through
RunE:itself. Fixing:Assistant
Sed-style regex on Go syntax is too error-prone. Resetting and doing this properly with
gofmt-driven AST rewrite:Assistant
Lost the main.go usageArgs + isCobraUsageError + root.go changes in the reset. Rather than more sed, let me do this properly with an AST-aware tool:
Assistant
All 25 command files rewritten, no unwrapped
RunE:left. Now re-apply the main.go/root.go changes the reset wiped:Assistant
rune.go survived the reset (untracked). Building and testing:
Assistant
ValidateRequiredFlagsreturns the untyped error beforeRunEever runs — my wrapper can't intercept it from inside. The only hook cobra fires before that validation isPreRunE. Moving the wrap there:Assistant
Wait — the validation now lives in the installed
PreRunE, but cobra callsPreRunEbeforeRunE, so installing it from inside theRunEwrapper is still too late. The installation must happen in the root'sPersistentPreRunE(which does run before required-flag validation):Assistant
Wait — I'm over-engineering.
ValidateRequiredFlagsruns afterPersistentPreRunEin cobra's execute — so installing in the root'sPersistentPreRunEand doing the validation there is the correct seam. ButusageRunEwrapping every command is now pointless. Simplify: rootPersistentPreRunEinstalls the flag-error func on the resolved command chain, and aPreRunEon the resolved command wraps the validation errors.- User
try again
Assistant
Let me reassess the current state and fix this cleanly. First, checking where things stand:
Assistant
The approach needs one correction. From cobra v1.10.2's
execute()(verified in the module source):ParseFlags(→FlagErrorFunc) →ValidateArgs→ inheritedPersistentPreRunE→PreRunE→ValidateRequiredFlags/ValidateFlagGroups→RunE. That gives three clean seams — noPreRunEmutation needed:- Parse errors:
rootCmd.SetFlagErrorFuncatinit()— cobra'sFlagErrorFunc()walks the parent chain, so setting it on root covers every subcommand's parse errors before they happen. - Arg-count errors:
usageArgs(already applied). - Required-flag/flag-group errors: call
ValidateRequiredFlags/ValidateFlagGroupsin root'sPersistentPreRunEand wrap — cobra re-runs them later, but they only fail once.
So
usageRunEis unnecessary — unwrapping it with the same AST tool:- Parse errors:
Assistant
Clean. Now rewrite rune.go with the correct mechanism and wire root.go:
Assistant
Package green. Full suite + staticcheck + live binary verification:
Assistant
All usage-error classes now exit 2 — structurally, not by message sniffing. Let me review the final diff and clean up before committing:
Assistant
Diff looks right. One spot-check of the AST rewrite output, then commit:
Assistant
All five findings fixed, committed on
fix/cli-review-v1(a27b9ab5), branched fromrelease/v1. Full suite green,go vetandstaticcheckclean.docs/plans/was left alone — pre-existing untracked files, not mine to commit.What shipped
Finding 1 — typed usage errors (the fragile part).
exitCodeno longer sniffs cobra's message strings for "required flag(s)", "accepts ", etc. Cobra's own errors are now wrapped as typedusageErrorat the three seams its execution order allows (verified against v1.10.2 source):