Skip to content

Commit c443772

Browse files
authored
#17: Failling actions do not return an error code (#18)
#17: Failling actions do not return an error code
1 parent bb61649 commit c443772

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

app.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package launchr
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -218,9 +219,20 @@ func (app *appImpl) Execute() int {
218219
return 125
219220
}
220221
if err = app.exec(); err != nil {
221-
fmt.Fprintln(os.Stderr, "Error:", err)
222-
return 1
222+
var status int
223+
var stErr action.RunStatusError
224+
225+
switch {
226+
case errors.As(err, &stErr):
227+
status = stErr.GetCode()
228+
default:
229+
status = 1
230+
fmt.Fprintln(os.Stderr, "Error:", err)
231+
}
232+
233+
return status
223234
}
235+
224236
return 0
225237
}
226238

pkg/action/env.container.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ func (c *containerEnv) Execute(ctx context.Context, a *Action) (err error) {
220220

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

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

251-
return nil
255+
return err
252256
}
253257

254258
func getCurrentUser() string {

pkg/action/env.container_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ func Test_ContainerExec(t *testing.T) {
548548
errAny := errors.New("any")
549549
errAttach := errors.New("attach error")
550550
errStart := errors.New("start error")
551+
errExecError := RunStatusError{code: 2, msg: "action \"test\" finished with the exit code 2"}
551552

552553
successSteps := []mockCallInfo{
553554
{
@@ -676,7 +677,7 @@ func Test_ContainerExec(t *testing.T) {
676677
[]interface{}{nil},
677678
},
678679
),
679-
nil,
680+
errExecError,
680681
},
681682
}
682683

pkg/action/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ import (
88
"github.com/launchrctl/launchr/pkg/types"
99
)
1010

11+
// RunStatusError is an execution error also containing command exit code.
12+
type RunStatusError struct {
13+
code int
14+
msg string
15+
}
16+
17+
func (e RunStatusError) Error() string {
18+
return e.msg
19+
}
20+
21+
// GetCode returns executions exit code.
22+
func (e RunStatusError) GetCode() int {
23+
return e.code
24+
}
25+
1126
// RunEnvironment is a common interface for all action run environments.
1227
type RunEnvironment interface {
1328
// Init prepares the run environment.

0 commit comments

Comments
 (0)