Skip to content

fix: reject unsupported values for --output - #608

Merged
fulviodenza merged 5 commits into
civo:masterfrom
Aubaid12:fix/398-validate-output-format
Nov 2, 2026
Merged

fulviodenza merged 5 commits into
civo:masterfrom
Aubaid12:fix/398-validate-output-format

Conversation

@Aubaid12

Copy link
Copy Markdown
Contributor

Part of #398

Problem

-o takes any string, and anything that isn't json or custom falls through to the table output, so a typo like civo kubernetes ls -o lefafa just prints the normal cluster table with no warning.

Fix

  • --output is now a typed flag (common.OutputFormatFlag) that only accepts json, human or custom. A bad value fails while cobra parses the flags, so it errors before the config is read or any API call is made, and no subcommand hook can skip it.
  • The valid formats live in one list, common.OutputFormats, which also builds the help text. The help line is unchanged, so the copy in the README still matches.
  • Matching is case-sensitive, so -o JSON is rejected too instead of silently printing the table.
$ civo kubernetes ls -o lefafa
Error: invalid argument "lefafa" for "-o, --output" flag: must be one of json, human, custom

Tests

  • TestOutputFormatFlag in common/common_test.go
  • TestOutputFlagRejectsUnsupportedFormats in cmd/root_test.go, which checks the root command actually uses the typed flag
  • go test ./... and golangci-lint pass locally

I'll send the --field check and the kubernetes create name handling as separate PRs, as discussed in the issue.

Aubaid12 and others added 2 commits October 20, 2026 05:18
--output was a plain string flag, so any value was accepted and the output code fell back to the human table. Make it a typed flag that only accepts json, human or custom, so a bad value fails while flags are parsed, before the config is loaded or any API call is made.

Part of civo#398

@fulviodenza fulviodenza 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.

Thanks, this looks good. I checked that nothing that worked before breaks: across the codebase common.OutputFormat is only ever compared against json, human and custom, no subcommand defines its own -o, and every -o usage in the README/docs is json or custom. Values that are now rejected (JSON, yaml, table, empty) previously fell through silently to the table output, so erroring is the right behaviour. Help output still reads -o, --output string ... (default "human").

A couple of small things:

  1. Test depends on list order (common/common_test.go, TestOutputFormatFlag): the rejection loop asserts format == OutputFormatCustom, which only holds because custom happens to be the last entry in OutputFormats. Capturing the value before the loop (e.g. before := format) and asserting it is unchanged would make the test robust to reordering.
  2. Heads up on overlap with #599: both PRs touch the common/common.go import block and append to the end of common/common_test.go, so whichever lands second will need a rebase.
  3. Optional: registering completion for the flag (RegisterFlagCompletionFunc("output", ...) returning OutputFormats) would be a nice touch, not needed for this PR.

I've approved the CI run; happy to approve once it is green and (1) is addressed.

@Aubaid12

Aubaid12 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@fulviodenza Thanks for the review! Pushed the test fix, it saves the value before the loop now and checks it stays the same. I'll rebase if #599 goes in first. Left completion out for now, I can do it in a separate PR.

@fulviodenza

Copy link
Copy Markdown
Contributor

Awesome, LGTM 🚀

@fulviodenza
fulviodenza merged commit b60d815 into civo:master Nov 2, 2026
3 checks passed
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.

2 participants