Skip to content

Commit

Permalink
Merge pull request #18 from dave-shawley/exit-status
Browse files Browse the repository at this point in the history
Exit with failure when child does
  • Loading branch information
gmr authored Jul 28, 2023
2 parents 963851f + 7597fc6 commit e6b08af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func main() {
app.Action = func(c *cli.Context) error {
return action(c)
}
app.Run(os.Args)
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

func action(c *cli.Context) error {
Expand All @@ -47,7 +50,7 @@ func action(c *cli.Context) error {

params, err := getParameters(c)
if err != nil {
return cli.NewExitError(errorPrefix(err), code)
return cli.NewExitError(errorPrefix(err), -1)
}

envVars := BuildEnvVars(
Expand All @@ -66,6 +69,9 @@ func action(c *cli.Context) error {

err = RunCommand(c.Args()[0], c.Args()[1:], envVars)
if err != nil {
if cmdError, ok := err.(*CommandFailedError); ok {
return cli.NewExitError(errorPrefix(err), cmdError.ExitCode)
}
return cli.NewExitError(errorPrefix(err), 128)
}

Expand Down
20 changes: 17 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"os/signal"
"strings"
Expand All @@ -9,6 +10,14 @@ import (
log "github.com/sirupsen/logrus"
)

type CommandFailedError struct {
ExitCode int
}

func (e *CommandFailedError) Error() string {
return fmt.Sprintf("Command failed with exit code %d", e.ExitCode)
}

func RunCommand(command string, args []string, envVars []string) error {

log.Infof("PID %v running %s %s", os.Getpid(), command,
Expand Down Expand Up @@ -53,7 +62,12 @@ func RunCommand(command string, args []string, envVars []string) error {
"signal": sigv},
).Info("Caught signal, sent to child")
}()
_, err = proc.Wait()
log.Debug("Exiting")
return err
procState, err := proc.Wait()
if err != nil {
return err
}
if procState.ExitCode() != 0 {
return &CommandFailedError{procState.ExitCode()}
}
return nil
}

0 comments on commit e6b08af

Please sign in to comment.