Skip to content

Commit 3dd8a8a

Browse files
committed
Propagate the exit code to the caller
When passing commands to ssh or run it is better to propagate the exit code to the caller to improve podman-bootc usage in scripts. Signed-off-by: German Maglione <[email protected]>
1 parent 21a5fb2 commit 3dd8a8a

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

cmd/root.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010
)
1111

1212
// RootCmd represents the base command when called without any subcommands
13-
var RootCmd = &cobra.Command{
14-
Use: "podman-bootc",
15-
Short: "Run bootable containers as a virtual machine",
16-
Long: "Run bootable containers as a virtual machine",
17-
PersistentPreRunE: preExec,
18-
SilenceUsage: true,
19-
}
13+
var (
14+
RootCmd = &cobra.Command{
15+
Use: "podman-bootc",
16+
Short: "Run bootable containers as a virtual machine",
17+
Long: "Run bootable containers as a virtual machine",
18+
PersistentPreRunE: preExec,
19+
SilenceUsage: true,
20+
}
21+
ExitCode int
22+
)
2023

2124
var rootLogLevel string
2225

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func doRun(flags *cobra.Command, args []string) error {
177177
}
178178

179179
// ssh into the VM
180-
err = bootcVM.RunSSH(cmd)
180+
ExitCode, err = utils.WithExitCode(bootcVM.RunSSH(cmd))
181181
if err != nil {
182182
return fmt.Errorf("ssh: %w", err)
183183
}

cmd/ssh.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"gitlab.com/bootc-org/podman-bootc/pkg/config"
55
"gitlab.com/bootc-org/podman-bootc/pkg/user"
6+
"gitlab.com/bootc-org/podman-bootc/pkg/utils"
67
"gitlab.com/bootc-org/podman-bootc/pkg/vm"
78

89
"github.com/spf13/cobra"
@@ -51,5 +52,7 @@ func doSsh(_ *cobra.Command, args []string) error {
5152
if len(args) > 1 {
5253
cmd = args[1:]
5354
}
54-
return vm.RunSSH(cmd)
55+
56+
ExitCode, err = utils.WithExitCode(vm.RunSSH(cmd))
57+
return err
5558
}

pkg/utils/errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package utils
22

3+
import (
4+
"errors"
5+
"os/exec"
6+
)
7+
38
const (
49
PodmanMachineErrorMessage = `
510
******************************************************************
@@ -8,3 +13,17 @@ const (
813
`
914
// PodmanMachineErrorMessage = "\n**** A rootful Podman machine is required to run podman-bootc ****\n"
1015
)
16+
17+
// SetExitCode set the exit code for exec.ExitError errors, and no
18+
// error is returned
19+
func WithExitCode(err error) (int, error) {
20+
if err == nil {
21+
return 0, nil
22+
}
23+
24+
var exitError *exec.ExitError
25+
if errors.As(err, &exitError) {
26+
return exitError.ExitCode(), nil
27+
}
28+
return 1, err
29+
}

podman-bootc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ func main() {
7474
}()
7575

7676
cmd.Execute()
77+
os.Exit(cmd.ExitCode)
7778
}

0 commit comments

Comments
 (0)