diff --git a/clients/githubrepo/client.go b/clients/githubrepo/client.go index 1c7782bb091..1298924c088 100644 --- a/clients/githubrepo/client.go +++ b/clients/githubrepo/client.go @@ -67,8 +67,8 @@ type Client struct { gitMode bool } -// WithGitMode configures the repo client to fetch files using git. -func WithGitMode() Option { +// WithFileModeGit configures the repo client to fetch files using git. +func WithFileModeGit() Option { return func(c *repoClientConfig) error { c.gitMode = true return nil @@ -263,7 +263,7 @@ func (client *Client) GetOrgRepoClient(ctx context.Context) (clients.RepoClient, options := []Option{WithRoundTripper(client.repoClient.Client().Transport)} if client.gitMode { - options = append(options, WithGitMode()) + options = append(options, WithFileModeGit()) } c, err := NewRepoClient(ctx, options...) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index a4e27cfea29..23299e03edb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -154,8 +154,8 @@ func rootCmd(o *options.Options) error { scorecard.WithProbes(enabledProbes), scorecard.WithChecks(checks), } - if o.GitMode { - opts = append(opts, scorecard.WithGitMode()) + if strings.EqualFold(o.FileMode, options.FileModeGit) { + opts = append(opts, scorecard.WithFileModeGit()) } repoResult, err = scorecard.Run(ctx, repo, opts...) diff --git a/options/flags.go b/options/flags.go index 7f35e6e49c5..7ad94116c23 100644 --- a/options/flags.go +++ b/options/flags.go @@ -54,8 +54,8 @@ const ( // FlagShowDetails is the flag name for outputting additional check info. FlagShowDetails = "show-details" - // Flag FlagGitMode is the flag name for enabling git compatibility mode. - FlagGitMode = "git-mode" + // Flag FlagFileMode is the flag name for specifying how files are fetched for a repository. + FlagFileMode = "file-mode" // FlagShowAnnotations is the flag name for outputting annotations on checks. FlagShowAnnotations = "show-annotations" @@ -226,10 +226,11 @@ func (o *Options) AddFlags(cmd *cobra.Command) { "output file", ) - cmd.Flags().BoolVar( - &o.GitMode, - FlagGitMode, - o.GitMode, - "fetch repository files using git for maximum compatibility", + allowedModes := []string{FileModeArchive, FileModeGit} + cmd.Flags().StringVar( + &o.FileMode, + FlagFileMode, + o.FileMode, + fmt.Sprintf("mode to fetch repository files: %s", strings.Join(allowedModes, ", ")), ) } diff --git a/options/options.go b/options/options.go index 086257fcd69..5a646886735 100644 --- a/options/options.go +++ b/options/options.go @@ -41,13 +41,13 @@ type Options struct { Nuget string PolicyFile string ResultsFile string + FileMode string ChecksToRun []string ProbesToRun []string Metadata []string CommitDepth int ShowDetails bool ShowAnnotations bool - GitMode bool // Feature flags. EnableSarif bool `env:"ENABLE_SARIF"` EnableScorecardV6 bool `env:"SCORECARD_V6"` @@ -56,21 +56,15 @@ type Options struct { // New creates a new instance of `Options`. func New() *Options { - opts := &Options{} + opts := &Options{ + Commit: DefaultCommit, + Format: FormatDefault, + LogLevel: DefaultLogLevel, + FileMode: FileModeArchive, + } if err := env.Parse(opts); err != nil { log.Printf("could not parse env vars, using default options: %v", err) } - // Defaulting. - // TODO(options): Consider moving this to a separate function/method. - if opts.Commit == "" { - opts.Commit = DefaultCommit - } - if opts.Format == "" { - opts.Format = FormatDefault - } - if opts.LogLevel == "" { - opts.LogLevel = DefaultLogLevel - } return opts } @@ -90,6 +84,12 @@ const ( // FormatRaw specifies that results should be output in raw format. FormatRaw = "raw" + // File Modes + // FileModeGit specifies that files should be fetched using git. + FileModeGit = "git" + // FileModeArchive specifies that files should be fetched using the export archive (tarball). + FileModeArchive = "archive" + // Environment variables. // EnvVarEnableSarif is the environment variable which controls enabling // SARIF logging. @@ -108,6 +108,7 @@ var ( errCommitIsEmpty = errors.New("commit should be non-empty") errFormatNotSupported = errors.New("unsupported format") + errFileModeNotSupported = errors.New("unsupported file mode") errPolicyFileNotSupported = errors.New("policy file is not supported yet") errRawOptionNotSupported = errors.New("raw option is not supported yet") errRepoOptionMustBeSet = errors.New( @@ -177,6 +178,13 @@ func (o *Options) Validate() error { ) } + if !validateFileMode(o.FileMode) { + errs = append( + errs, + errFileModeNotSupported, + ) + } + if len(errs) != 0 { return fmt.Errorf( "%w: %+v", @@ -253,3 +261,12 @@ func validateFormat(format string) bool { return false } } + +func validateFileMode(mode string) bool { + switch strings.ToLower(mode) { + case FileModeGit, FileModeArchive: + return true + default: + return false + } +} diff --git a/pkg/scorecard/scorecard.go b/pkg/scorecard/scorecard.go index a3ae568be4c..007d0671128 100644 --- a/pkg/scorecard/scorecard.go +++ b/pkg/scorecard/scorecard.go @@ -341,12 +341,12 @@ func WithOpenSSFBestPraticesClient(client clients.CIIBestPracticesClient) Option } } -// WithGitMode will configure supporting repository clients to download files -// using git, instead of the archive tarball. This is useful for repositories -// which "export-ignore" files in a .gitattributes file. +// WithFileModeGit will configure supporting repository clients to download files +// using git. This is useful for repositories which "export-ignore" files in its +// .gitattributes file. // // Repository analysis may be slower. -func WithGitMode() Option { +func WithFileModeGit() Option { return func(c *runConfig) error { c.gitMode = true return nil @@ -392,7 +392,7 @@ func Run(ctx context.Context, repo clients.Repo, opts ...Option) (Result, error) if c.client == nil { var opts []githubrepo.Option if c.gitMode { - opts = append(opts, githubrepo.WithGitMode()) + opts = append(opts, githubrepo.WithFileModeGit()) } client, err := githubrepo.NewRepoClient(ctx, opts...) if err != nil {