Skip to content

Commit

Permalink
change flag to file-mode
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <[email protected]>
  • Loading branch information
spencerschrock committed Jan 8, 2025
1 parent 0db4245 commit 50be4b6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
6 changes: 3 additions & 3 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
15 changes: 8 additions & 7 deletions options/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, ", ")),
)
}
43 changes: 30 additions & 13 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
}
10 changes: 5 additions & 5 deletions pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 50be4b6

Please sign in to comment.