Skip to content

fix: reject unsupported output formats - #251

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
sanmaxdev:fix/validate-format-values
Jul 1, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
sanmaxdev:fix/validate-format-values

Conversation

@sanmaxdev

Copy link
Copy Markdown
Contributor

Summary

  • validate --format values before command execution
  • return supported format lists for text/json, csv, XML, SARIF, SQL, and markdown outputs
  • add regression coverage for unsupported format values

Testing

  • go test ./cmd -run 'TestUnsupportedFormatValuesAreRejected|TestHelpJSONRejectsUnsupportedFormat'
  • go test ./cmd
  • go test ./...
  • go build ./...
  • go tool golangci-lint run ./...
  • git diff --check

Closes #234

@andrew andrew 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 for this, it covers every --format flag in cmd/ and matches the error format from #234 exactly.

One cleanup before merge:

cmd/help.go:53-73 still has the old inline validation — case "text", "": and a default: branch returning unsupported help format %q. Both are now unreachable because getFormatFlag validates before the switch (and "" isn't in the allowed list). Since this PR centralises format validation, please drop the redundant branches so help.go matches the other commands, e.g.:

target, err := helpTargetCommand(root, args)
if err != nil {
    return err
}
if format == formatJSON {
    encoder := json.NewEncoder(cmd.OutOrStdout())
    encoder.SetIndent("", "  ")
    encoder.SetEscapeHTML(false)
    return encoder.Encode(buildHelpCommandDoc(target))
}
target.SetOut(cmd.OutOrStdout())
target.SetErr(cmd.ErrOrStderr())
return target.Help()

Optional nit: cmd/format_test.go's quote() helper reimplements %q — you could build the expected string with fmt.Sprintf("unsupported format %q; supported formats: %s", ...) and drop the helper. Not blocking.

@andrew andrew mentioned this pull request Jun 28, 2026
5 tasks done
@sanmaxdev

Copy link
Copy Markdown
Contributor Author

Updated. I removed the redundant help format switch/default branch and replaced the test helper with fmt.Sprintf.

Verified with:

  • go test ./cmd -run 'TestUnsupportedFormatValuesAreRejected|TestHelpJSONRejectsUnsupportedFormat'
  • go test ./cmd
  • go test ./...
  • go build ./...
  • git diff --check

@andrew

andrew commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@sanmaxdev can you rebase and fix the conflict please

@sanmaxdev
sanmaxdev force-pushed the fix/validate-format-values branch from 2574f5b to 2b29c07 Compare June 29, 2026 16:20
@sanmaxdev

Copy link
Copy Markdown
Contributor Author

Rebased and resolved the cmd/helpers.go conflict.

Verified with:

  • go test ./cmd -run 'TestUnsupportedFormatValuesAreRejected|TestHelpJSONRejectsUnsupportedFormat'\n- go test ./cmd\n- go test ./...\n- go build ./...\n- git diff --check

@sanmaxdev

Copy link
Copy Markdown
Contributor Author

Rechecked this on current main. The branch is already up to date and GitHub reports the merge state as clean now.

Verification passed:

  • go test -v -race ./cmd -run 'TestFormatFlagValidation|TestHelpJSONUnsupportedFormat'
  • go test -v -race ./...
  • go build -v ./...
  • go tool golangci-lint run ./...
  • git diff --check

@andrew
andrew merged commit a30ca52 into git-pkgs:main Jul 1, 2026
4 checks passed
@sanmaxdev
sanmaxdev deleted the fix/validate-format-values branch July 1, 2026 16:14
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.

Validate unsupported --format values

2 participants