Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Exit with failure when child does #18

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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(
@@ -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)
}

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"
@@ -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,
@@ -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
}