Skip to content

Commit

Permalink
⚠️ remove CSV support (#1119)
Browse files Browse the repository at this point in the history
* remove CSV support

* fixes
  • Loading branch information
laurentsimon authored Oct 7, 2021
1 parent 8c2e123 commit f153db5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
22 changes: 16 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ var (
)

const (
formatCSV = "csv"
formatJSON = "json"
formatSarif = "sarif"
formatDefault = "default"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
36 changes: 0 additions & 36 deletions pkg/scorecard_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
package pkg

import (
"encoding/csv"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"

"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f153db5

Please sign in to comment.