diff --git a/main.go b/main.go index 4ef80ea..926d6cc 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "syscall" "github.com/outblocks/outblocks-cli/cmd" + "github.com/outblocks/outblocks-cli/pkg/actions" "google.golang.org/grpc/grpclog" ) @@ -43,10 +44,18 @@ func main() { err := exec.Execute(ctx) if err != nil { + if e, ok := err.(*actions.ErrExit); ok { + if e.Message != "" { + exec.Log().Errorln(e.Message) + } + + os.Exit(e.StatusCode) //nolint + } + if ctx.Err() != context.Canceled { exec.Log().Errorln("Error occurred:", err) } - os.Exit(1) //nolint: gocritic + os.Exit(1) } } diff --git a/pkg/actions/run.go b/pkg/actions/run.go index 854e063..ed96997 100644 --- a/pkg/actions/run.go +++ b/pkg/actions/run.go @@ -72,6 +72,15 @@ const ( healthcheckTimeout = 3 * time.Second ) +type ErrExit struct { + StatusCode int + Message string +} + +func (e *ErrExit) Error() string { + return fmt.Sprintf("exit code %d", e.StatusCode) +} + func NewRun(log logger.Logger, cfg *config.Project, opts *RunOptions) *Run { return &Run{ log: log, @@ -751,7 +760,14 @@ func (d *Run) runSelfAsSudo() error { cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout - return cmd.Run() + err := cmd.Run() + if e, ok := err.(*exec.ExitError); ok { + return &ErrExit{ + StatusCode: e.ProcessState.ExitCode(), + } + } + + return err } func (d *Run) Run(ctx context.Context) error {