Skip to content

Commit

Permalink
feat: warn when cached data is used (#500)
Browse files Browse the repository at this point in the history
* fix: prefer outputhandler

* feat: warn when cached data is used
  • Loading branch information
elsapet authored Feb 7, 2023
1 parent 456e82c commit cbbf298
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/bearer/curio/pkg/commands/process/worker/work"
"github.com/bearer/curio/pkg/flag"
reportoutput "github.com/bearer/curio/pkg/report/output"
"github.com/bearer/curio/pkg/util/output"
outputhandler "github.com/bearer/curio/pkg/util/output"

"github.com/bearer/curio/pkg/types"
Expand All @@ -43,6 +42,8 @@ type ScannerConfig struct {
}

type Runner interface {
// Cached returns true if cached data was used in scan
CacheUsed() bool
// ScanFilesystem scans a filesystem
ScanFilesystem(ctx context.Context, opts flag.Options) (types.Report, error)
// ScanRepository scans repository
Expand Down Expand Up @@ -154,6 +155,10 @@ func buildScanID(scanSettings settings.Config) (string, error) {
return scanID, nil
}

func (r *runner) CacheUsed() bool {
return r.reuseDetection
}

// Close closes everything
func (r *runner) Close(ctx context.Context) error {
return nil
Expand Down Expand Up @@ -219,7 +224,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
case TargetFilesystem:
if report, err = r.ScanFilesystem(ctx, opts); err != nil {
if errors.Is(err, balancer.ErrFileListEmpty) {
output.StdOutLogger().Msgf("directory empty: %s", err)
outputhandler.StdOutLogger().Msgf("directory empty: %s", err)
os.Exit(0)
return
}
Expand Down Expand Up @@ -251,6 +256,7 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
}

func (r *runner) Report(config settings.Config, report types.Report) (bool, error) {
cacheUsed := r.CacheUsed()
// if output is defined we want to write only to file
logger := outputhandler.StdOutLogger()
if config.Report.Output != "" {
Expand All @@ -261,13 +267,19 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
logger = outputhandler.PlainLogger(reportFile)
}

if cacheUsed && !config.Scan.Quiet {
// output cached data warning at start of report
outputhandler.StdErrLogger().Msg("Using cached data")
}

if config.Report.Format == flag.FormatEmpty {
if config.Report.Report == flag.ReportSummary {
// for policy report, default report format is NOT JSON
reportPassed, err := reportoutput.ReportSummary(report, logger, config)
if err != nil {
return false, fmt.Errorf("error generating report %w", err)
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return reportPassed, nil
}
if config.Report.Report == flag.ReportPrivacy {
Expand All @@ -276,6 +288,7 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
if err != nil {
return false, fmt.Errorf("error generating report %w", err)
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return true, nil
}
}
Expand All @@ -293,5 +306,14 @@ func (r *runner) Report(config settings.Config, report types.Report) (bool, erro
return false, fmt.Errorf("error generating report %w", err)
}
}
outputCachedDataWarning(cacheUsed, config.Scan.Quiet)
return true, nil
}

func outputCachedDataWarning(cacheUsed bool, quietMode bool) {
if quietMode || !cacheUsed {
return
}

outputhandler.StdErrLogger().Msg("Cached data used (no code changes detected). Unexpected? Use --force to force a re-scan.\n")
}

0 comments on commit cbbf298

Please sign in to comment.