mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 07:10:41 -08:00
feat: show help message when the context's deadline passes (#955)
This commit is contained in:
@@ -22,8 +22,8 @@ func filesystemScanner(ctx context.Context, dir string, ac cache.ArtifactCache,
|
||||
}
|
||||
|
||||
// FilesystemRun runs scan on filesystem
|
||||
func FilesystemRun(cliCtx *cli.Context) error {
|
||||
c, err := NewConfig(cliCtx)
|
||||
func FilesystemRun(ctx *cli.Context) error {
|
||||
c, err := NewConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -33,5 +33,5 @@ func FilesystemRun(cliCtx *cli.Context) error {
|
||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
||||
}
|
||||
|
||||
return run(c, filesystemScanner)
|
||||
return run(ctx.Context, c, filesystemScanner)
|
||||
}
|
||||
|
||||
@@ -32,21 +32,21 @@ func dockerScanner(ctx context.Context, imageName string, ac cache.ArtifactCache
|
||||
}
|
||||
|
||||
// ImageRun runs scan on docker image
|
||||
func ImageRun(cliCtx *cli.Context) error {
|
||||
c, err := NewConfig(cliCtx)
|
||||
func ImageRun(ctx *cli.Context) error {
|
||||
c, err := NewConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// initialize config
|
||||
if err := c.Init(); err != nil {
|
||||
if err = c.Init(); err != nil {
|
||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
||||
}
|
||||
|
||||
if c.Input != "" {
|
||||
// scan tar file
|
||||
return run(c, archiveScanner)
|
||||
return run(ctx.Context, c, archiveScanner)
|
||||
}
|
||||
|
||||
return run(c, dockerScanner)
|
||||
return run(ctx.Context, c, dockerScanner)
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ func repositoryScanner(ctx context.Context, dir string, ac cache.ArtifactCache,
|
||||
}
|
||||
|
||||
// RepositoryRun runs scan on repository
|
||||
func RepositoryRun(cliCtx *cli.Context) error {
|
||||
c, err := NewConfig(cliCtx)
|
||||
func RepositoryRun(ctx *cli.Context) error {
|
||||
c, err := NewConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -34,5 +34,5 @@ func RepositoryRun(cliCtx *cli.Context) error {
|
||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
||||
}
|
||||
|
||||
return run(c, repositoryScanner)
|
||||
return run(ctx.Context, c, repositoryScanner)
|
||||
}
|
||||
|
||||
@@ -26,14 +26,18 @@ var errSkipScan = errors.New("skip subsequent processes")
|
||||
type InitializeScanner func(context.Context, string, cache.ArtifactCache, cache.LocalArtifactCache, time.Duration,
|
||||
[]analyzer.Type) (scanner.Scanner, func(), error)
|
||||
|
||||
func run(conf Config, initializeScanner InitializeScanner) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
||||
func run(ctx context.Context, conf Config, initializeScanner InitializeScanner) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, conf.Timeout)
|
||||
defer cancel()
|
||||
|
||||
return runWithContext(ctx, conf, initializeScanner)
|
||||
err := runWithTimeout(ctx, conf, initializeScanner)
|
||||
if xerrors.Is(err, context.DeadlineExceeded) {
|
||||
log.Logger.Warn("Increase --timeout value")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func runWithContext(ctx context.Context, conf Config, initializeScanner InitializeScanner) error {
|
||||
func runWithTimeout(ctx context.Context, conf Config, initializeScanner InitializeScanner) error {
|
||||
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -18,22 +18,26 @@ import (
|
||||
)
|
||||
|
||||
// Run runs the scan
|
||||
func Run(cliCtx *cli.Context) error {
|
||||
c, err := NewConfig(cliCtx)
|
||||
func Run(ctx *cli.Context) error {
|
||||
c, err := NewConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return run(c)
|
||||
return run(ctx.Context, c)
|
||||
}
|
||||
|
||||
func run(conf Config) error {
|
||||
func run(ctx context.Context, conf Config) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
||||
defer cancel()
|
||||
|
||||
return runWithContext(ctx, conf)
|
||||
err := runWithTimeout(ctx, conf)
|
||||
if xerrors.Is(err, context.DeadlineExceeded) {
|
||||
log.Logger.Warn("Increase --timeout value")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func runWithContext(ctx context.Context, conf Config) error {
|
||||
func runWithTimeout(ctx context.Context, conf Config) error {
|
||||
if err := initialize(&conf); err != nil {
|
||||
return xerrors.Errorf("initialize error: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user