Skip to content

Commit

Permalink
feat: show help message when the context's deadline passes (#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed Apr 27, 2021
1 parent 1b73636 commit 875cbc0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
6 changes: 3 additions & 3 deletions pkg/commands/artifact/fs.go
Expand Up @@ -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
}
Expand All @@ -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)
}
10 changes: 5 additions & 5 deletions pkg/commands/artifact/image.go
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions pkg/commands/artifact/repository.go
Expand Up @@ -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
}
Expand All @@ -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)
}
12 changes: 8 additions & 4 deletions pkg/commands/artifact/run.go
Expand Up @@ -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)
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/commands/client/run.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit 875cbc0

Please sign in to comment.