From f7a1572d9e226ac8ce6af8563536b69df2ce2f39 Mon Sep 17 00:00:00 2001 From: elsapet Date: Mon, 10 Jun 2024 12:15:18 +0200 Subject: [PATCH] fix: engine.Close() ordering (#1631) --- external/run/run.go | 1 - pkg/commands/artifact/run.go | 10 ++++++++-- pkg/commands/scan.go | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/external/run/run.go b/external/run/run.go index 6e31ed486..a3e510745 100644 --- a/external/run/run.go +++ b/external/run/run.go @@ -88,7 +88,6 @@ func DefaultLanguages() []Language { func Run(version, commitSHA string, engine Engine) { err := commands.NewApp(version, commitSHA, engine).Execute() - engine.Close() if err != nil { // error messages are printed by the framework diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index c0e7b9f5e..c146e0781 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -240,6 +240,12 @@ func getIgnoredFingerprints(settings settings.Config) ( return false, localIgnoredFingerprints, []string{}, nil } + +type ReportFailedError int +func (exitcode ReportFailedError) Error() string { + return "Report failed with exitcode" +} + // Run performs artifact scanning func Run(ctx context.Context, opts flagtypes.Options, engine engine.Engine) (err error) { targetPath, err := file.CanonicalPath(opts.Target) @@ -341,9 +347,9 @@ func Run(ctx context.Context, opts flagtypes.Options, engine engine.Engine) (err if reportFailed { if scanSettings.Scan.ExitCode == -1 { - defer os.Exit(1) + return ReportFailedError(1) } else { - defer os.Exit(scanSettings.Scan.ExitCode) + return ReportFailedError(scanSettings.Scan.ExitCode) } } diff --git a/pkg/commands/scan.go b/pkg/commands/scan.go index 1deb072fd..e57dd5c21 100644 --- a/pkg/commands/scan.go +++ b/pkg/commands/scan.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "os" "github.com/bearer/bearer/pkg/commands/artifact" "github.com/bearer/bearer/pkg/commands/debugprofile" @@ -87,6 +88,12 @@ func NewScanCommand(engine engine.Engine) *cobra.Command { err = artifact.Run(cmd.Context(), options, engine) debugprofile.Stop() + engine.Close() + + if exitcode, ok := err.(artifact.ReportFailedError); ok { + os.Exit(int(exitcode)) + } + return err }, SilenceErrors: false,