Skip to content

Commit

Permalink
feat: add --quiet option (#134)
Browse files Browse the repository at this point in the history
Signed-off-by: becojo <[email protected]>
Co-authored-by: Becojo <[email protected]>
  • Loading branch information
becojo and becojo authored Jun 21, 2024
1 parent d5a9e06 commit ef581f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
46 changes: 24 additions & 22 deletions analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)

log.Debug().Msgf("Starting repository analysis for organization: %s on %s", org, provider)
bar := progressbar.NewOptions(
0,
progressbar.OptionSetDescription("Analyzing repositories"),
progressbar.OptionShowCount(),
progressbar.OptionSetWriter(os.Stderr),
)
bar := a.progressBar(0, "Analyzing repositories")

var wg sync.WaitGroup
errChan := make(chan error, 1)
Expand Down Expand Up @@ -162,6 +157,8 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
}
}

_ = bar.Finish()

return a.finalizeAnalysis(ctx, inventory)
}

Expand All @@ -187,19 +184,18 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)

log.Debug().Msgf("Starting repository analysis for: %s/%s on %s", org, repoName, provider)
bar := progressbar.NewOptions(
1,
progressbar.OptionSetDescription("Analyzing repository"),
progressbar.OptionShowCount(),
progressbar.OptionSetWriter(os.Stderr),
)
bar := a.progressBar(2, "Cloning repository")
_ = bar.RenderBlank()

tempDir, err := a.cloneRepoToTemp(ctx, repo.BuildGitURL(a.ScmClient.GetProviderBaseURL()), a.ScmClient.GetToken(), ref)
if err != nil {
return err
}
defer os.RemoveAll(tempDir)

bar.Describe("Analyzing repository")
_ = bar.Add(1)

pkg, err := a.generatePackageInsights(ctx, tempDir, repo, ref)
if err != nil {
return err
Expand All @@ -209,9 +205,8 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
if err != nil {
return err
}
_ = bar.Add(1)
_ = bar.Finish()

fmt.Print("\n\n")
return a.finalizeAnalysis(ctx, inventory)
}

Expand All @@ -237,12 +232,6 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)

log.Debug().Msgf("Starting repository analysis for: %s/%s on %s", org, repoName, provider)
bar := progressbar.NewOptions(
1,
progressbar.OptionSetDescription("Analyzing repository"),
progressbar.OptionShowCount(),
progressbar.OptionSetWriter(os.Stderr),
)

pkg, err := a.generatePackageInsights(ctx, repoPath, repo, "")
if err != nil {
Expand All @@ -253,9 +242,7 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
if err != nil {
return err
}
_ = bar.Add(1)

fmt.Print("\n\n")
return a.finalizeAnalysis(ctx, inventory)
}

Expand Down Expand Up @@ -325,3 +312,18 @@ func (a *Analyzer) cloneRepoToTemp(ctx context.Context, gitURL string, token str
}
return tempDir, nil
}

func (a *Analyzer) progressBar(max int64, description string) *progressbar.ProgressBar {
if a.Config.Quiet {
return progressbar.DefaultSilent(max, description)
} else {
return progressbar.NewOptions64(
max,
progressbar.OptionSetDescription(description),
progressbar.OptionShowCount(),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionClearOnFinish(),
)

}
}
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var Format string
var Verbose bool
var ScmProvider string
var ScmBaseURL scm.ScmBaseDomain
var Quiet bool
var (
Version string
Commit string
Expand Down Expand Up @@ -59,7 +60,6 @@ By BoostSecurity.io - https://github.com/boostsecurityio/poutine `,
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
log.Logger = log.Output(output)

},
}

Expand Down Expand Up @@ -111,6 +111,9 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "Enable verbose logging")
rootCmd.PersistentFlags().StringVarP(&ScmProvider, "scm", "s", "github", "SCM platform (github, gitlab)")
rootCmd.PersistentFlags().VarP(&ScmBaseURL, "scm-base-url", "b", "Base URI of the self-hosted SCM instance (optional)")
rootCmd.PersistentFlags().BoolVarP(&Quiet, "quiet", "q", false, "Disable progress output")

viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))
}

func initConfig() {
Expand Down
1 change: 1 addition & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
Skip []ConfigSkip `json:"skip"`
Include []ConfigInclude `json:"include"`
IgnoreForks bool `json:"ignore_forks,omitempty"`
Quiet bool `json:"quiet,omitempty"`
}

func DefaultConfig() *Config {
Expand Down

0 comments on commit ef581f7

Please sign in to comment.