Skip to content

Commit

Permalink
refactor: clean up following code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Aug 28, 2023
1 parent 710f7d9 commit 5c8cb6f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 44 deletions.
8 changes: 2 additions & 6 deletions pkg/report/output/dataflow/formatter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/report/output/detectors/formatter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
23 changes: 14 additions & 9 deletions pkg/report/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions pkg/report/output/privacy/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/report/output/saas/formatter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions pkg/report/output/security/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/report/output/stats/formatter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 5c8cb6f

Please sign in to comment.