Skip to content

Commit

Permalink
feat: warn when cached data is used
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Feb 7, 2023
1 parent f583f1e commit ba7aaf6
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion 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 @@ -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 ba7aaf6

Please sign in to comment.