From f153db5a4ab307d879a02fec7927e1c8c957ba6c Mon Sep 17 00:00:00 2001 From: laurentsimon <64505099+laurentsimon@users.noreply.github.com> Date: Thu, 7 Oct 2021 13:54:21 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A0=EF=B8=8F=20=20remove=20CSV=20support?= =?UTF-8?q?=20(#1119)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * remove CSV support * fixes --- cmd/root.go | 22 ++++++++++++++++------ pkg/scorecard_result.go | 36 ------------------------------------ 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 30a76b08a19..967a69cbc7e 100644 --- a/cmd/root.go +++ b/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") diff --git a/pkg/scorecard_result.go b/pkg/scorecard_result.go index 64d2b1822ef..969336fc6ff 100644 --- a/pkg/scorecard_result.go +++ b/pkg/scorecard_result.go @@ -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