diff --git a/pkg/report/output/dataflow/formatter.go b/pkg/report/output/dataflow/formatter.go index afbb7f26a..cc85a70c7 100644 --- a/pkg/report/output/dataflow/formatter.go +++ b/pkg/report/output/dataflow/formatter.go @@ -1,8 +1,6 @@ package dataflow import ( - "fmt" - "github.com/bearer/bearer/pkg/commands/process/settings" "github.com/bearer/bearer/pkg/flag" outputtypes "github.com/bearer/bearer/pkg/report/output/types" @@ -24,11 +22,9 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F func (f Formatter) Format(format string) (output *string, err error) { switch format { case flag.FormatEmpty, flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.Dataflow) + return outputhandler.ReportJSON(f.ReportData.Dataflow) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.Dataflow) - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) + return outputhandler.ReportYAML(f.ReportData.Dataflow) } return output, err diff --git a/pkg/report/output/detectors/formatter.go b/pkg/report/output/detectors/formatter.go index 96f2aae66..b7d54d540 100644 --- a/pkg/report/output/detectors/formatter.go +++ b/pkg/report/output/detectors/formatter.go @@ -1,8 +1,6 @@ package detectors import ( - "fmt" - "github.com/bearer/bearer/pkg/commands/process/settings" "github.com/bearer/bearer/pkg/flag" outputtypes "github.com/bearer/bearer/pkg/report/output/types" @@ -24,11 +22,9 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F func (f Formatter) Format(format string) (output *string, err error) { switch format { case flag.FormatEmpty, flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.Detectors) + return outputhandler.ReportJSON(f.ReportData.Detectors) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.Detectors) - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) + return outputhandler.ReportYAML(f.ReportData.Detectors) } return output, err diff --git a/pkg/report/output/output.go b/pkg/report/output/output.go index 32426a5dc..ffc0d2b0d 100644 --- a/pkg/report/output/output.go +++ b/pkg/report/output/output.go @@ -34,22 +34,19 @@ func GetData( data := &types.ReportData{} // add detectors err := detectors.AddReportData(data, report, config) - if config.Report.Report == flag.ReportDetectors { + if config.Report.Report == flag.ReportDetectors || err != nil { return data, err } - if config.Report.Report == flag.ReportDataFlow { - err = GetDataflow(data, report, config, false) - return data, err - } - - // add dataflow to data for internal use - if err = GetDataflow(data, report, config, true); err != nil { + // add dataflow to data + if err = GetDataflow(data, report, config, config.Report.Report == flag.ReportDataFlow); err != nil { return data, err } // add report-specific items switch config.Report.Report { + case flag.ReportDataFlow: + return data, err case flag.ReportSecurity: sendToCloud = true err = security.AddReportData(data, config, baseBranchFindings) @@ -113,5 +110,13 @@ func FormatOutput( return nil, fmt.Errorf(`--report flag "%s" is not supported`, config.Report.Report) } - return formatter.Format(config.Report.Format) + formatStr, err := formatter.Format(config.Report.Format) + if err != nil { + return formatStr, err + } + if formatStr == nil { + return nil, fmt.Errorf(`--report flag "%s" does not support --format flag "%s"`, config.Report.Report, config.Report.Format) + } + + return formatStr, err } diff --git a/pkg/report/output/privacy/formatter.go b/pkg/report/output/privacy/formatter.go index 3306bc932..71fa6fe8f 100644 --- a/pkg/report/output/privacy/formatter.go +++ b/pkg/report/output/privacy/formatter.go @@ -32,9 +32,9 @@ func (f Formatter) Format(format string) (output *string, err error) { csvStr := stringBuilder.String() output = &csvStr case flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.PrivacyReport) + return outputhandler.ReportJSON(f.ReportData.PrivacyReport) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.PrivacyReport) + return outputhandler.ReportYAML(f.ReportData.PrivacyReport) case flag.FormatHTML: title := "Privacy Report" body, err := html.ReportPrivacyHTML(f.ReportData.PrivacyReport) @@ -46,8 +46,6 @@ func (f Formatter) Format(format string) (output *string, err error) { if err != nil { return output, fmt.Errorf("could not generate html page %s", err) } - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) } return output, err diff --git a/pkg/report/output/saas/formatter.go b/pkg/report/output/saas/formatter.go index 75604d223..4da616db7 100644 --- a/pkg/report/output/saas/formatter.go +++ b/pkg/report/output/saas/formatter.go @@ -1,8 +1,6 @@ package saas import ( - "fmt" - "github.com/bearer/bearer/pkg/commands/process/settings" "github.com/bearer/bearer/pkg/flag" outputtypes "github.com/bearer/bearer/pkg/report/output/types" @@ -24,11 +22,9 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F func (f Formatter) Format(format string) (output *string, err error) { switch format { case flag.FormatEmpty, flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.SaasReport) + return outputhandler.ReportJSON(f.ReportData.SaasReport) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.SaasReport) - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) + return outputhandler.ReportYAML(f.ReportData.SaasReport) } return output, err diff --git a/pkg/report/output/security/formatter.go b/pkg/report/output/security/formatter.go index de31665df..86de47ab2 100644 --- a/pkg/report/output/security/formatter.go +++ b/pkg/report/output/security/formatter.go @@ -43,23 +43,23 @@ func (f Formatter) Format(format string) (output *string, err error) { if sarifErr != nil { return output, fmt.Errorf("error generating sarif report %s", sarifErr) } - output, err = outputhandler.ReportJSON(sarifContent) + return outputhandler.ReportJSON(sarifContent) case flag.FormatReviewDog: sastContent, reviewdogErr := reviewdog.ReportReviewdog(f.ReportData.FindingsBySeverity) if reviewdogErr != nil { return output, fmt.Errorf("error generating reviewdog report %s", reviewdogErr) } - output, err = outputhandler.ReportJSON(sastContent) + return outputhandler.ReportJSON(sastContent) case flag.FormatGitLabSast: sastContent, sastErr := gitlab.ReportGitLab(f.ReportData.FindingsBySeverity, f.StartTime, f.EndTime) if sastErr != nil { return output, fmt.Errorf("error generating gitlab-sast report %s", sastErr) } - output, err = outputhandler.ReportJSON(sastContent) + return outputhandler.ReportJSON(sastContent) case flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.FindingsBySeverity) + return outputhandler.ReportJSON(f.ReportData.FindingsBySeverity) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.FindingsBySeverity) + return outputhandler.ReportYAML(f.ReportData.FindingsBySeverity) case flag.FormatHTML: title := "Security Report" body, securityErr := html.ReportSecurityHTML(f.ReportData.FindingsBySeverity) @@ -71,8 +71,6 @@ func (f Formatter) Format(format string) (output *string, err error) { if err != nil { err = fmt.Errorf("could not generate html page %s", err) } - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) } return output, err diff --git a/pkg/report/output/stats/formatter.go b/pkg/report/output/stats/formatter.go index 948840932..e7d315d5c 100644 --- a/pkg/report/output/stats/formatter.go +++ b/pkg/report/output/stats/formatter.go @@ -1,8 +1,6 @@ package stats import ( - "fmt" - "github.com/bearer/bearer/pkg/commands/process/settings" "github.com/bearer/bearer/pkg/flag" outputtypes "github.com/bearer/bearer/pkg/report/output/types" @@ -24,11 +22,9 @@ func NewFormatter(reportData *outputtypes.ReportData, config settings.Config) *F func (f Formatter) Format(format string) (output *string, err error) { switch format { case flag.FormatEmpty, flag.FormatJSON: - output, err = outputhandler.ReportJSON(f.ReportData.Stats) + return outputhandler.ReportJSON(f.ReportData.Stats) case flag.FormatYAML: - output, err = outputhandler.ReportYAML(f.ReportData.Stats) - default: - err = fmt.Errorf(`--report flag "%s" is not supported`, f.Config.Report.Report) + return outputhandler.ReportYAML(f.ReportData.Stats) } return output, err