Repository navigation
fix: reject unsupported output formats - #251
Conversation
andrew
left a comment
There was a problem hiding this comment.
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.
|
Updated. I removed the redundant help format switch/default branch and replaced the test helper with fmt.Sprintf. Verified with:
|
|
@sanmaxdev can you rebase and fix the conflict please |
2574f5b to
2b29c07
Compare
|
Rebased and resolved the Verified with:
|
|
Rechecked this on current main. The branch is already up to date and GitHub reports the merge state as clean now. Verification passed:
|
Summary
--formatvalues before command executionTesting
go test ./cmd -run 'TestUnsupportedFormatValuesAreRejected|TestHelpJSONRejectsUnsupportedFormat'go test ./cmdgo test ./...go build ./...go tool golangci-lint run ./...git diff --checkCloses #234