Skip to content

Commit

Permalink
fix: engine.Close() ordering (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Jun 10, 2024
1 parent e766ccf commit f7a1572
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 0 additions & 1 deletion external/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"os"

"github.com/bearer/bearer/pkg/commands/artifact"
"github.com/bearer/bearer/pkg/commands/debugprofile"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f7a1572

Please sign in to comment.