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
|
// FilesystemRun runs scan on filesystem
|
||||||
func FilesystemRun(cliCtx *cli.Context) error {
|
func FilesystemRun(ctx *cli.Context) error {
|
||||||
c, err := NewConfig(cliCtx)
|
c, err := NewConfig(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -33,5 +33,5 @@ func FilesystemRun(cliCtx *cli.Context) error {
|
|||||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
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
|
// ImageRun runs scan on docker image
|
||||||
func ImageRun(cliCtx *cli.Context) error {
|
func ImageRun(ctx *cli.Context) error {
|
||||||
c, err := NewConfig(cliCtx)
|
c, err := NewConfig(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize config
|
// initialize config
|
||||||
if err := c.Init(); err != nil {
|
if err = c.Init(); err != nil {
|
||||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
return xerrors.Errorf("failed to initialize options: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Input != "" {
|
if c.Input != "" {
|
||||||
// scan tar file
|
// 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
|
// RepositoryRun runs scan on repository
|
||||||
func RepositoryRun(cliCtx *cli.Context) error {
|
func RepositoryRun(ctx *cli.Context) error {
|
||||||
c, err := NewConfig(cliCtx)
|
c, err := NewConfig(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -34,5 +34,5 @@ func RepositoryRun(cliCtx *cli.Context) error {
|
|||||||
return xerrors.Errorf("failed to initialize options: %w", err)
|
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,
|
type InitializeScanner func(context.Context, string, cache.ArtifactCache, cache.LocalArtifactCache, time.Duration,
|
||||||
[]analyzer.Type) (scanner.Scanner, func(), error)
|
[]analyzer.Type) (scanner.Scanner, func(), error)
|
||||||
|
|
||||||
func run(conf Config, initializeScanner InitializeScanner) error {
|
func run(ctx context.Context, conf Config, initializeScanner InitializeScanner) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
ctx, cancel := context.WithTimeout(ctx, conf.Timeout)
|
||||||
defer cancel()
|
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 {
|
if err := log.InitLogger(conf.Debug, conf.Quiet); err != nil {
|
||||||
l.Fatal(err)
|
l.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,22 +18,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Run runs the scan
|
// Run runs the scan
|
||||||
func Run(cliCtx *cli.Context) error {
|
func Run(ctx *cli.Context) error {
|
||||||
c, err := NewConfig(cliCtx)
|
c, err := NewConfig(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
ctx, cancel := context.WithTimeout(context.Background(), conf.Timeout)
|
||||||
defer cancel()
|
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 {
|
if err := initialize(&conf); err != nil {
|
||||||
return xerrors.Errorf("initialize error: %w", err)
|
return xerrors.Errorf("initialize error: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user