Skip to content

Commit e6b08af

Browse files
authored
Merge pull request #18 from dave-shawley/exit-status
Exit with failure when child does
2 parents 963851f + 7597fc6 commit e6b08af

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

main.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ func main() {
2626
app.Action = func(c *cli.Context) error {
2727
return action(c)
2828
}
29-
app.Run(os.Args)
29+
err := app.Run(os.Args)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
3033
}
3134

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

4851
params, err := getParameters(c)
4952
if err != nil {
50-
return cli.NewExitError(errorPrefix(err), code)
53+
return cli.NewExitError(errorPrefix(err), -1)
5154
}
5255

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

6770
err = RunCommand(c.Args()[0], c.Args()[1:], envVars)
6871
if err != nil {
72+
if cmdError, ok := err.(*CommandFailedError); ok {
73+
return cli.NewExitError(errorPrefix(err), cmdError.ExitCode)
74+
}
6975
return cli.NewExitError(errorPrefix(err), 128)
7076
}
7177

runner.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
"os/signal"
67
"strings"
@@ -9,6 +10,14 @@ import (
910
log "github.com/sirupsen/logrus"
1011
)
1112

13+
type CommandFailedError struct {
14+
ExitCode int
15+
}
16+
17+
func (e *CommandFailedError) Error() string {
18+
return fmt.Sprintf("Command failed with exit code %d", e.ExitCode)
19+
}
20+
1221
func RunCommand(command string, args []string, envVars []string) error {
1322

1423
log.Infof("PID %v running %s %s", os.Getpid(), command,
@@ -53,7 +62,12 @@ func RunCommand(command string, args []string, envVars []string) error {
5362
"signal": sigv},
5463
).Info("Caught signal, sent to child")
5564
}()
56-
_, err = proc.Wait()
57-
log.Debug("Exiting")
58-
return err
65+
procState, err := proc.Wait()
66+
if err != nil {
67+
return err
68+
}
69+
if procState.ExitCode() != 0 {
70+
return &CommandFailedError{procState.ExitCode()}
71+
}
72+
return nil
5973
}

0 commit comments

Comments
 (0)