Skip to content

Commit

Permalink
feat(flag): early fail when the format is invalid (#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueskyson committed Jan 4, 2023
1 parent 9470e3c commit 60cf4fe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/flag/report_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
ConfigName: "format",
Shorthand: "f",
Value: report.FormatTable,
Usage: "format (table, json, sarif, template, cyclonedx, spdx, spdx-json, github, cosign-vuln)",
Usage: "format (" + strings.Join(report.SupportedFormats, ", ") + ")",
}
ReportFormatFlag = Flag{
Name: "report",
Expand Down Expand Up @@ -155,6 +155,10 @@ func (f *ReportFlagGroup) ToOptions(out io.Writer) (ReportOptions, error) {
listAllPkgs := getBool(f.ListAllPkgs)
output := getString(f.Output)

if format != "" && !slices.Contains(report.SupportedFormats, format) {
return ReportOptions{}, xerrors.Errorf("unknown format: %v", format)
}

if template != "" {
if format == "" {
log.Logger.Warn("'--template' is ignored because '--format template' is not specified. Use '--template' option with '--format template' option.")
Expand Down
4 changes: 4 additions & 0 deletions pkg/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const (
FormatCosignVuln = "cosign-vuln"
)

var (
SupportedFormats = []string{FormatTable, FormatJSON, FormatTemplate, FormatSarif, FormatCycloneDX, FormatSPDX, FormatSPDXJSON, FormatGitHub, FormatCosignVuln}
)

var (
SupportedSBOMFormats = []string{FormatCycloneDX, FormatSPDX, FormatSPDXJSON, FormatGitHub}
)
Expand Down

0 comments on commit 60cf4fe

Please sign in to comment.