-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(flag): early fail when the format is invalid #3370
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I left some comments.
pkg/report/writer.go
Outdated
switch format { | ||
case FormatTable, FormatJSON, FormatGitHub, FormatCycloneDX, FormatSPDX, FormatSPDXJSON, FormatTemplate, FormatSarif, FormatCosignVuln: | ||
return nil | ||
default: | ||
return xerrors.Errorf("unknown format: %v", format) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding SupportedFormats
may be better.
Lines 35 to 37 in 3daf3df
var ( | |
SupportedSBOMFormats = []string{FormatCycloneDX, FormatSPDX, FormatSPDXJSON, FormatGitHub} | |
) |
switch format { | |
case FormatTable, FormatJSON, FormatGitHub, FormatCycloneDX, FormatSPDX, FormatSPDXJSON, FormatTemplate, FormatSarif, FormatCosignVuln: | |
return nil | |
default: | |
return xerrors.Errorf("unknown format: %v", format) | |
} | |
return slices.Contains(SupportedFormats, format) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it would be great to replace these values with SupportedFormats
.
trivy/pkg/flag/report_flags.go
Line 29 in a3eece4
Usage: "format (table, json, sarif, template, cyclonedx, spdx, spdx-json, github, cosign-vuln)", |
pkg/flag/report_flags.go
Outdated
@@ -155,6 +155,10 @@ func (f *ReportFlagGroup) ToOptions(out io.Writer) (ReportOptions, error) { | |||
listAllPkgs := getBool(f.ListAllPkgs) | |||
output := getString(f.Output) | |||
|
|||
if err := report.ValidateFormat(format); err != nil { | |||
return ReportOptions{}, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return ReportOptions{}, err | |
return ReportOptions{}, xerrors.Errorf("unknown format: %v", format) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@knqyf263 Thanks for your suggestions. I also thought replacing independent strings with something like SupportedSBOMFormats
is a better solution. Shall I replace the original strings with the slice or I just add the slice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace the original strings with the slice
What does it look like? I thought we would add the slice, but I would like to hear another approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still learning Golang, I wrongly thought that I can treat Golang's slice as Cshap's enum which can be used in switch-case or test if a value belongs to one of the enums. Anyway, I finally implemented your suggestion.
if format != "" && !slices.Contains(report.SupportedFormats, format) { | ||
return ReportOptions{}, xerrors.Errorf("unknown format: %v", format) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the unit tests in /pkg/flag/report_flags_test.go
use ""
as their format, so I make a wild card for ""
.
Thanks! |
Description
This PR will make trivy fail earlier when the format is wrong.
Related issues
Before:
After:
Checklist