Skip to content

Commit

Permalink
Support for guessing the file type
Browse files Browse the repository at this point in the history
  • Loading branch information
justinabrahms committed Nov 23, 2022
1 parent c7c5922 commit 712e766
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions cmd/sbom-scorecard/cmd/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"errors"

"strings"

"github.com/spf13/cobra"
"opensource.ebay.com/sbom-scorecard/pkg/cdx"
"opensource.ebay.com/sbom-scorecard/pkg/scorecard"
Expand All @@ -25,11 +28,23 @@ type options struct {
}

func init() {
scoreCmd.PersistentFlags().StringVar(&flags.sbomType, "sbomtype", "spdx", "type of sbom being evaluated")
scoreCmd.PersistentFlags().StringVar(&flags.sbomType, "sbomtype", "guess", "type of sbom being evaluated")
scoreCmd.PersistentFlags().StringVar(&flags.outputFormat, "outputFormat", "text", "format to output")
_ = scoreCmd.MarkPersistentFlagRequired("sbomType")
}

func determineSbomType(filepath string) string {
content, err := ioutil.ReadFile(filepath)
if err != nil {
panic(fmt.Sprintf("Error! %v", err))
}

if strings.Contains(strings.ToLower(string(content)), "spdx") {
return "spdx"
}
return "cdx"
}

var scoreCmd = &cobra.Command{
Use: "score",
Short: "score evaluates the SBOM being passed and outputs based on the composition and completion",
Expand All @@ -42,6 +57,12 @@ var scoreCmd = &cobra.Command{
}

var r scorecard.SbomReport

if opts.sbomType == "guess" {
opts.sbomType = determineSbomType(opts.path)
print("Guessed: " + opts.sbomType + "\n")
}

switch opts.sbomType {
case "spdx":
r = spdx.GetSpdxReport(opts.path)
Expand All @@ -62,7 +83,9 @@ var scoreCmd = &cobra.Command{
func validateFlags(args []string) (options, error) {
var opts options
opts.sbomType = flags.sbomType
if flags.sbomType != "spdx" && flags.sbomType != "cdx" {
if flags.sbomType != "spdx" &&
flags.sbomType != "cdx" &&
flags.sbomType != "guess" {
return opts, errors.New(fmt.Sprintf("Unknown sbomType %s", flags.sbomType))
}

Expand Down

0 comments on commit 712e766

Please sign in to comment.