diff --git a/cmd/internal/scdiff/app/format/format.go b/cmd/internal/scdiff/app/format/format.go index 34067b2459c..876460631ab 100644 --- a/cmd/internal/scdiff/app/format/format.go +++ b/cmd/internal/scdiff/app/format/format.go @@ -56,7 +56,7 @@ func JSON(r *pkg.ScorecardResult, w io.Writer) error { return err } Normalize(r) - o := pkg.AsJSON2ResultOption{ + o := &pkg.AsJSON2ResultOption{ Details: details, LogLevel: logLevel, } diff --git a/pkg/json.go b/pkg/json.go index 75539f3618e..d86ed3b4ea6 100644 --- a/pkg/json.go +++ b/pkg/json.go @@ -129,9 +129,14 @@ func (r *ScorecardResult) AsJSON(showDetails bool, logLevel log.Level, writer io } // AsJSON2 exports results as JSON for new detail format. -func (r *ScorecardResult) AsJSON2(writer io.Writer, - checkDocs docs.Doc, o AsJSON2ResultOption, -) error { +func (r *ScorecardResult) AsJSON2(writer io.Writer, checkDocs docs.Doc, opt *AsJSON2ResultOption) error { + if opt == nil { + opt = &AsJSON2ResultOption{ + LogLevel: log.DefaultLevel, + Details: false, + Annotations: false, + } + } score, err := r.GetAggregateScore(checkDocs) if err != nil { return err @@ -170,17 +175,17 @@ func (r *ScorecardResult) AsJSON2(writer io.Writer, Reason: checkResult.Reason, Score: checkResult.Score, } - if o.Details { + if opt.Details { for i := range checkResult.Details { d := checkResult.Details[i] - m := DetailToString(&d, o.LogLevel) + m := DetailToString(&d, opt.LogLevel) if m == "" { continue } tmpResult.Details = append(tmpResult.Details, m) } } - if o.Annotations { + if opt.Annotations { exempted, reasons := checkResult.IsExempted(r.Config) if exempted { tmpResult.Annotations = reasons @@ -243,9 +248,3 @@ func ExperimentalFromJSON2(r io.Reader) (result ScorecardResult, score float64, return sr, float64(jsr.AggregateScore), nil } - -func (r *ScorecardResult) AsFJSON(showDetails bool, - logLevel log.Level, checkDocs docs.Doc, writer io.Writer, -) error { - return sce.WithMessage(sce.ErrScorecardInternal, "WIP: not supported") -} diff --git a/pkg/json_test.go b/pkg/json_test.go index 759df15a9da..f5439269db0 100644 --- a/pkg/json_test.go +++ b/pkg/json_test.go @@ -499,7 +499,7 @@ func TestJSONOutput(t *testing.T) { } var result bytes.Buffer - o := AsJSON2ResultOption{ + o := &AsJSON2ResultOption{ Details: tt.showDetails, LogLevel: tt.logLevel, Annotations: tt.showAnnotations, diff --git a/pkg/scorecard_result.go b/pkg/scorecard_result.go index 27e9f0409ad..3cf90f54a11 100644 --- a/pkg/scorecard_result.go +++ b/pkg/scorecard_result.go @@ -140,7 +140,7 @@ func FormatResults( switch opts.Format { case options.FormatDefault: - o := AsStringResultOption{ + o := &AsStringResultOption{ Details: opts.ShowDetails, Annotations: opts.ShowAnnotations, LogLevel: log.ParseLevel(opts.LogLevel), @@ -150,7 +150,7 @@ func FormatResults( // TODO: support config files and update checker.MaxResultScore. err = results.AsSARIF(opts.ShowDetails, log.ParseLevel(opts.LogLevel), output, doc, policy, opts) case options.FormatJSON: - o := AsJSON2ResultOption{ + o := &AsJSON2ResultOption{ Details: opts.ShowDetails, Annotations: opts.ShowAnnotations, LogLevel: log.ParseLevel(opts.LogLevel), @@ -179,9 +179,15 @@ func FormatResults( } // AsString returns ScorecardResult in string format. -func (r *ScorecardResult) AsString(writer io.Writer, - checkDocs docChecks.Doc, o AsStringResultOption, -) error { +func (r *ScorecardResult) AsString(writer io.Writer, checkDocs docChecks.Doc, opt *AsStringResultOption) error { + if opt == nil { + opt = &AsStringResultOption{ + LogLevel: log.DefaultLevel, + Details: false, + Annotations: false, + } + } + data := make([][]string, len(r.Checks)) for i, row := range r.Checks { @@ -201,14 +207,14 @@ func (r *ScorecardResult) AsString(writer io.Writer, doc := cdoc.GetDocumentationURL(r.Scorecard.CommitSHA) x = append(x, row.Name, row.Reason) - if o.Details { - details, show := detailsToString(row.Details, o.LogLevel) + if opt.Details { + details, show := detailsToString(row.Details, opt.LogLevel) if show { x = append(x, details) } } x = append(x, doc) - if o.Annotations { + if opt.Annotations { _, reasons := row.IsExempted(r.Config) x = append(x, strings.Join(reasons, "\n")) } @@ -228,11 +234,11 @@ func (r *ScorecardResult) AsString(writer io.Writer, table := tablewriter.NewWriter(writer) header := []string{"Score", "Name", "Reason"} - if o.Details { + if opt.Details { header = append(header, "Details") } header = append(header, "Documentation/Remediation") - if o.Annotations { + if opt.Annotations { header = append(header, "Annotation") } table.SetHeader(header)