fix: return non-zero exit for unrecognized commands - #1051
Conversation
|
Claude finished @mbevc1's task in 2m 36s —— View job Re-review after commit
|
|
Addressed the review in 68c1089: 🔴 High — probe corrupted the package-level 🟡 Medium — normalization mismatch. The probe now resolves 🟢 Tests. Added Verified with a real binary (the sandbox does allow execution here):
The duplication between |
Cobra's `TraverseChildren` routing (set on the root command) hands an unrecognized command token to the nearest group command with no error; that non-runnable command then prints help and returns nil, so the CLI exits 0. A typo'd command such as `kosli garbage list flows` therefore silently reports success (issue #1043). Add `findUnknownCommand`, called from `innerMain` before `ExecuteC`. It resolves the args with cobra's `Traverse` on a throwaway command tree (so the real command's flag state is untouched) and returns an error when resolution lands on a non-runnable group command while an unconsumed non-flag token that names no subcommand remains. Real commands, group commands invoked with no leftover (e.g. `kosli list`), and the existing unknown-flag path are all left unchanged.
Address review feedback on the unknown-command detection: - `findUnknownCommand` built its probe with `newRootCmd`, which reassigns the package-level `global` and rebinds the real command's persistent-flag pointers to a throwaway struct. Nothing restored it, so after the probe ran every global.* read (ApiToken, Host, ...) saw the throwaway defaults and real invocations silently ignored their global flags. Save and restore `global` around the probe. - The probe resolved the raw args while `innerMain` executes normalized args, so a space-form bool flag on a group command (`kosli list --debug false`) had its value treated as a leftover positional and was wrongly reported as `unknown command: false`. Normalize the probe args with normalizeBoolFlagArgs too. Add `TestInnerMainPreservesGlobalAfterProbe` (drives a successful real command with global flags through `innerMain` and asserts the values survive) and a space-form-bool negative case to TestFindUnknownCommand. Both fail without the corresponding fix.
68c1089 to
d615c32
Compare
Cobra's
TraverseChildrenrouting (set on the root command) hands an unrecognized command token to the nearest group command with no error; that non-runnable command then prints help and returns nil, so the CLI exits 0. A typo'd command such askosli garbage list flowstherefore silently reports success (issue #1043).Add
findUnknownCommand, called frominnerMainbeforeExecuteC. It resolves the args with cobra'sTraverseon a throwaway command tree (so the real command's flag state is untouched) and returns an error when resolution lands on a non-runnable group command while an unconsumed non-flag token that names no subcommand remains. Real commands, group commands invoked with no leftover (e.g.kosli list), and the existing unknown-flag path are all left unchanged.Example output:
Closes #1043
Checklist
charts/k8s-reporter/) updated, if needed. Note: these changes live in a separate PR