Skip to content

Commit c385c09

Browse files
authored
Return exit code of the exec call (#441)
## Summary Currently, there is no way to figuring out if the shell / run command succeeded or not in the CI-like environment. This commit fixes this issue by returning exit code of the exec call if errored. ## How was it tested? - Running `shell` with the failing command ```sh ❯ ./devbox shell -- exit 1 Installing nix packages. This may take a while... done. Error: exit status 1 ❯ echo $? 1 ``` - Running `script` with the failing command ```json { "shell": { "scripts": { "failing_script": "exit 1" } } } ``` ```sh ❯ ./devbox run failing_script Installing nix packages. This may take a while... done. Starting a devbox shell... Error: exit status 1 ❯ echo $? 1 ``` Signed-off-by: Sunghoon Kang <[email protected]>
1 parent 6527e9d commit c385c09

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

internal/boxcli/midcobra/midcobra.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package midcobra
66
import (
77
"context"
88
"encoding/hex"
9+
"errors"
10+
"os/exec"
911

1012
"github.com/google/uuid"
1113
"github.com/spf13/cobra"
@@ -69,6 +71,11 @@ func (ex *midcobraExecutable) Execute(ctx context.Context, args []string) int {
6971
}
7072

7173
if err != nil {
74+
// If the error is from the exec call, return the exit code of the exec call.
75+
var exitErr *exec.ExitError
76+
if errors.As(err, &exitErr) {
77+
return exitErr.ExitCode()
78+
}
7279
return 1 // Error exit code
7380
} else {
7481
return 0

internal/boxcli/run.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package boxcli
55

66
import (
7-
"os/exec"
87
"sort"
98

109
"github.com/pkg/errors"
@@ -61,11 +60,6 @@ func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {
6160
} else {
6261
err = box.RunScript(script)
6362
}
64-
65-
var exitErr *exec.ExitError
66-
if errors.As(err, &exitErr) {
67-
return nil
68-
}
6963
return err
7064
}
7165

internal/boxcli/shell.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package boxcli
55

66
import (
77
"fmt"
8-
"os/exec"
98

109
"github.com/pkg/errors"
1110
"github.com/spf13/cobra"
@@ -65,11 +64,6 @@ func runShellCmd(cmd *cobra.Command, args []string, flags shellCmdFlags) error {
6564
} else {
6665
err = box.Shell()
6766
}
68-
69-
var exitErr *exec.ExitError
70-
if errors.As(err, &exitErr) {
71-
return nil
72-
}
7367
return err
7468
}
7569

0 commit comments

Comments
 (0)