Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws): quiet flag support #6331

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pkg/cloud/aws/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ func Run(ctx context.Context, opt flag.Options) error {
ctx, cancel := context.WithTimeout(ctx, opt.GlobalOptions.Timeout)
defer cancel()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err := log.InitLogger(opt.Debug, false); err != nil {
return xerrors.Errorf("logger error: %w", err)
}

var err error
defer func() {
if errors.Is(err, context.DeadlineExceeded) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/cloud/aws/scanner/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scanner

import (
"fmt"
"io"
"os"

"github.com/aquasecurity/loading/pkg/bar"
Expand All @@ -12,15 +13,17 @@ type progressTracker struct {
serviceTotal int
serviceCurrent int
isTTY bool
debugWriter io.Writer
}

func newProgressTracker() *progressTracker {
func newProgressTracker(w io.Writer) *progressTracker {
var isTTY bool
if stat, err := os.Stdout.Stat(); err == nil {
isTTY = stat.Mode()&os.ModeCharDevice == os.ModeCharDevice
}
return &progressTracker{
isTTY: isTTY,
isTTY: isTTY,
debugWriter: w,
}
}

Expand Down Expand Up @@ -69,7 +72,8 @@ func (m *progressTracker) StartService(name string) {
if !m.isTTY {
return
}
fmt.Printf("[%d/%d] Scanning %s...\n", m.serviceCurrent+1, m.serviceTotal, name)

fmt.Fprintf(m.debugWriter, "[%d/%d] Scanning %s...\n", m.serviceCurrent+1, m.serviceTotal, name)
m.serviceBar = bar.New(
bar.OptionHideOnFinish(true),
bar.OptionWithAutoComplete(false),
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloud/aws/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ func (s *AWSScanner) Scan(ctx context.Context, option flag.Options) (scan.Result
awsCache := cache.New(option.CacheDir, option.MaxCacheAge, option.Account, option.Region)
included, missing := awsCache.ListServices(option.Services)

prefixedLogger := &log.PrefixedLogger{Name: "aws"}

var scannerOpts []options.ScannerOption
if !option.NoProgress {
tracker := newProgressTracker()
tracker := newProgressTracker(prefixedLogger)
defer tracker.Finish()
scannerOpts = append(scannerOpts, aws.ScannerWithProgressTracker(tracker))
}
Expand All @@ -43,11 +45,11 @@ func (s *AWSScanner) Scan(ctx context.Context, option flag.Options) (scan.Result
}

if option.Debug {
scannerOpts = append(scannerOpts, options.ScannerWithDebug(&log.PrefixedLogger{Name: "aws"}))
scannerOpts = append(scannerOpts, options.ScannerWithDebug(prefixedLogger))
}

if option.Trace {
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&log.PrefixedLogger{Name: "aws"}))
scannerOpts = append(scannerOpts, options.ScannerWithTrace(prefixedLogger))
}

if option.Region != "" {
Expand Down