Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package launchr

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -218,9 +219,20 @@ func (app *appImpl) Execute() int {
return 125
}
if err = app.exec(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
return 1
var status int
var stErr action.RunStatusError

switch {
case errors.As(err, &stErr):
status = stErr.GetCode()
default:
status = 1
fmt.Fprintln(os.Stderr, "Error:", err)
}

return status
}

return 0
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/action/env.container.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ func (c *containerEnv) Execute(ctx context.Context, a *Action) (err error) {

status := <-statusCh
// @todo maybe we should note that SIG was sent to the container. Code 130 is sent on Ctlr+C.
log.Info("action %q finished with the exit code %d", a.ID, status)
msg := fmt.Sprintf("action %q finished with the exit code %d", a.ID, status)
log.Info(msg)
if status != 0 {
err = RunStatusError{code: status, msg: msg}
}

// Copy back the result from the volume.
// @todo it's a bad implementation considering consequential runs, need to find a better way to sync with remote.
Expand Down Expand Up @@ -248,7 +252,7 @@ func (c *containerEnv) Execute(ctx context.Context, a *Action) (err error) {
}
}

return nil
return err
}

func getCurrentUser() string {
Expand Down
3 changes: 2 additions & 1 deletion pkg/action/env.container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ func Test_ContainerExec(t *testing.T) {
errAny := errors.New("any")
errAttach := errors.New("attach error")
errStart := errors.New("start error")
errExecError := RunStatusError{code: 2, msg: "action \"test\" finished with the exit code 2"}

successSteps := []mockCallInfo{
{
Expand Down Expand Up @@ -676,7 +677,7 @@ func Test_ContainerExec(t *testing.T) {
[]interface{}{nil},
},
),
nil,
errExecError,
},
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/action/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import (
"github.com/launchrctl/launchr/pkg/types"
)

// RunStatusError is an execution error also containing command exit code.
type RunStatusError struct {
code int
msg string
}

func (e RunStatusError) Error() string {
return e.msg
}

// GetCode returns executions exit code.
func (e RunStatusError) GetCode() int {
return e.code
}

// RunEnvironment is a common interface for all action run environments.
type RunEnvironment interface {
// Init prepares the run environment.
Expand Down