From ba8406ceab19dbd4b25e2c0f617c6373b6b75c1e Mon Sep 17 00:00:00 2001 From: nikpivkin Date: Fri, 15 Mar 2024 18:18:17 +0700 Subject: [PATCH 1/2] feat(aws): quiet flag support --- pkg/cloud/aws/commands/run.go | 4 ---- pkg/cloud/aws/scanner/progress.go | 10 +++++++--- pkg/cloud/aws/scanner/scanner.go | 8 +++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/cloud/aws/commands/run.go b/pkg/cloud/aws/commands/run.go index a4541e9f0544..23406aeafda5 100644 --- a/pkg/cloud/aws/commands/run.go +++ b/pkg/cloud/aws/commands/run.go @@ -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) { diff --git a/pkg/cloud/aws/scanner/progress.go b/pkg/cloud/aws/scanner/progress.go index 243e6eb47f39..c75ac56277de 100644 --- a/pkg/cloud/aws/scanner/progress.go +++ b/pkg/cloud/aws/scanner/progress.go @@ -2,6 +2,7 @@ package scanner import ( "fmt" + "io" "os" "github.com/aquasecurity/loading/pkg/bar" @@ -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, } } @@ -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) + + m.debugWriter.Write([]byte(fmt.Sprintf("[%d/%d] Scanning %s...\n", m.serviceCurrent+1, m.serviceTotal, name))) m.serviceBar = bar.New( bar.OptionHideOnFinish(true), bar.OptionWithAutoComplete(false), diff --git a/pkg/cloud/aws/scanner/scanner.go b/pkg/cloud/aws/scanner/scanner.go index d91721512505..97f544492747 100644 --- a/pkg/cloud/aws/scanner/scanner.go +++ b/pkg/cloud/aws/scanner/scanner.go @@ -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)) } @@ -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 != "" { From 2797135bcb64d993b69521e42cd611c8c5d78266 Mon Sep 17 00:00:00 2001 From: nikpivkin Date: Fri, 15 Mar 2024 18:34:53 +0700 Subject: [PATCH 2/2] fix lint issue --- pkg/cloud/aws/scanner/progress.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cloud/aws/scanner/progress.go b/pkg/cloud/aws/scanner/progress.go index c75ac56277de..a313dd482c6c 100644 --- a/pkg/cloud/aws/scanner/progress.go +++ b/pkg/cloud/aws/scanner/progress.go @@ -73,7 +73,7 @@ func (m *progressTracker) StartService(name string) { return } - m.debugWriter.Write([]byte(fmt.Sprintf("[%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),