Skip to content

Commit ef581f7

Browse files
authored
feat: add --quiet option (#134)
Signed-off-by: becojo <[email protected]> Co-authored-by: Becojo <[email protected]>
1 parent d5a9e06 commit ef581f7

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

analyze/analyze.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
9090
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)
9191

9292
log.Debug().Msgf("Starting repository analysis for organization: %s on %s", org, provider)
93-
bar := progressbar.NewOptions(
94-
0,
95-
progressbar.OptionSetDescription("Analyzing repositories"),
96-
progressbar.OptionShowCount(),
97-
progressbar.OptionSetWriter(os.Stderr),
98-
)
93+
bar := a.progressBar(0, "Analyzing repositories")
9994

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

160+
_ = bar.Finish()
161+
165162
return a.finalizeAnalysis(ctx, inventory)
166163
}
167164

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

189186
log.Debug().Msgf("Starting repository analysis for: %s/%s on %s", org, repoName, provider)
190-
bar := progressbar.NewOptions(
191-
1,
192-
progressbar.OptionSetDescription("Analyzing repository"),
193-
progressbar.OptionShowCount(),
194-
progressbar.OptionSetWriter(os.Stderr),
195-
)
187+
bar := a.progressBar(2, "Cloning repository")
188+
_ = bar.RenderBlank()
196189

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

196+
bar.Describe("Analyzing repository")
197+
_ = bar.Add(1)
198+
203199
pkg, err := a.generatePackageInsights(ctx, tempDir, repo, ref)
204200
if err != nil {
205201
return err
@@ -209,9 +205,8 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
209205
if err != nil {
210206
return err
211207
}
212-
_ = bar.Add(1)
208+
_ = bar.Finish()
213209

214-
fmt.Print("\n\n")
215210
return a.finalizeAnalysis(ctx, inventory)
216211
}
217212

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

239234
log.Debug().Msgf("Starting repository analysis for: %s/%s on %s", org, repoName, provider)
240-
bar := progressbar.NewOptions(
241-
1,
242-
progressbar.OptionSetDescription("Analyzing repository"),
243-
progressbar.OptionShowCount(),
244-
progressbar.OptionSetWriter(os.Stderr),
245-
)
246235

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

258-
fmt.Print("\n\n")
259246
return a.finalizeAnalysis(ctx, inventory)
260247
}
261248

@@ -325,3 +312,18 @@ func (a *Analyzer) cloneRepoToTemp(ctx context.Context, gitURL string, token str
325312
}
326313
return tempDir, nil
327314
}
315+
316+
func (a *Analyzer) progressBar(max int64, description string) *progressbar.ProgressBar {
317+
if a.Config.Quiet {
318+
return progressbar.DefaultSilent(max, description)
319+
} else {
320+
return progressbar.NewOptions64(
321+
max,
322+
progressbar.OptionSetDescription(description),
323+
progressbar.OptionShowCount(),
324+
progressbar.OptionSetWriter(os.Stderr),
325+
progressbar.OptionClearOnFinish(),
326+
)
327+
328+
}
329+
}

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var Format string
2727
var Verbose bool
2828
var ScmProvider string
2929
var ScmBaseURL scm.ScmBaseDomain
30+
var Quiet bool
3031
var (
3132
Version string
3233
Commit string
@@ -59,7 +60,6 @@ By BoostSecurity.io - https://github.com/boostsecurityio/poutine `,
5960
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
6061
}
6162
log.Logger = log.Output(output)
62-
6363
},
6464
}
6565

@@ -111,6 +111,9 @@ func init() {
111111
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "Enable verbose logging")
112112
rootCmd.PersistentFlags().StringVarP(&ScmProvider, "scm", "s", "github", "SCM platform (github, gitlab)")
113113
rootCmd.PersistentFlags().VarP(&ScmBaseURL, "scm-base-url", "b", "Base URI of the self-hosted SCM instance (optional)")
114+
rootCmd.PersistentFlags().BoolVarP(&Quiet, "quiet", "q", false, "Disable progress output")
115+
116+
viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))
114117
}
115118

116119
func initConfig() {

models/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Config struct {
1717
Skip []ConfigSkip `json:"skip"`
1818
Include []ConfigInclude `json:"include"`
1919
IgnoreForks bool `json:"ignore_forks,omitempty"`
20+
Quiet bool `json:"quiet,omitempty"`
2021
}
2122

2223
func DefaultConfig() *Config {

0 commit comments

Comments
 (0)