Skip to content

Commit

Permalink
feat(aws): quiet flag support (#6331)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored Mar 29, 2024
1 parent 712dcd3 commit 87a9aa6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
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()

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

0 comments on commit 87a9aa6

Please sign in to comment.