mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-17 11:57:12 +03:00
parent
8c2e123155
commit
f153db5a4a
22
cmd/root.go
22
cmd/root.go
@ -55,7 +55,6 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
formatCSV = "csv"
|
||||
formatJSON = "json"
|
||||
formatSarif = "sarif"
|
||||
formatDefault = "default"
|
||||
@ -129,6 +128,15 @@ func getEnabledChecks(sp *spol.ScorecardPolicy, argsChecks []string) (checker.Ch
|
||||
return enabledChecks, nil
|
||||
}
|
||||
|
||||
func validateFormat(format string) bool {
|
||||
switch format {
|
||||
case "json", "sarif", "default":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: scorecardUse,
|
||||
Short: scorecardShort,
|
||||
@ -185,6 +193,11 @@ var rootCmd = &cobra.Command{
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Validate format.
|
||||
if !validateFormat(format) {
|
||||
log.Fatalf("unsupported format '%s'", format)
|
||||
}
|
||||
|
||||
enabledChecks, err := getEnabledChecks(policy, checksToRun)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -226,7 +239,6 @@ var rootCmd = &cobra.Command{
|
||||
fmt.Println("\nRESULTS\n-------")
|
||||
}
|
||||
|
||||
// UPGRADEv2: support CSV/JSON.
|
||||
// TODO: move the doc inside Scorecard structure.
|
||||
checkDocs, e := docs.Read()
|
||||
if e != nil {
|
||||
@ -240,14 +252,12 @@ var rootCmd = &cobra.Command{
|
||||
// TODO: support config files and update checker.MaxResultScore.
|
||||
err = repoResult.AsSARIF(showDetails, *logLevel, os.Stdout, checkDocs, policy,
|
||||
policyFile)
|
||||
case formatCSV:
|
||||
err = repoResult.AsCSV(showDetails, *logLevel, checkDocs, os.Stdout)
|
||||
case formatJSON:
|
||||
// UPGRADEv2: rename.
|
||||
err = repoResult.AsJSON2(showDetails, *logLevel, checkDocs, os.Stdout)
|
||||
default:
|
||||
err = sce.WithMessage(sce.ErrScorecardInternal,
|
||||
fmt.Sprintf("invalid format flag: %v. Expected [default, csv, json]", format))
|
||||
fmt.Sprintf("invalid format flag: %v. Expected [default, json]", format))
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to output results: %v", err)
|
||||
@ -390,7 +400,7 @@ func init() {
|
||||
&rubygems, "rubygems", "",
|
||||
"rubygems package to check, given that the rubygems package has a GitHub repository")
|
||||
rootCmd.Flags().StringVar(&format, "format", formatDefault,
|
||||
"output format. allowed values are [default, sarif, html, json, csv]")
|
||||
"output format. allowed values are [default, sarif, json]")
|
||||
rootCmd.Flags().StringSliceVar(
|
||||
&metaData, "metadata", []string{}, "metadata for the project. It can be multiple separated by commas")
|
||||
rootCmd.Flags().BoolVar(&showDetails, "show-details", false, "show extra details about each check")
|
||||
|
@ -15,12 +15,9 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
@ -59,39 +56,6 @@ func scoreToString(s float64) string {
|
||||
return fmt.Sprintf("%.1f", s)
|
||||
}
|
||||
|
||||
// AsCSV outputs ScorecardResult in CSV format.
|
||||
func (r *ScorecardResult) AsCSV(showDetails bool, logLevel zapcore.Level,
|
||||
checkDocs docs.Doc, writer io.Writer) error {
|
||||
score, err := r.GetAggregateScore(checkDocs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w := csv.NewWriter(writer)
|
||||
record := []string{r.Repo.Name, scoreToString(score)}
|
||||
columns := []string{"Repository", "AggScore"}
|
||||
|
||||
// UPGRADEv2: remove nolint after ugrade.
|
||||
//nolint
|
||||
for _, checkResult := range r.Checks {
|
||||
columns = append(columns, checkResult.Name+"_Pass", checkResult.Name+"_Confidence")
|
||||
record = append(record, strconv.FormatBool(checkResult.Pass),
|
||||
strconv.Itoa(checkResult.Confidence))
|
||||
if showDetails {
|
||||
columns = append(columns, checkResult.Name+"_Details")
|
||||
record = append(record, checkResult.Details...)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(writer, "%s\n", strings.Join(columns, ","))
|
||||
if err := w.Write(record); err != nil {
|
||||
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("csv.Write: %v", err))
|
||||
}
|
||||
w.Flush()
|
||||
if err := w.Error(); err != nil {
|
||||
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("csv.Flush: %v", err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAggregateScore returns the aggregate score.
|
||||
func (r *ScorecardResult) GetAggregateScore(checkDocs docs.Doc) (float64, error) {
|
||||
// TODO: calculate the score and make it a field
|
||||
|
Loading…
Reference in New Issue
Block a user