Skip to content

Commit c912892

Browse files
committed
fix: run error code propagation
1 parent 7973148 commit c912892

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"syscall"
1010

1111
"github.com/outblocks/outblocks-cli/cmd"
12+
"github.com/outblocks/outblocks-cli/pkg/actions"
1213
"google.golang.org/grpc/grpclog"
1314
)
1415

@@ -43,10 +44,18 @@ func main() {
4344
err := exec.Execute(ctx)
4445

4546
if err != nil {
47+
if e, ok := err.(*actions.ErrExit); ok {
48+
if e.Message != "" {
49+
exec.Log().Errorln(e.Message)
50+
}
51+
52+
os.Exit(e.StatusCode) //nolint
53+
}
54+
4655
if ctx.Err() != context.Canceled {
4756
exec.Log().Errorln("Error occurred:", err)
4857
}
4958

50-
os.Exit(1) //nolint: gocritic
59+
os.Exit(1)
5160
}
5261
}

pkg/actions/run.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ const (
7272
healthcheckTimeout = 3 * time.Second
7373
)
7474

75+
type ErrExit struct {
76+
StatusCode int
77+
Message string
78+
}
79+
80+
func (e *ErrExit) Error() string {
81+
return fmt.Sprintf("exit code %d", e.StatusCode)
82+
}
83+
7584
func NewRun(log logger.Logger, cfg *config.Project, opts *RunOptions) *Run {
7685
return &Run{
7786
log: log,
@@ -751,7 +760,14 @@ func (d *Run) runSelfAsSudo() error {
751760
cmd.Stderr = os.Stderr
752761
cmd.Stdout = os.Stdout
753762

754-
return cmd.Run()
763+
err := cmd.Run()
764+
if e, ok := err.(*exec.ExitError); ok {
765+
return &ErrExit{
766+
StatusCode: e.ProcessState.ExitCode(),
767+
}
768+
}
769+
770+
return err
755771
}
756772

757773
func (d *Run) Run(ctx context.Context) error {

0 commit comments

Comments
 (0)