feat: show help message when the context's deadline passes (#955)

This commit is contained in:
Teppei Fukuda
2021-04-27 17:13:01 +03:00
committed by GitHub
parent 0346a106f8
commit 1385fa4190
5 changed files with 29 additions and 21 deletions

View File

@@ -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)
} }

View File

@@ -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)
} }

View File

@@ -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)
} }

View File

@@ -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)
} }

View File

@@ -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)
} }