From 2d4fa996ef86d803442f96112ab9ee341d26215a Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Mon, 23 Mar 2020 11:56:46 -0500 Subject: podmanv2 exit code add ability to set and get exit code. Signed-off-by: Brent Baude --- cmd/podmanV2/registry/registry.go | 11 +++++++++++ cmd/podmanV2/root.go | 12 ++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/podmanV2/registry/registry.go b/cmd/podmanV2/registry/registry.go index 793d520a8..b0c11ba04 100644 --- a/cmd/podmanV2/registry/registry.go +++ b/cmd/podmanV2/registry/registry.go @@ -1,6 +1,7 @@ package registry import ( + "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/infra" "github.com/pkg/errors" @@ -21,8 +22,18 @@ var ( EngineOpts entities.EngineOptions GlobalFlags entities.EngineFlags + + ExitCode = define.ExecErrorCodeGeneric ) +func SetExitCode(code int) { + ExitCode = code +} + +func GetExitCode() int { + return ExitCode +} + // HelpTemplate returns the help template for podman commands // This uses the short and long options. // command should not use this. diff --git a/cmd/podmanV2/root.go b/cmd/podmanV2/root.go index b0dd7643f..2becd126d 100644 --- a/cmd/podmanV2/root.go +++ b/cmd/podmanV2/root.go @@ -6,6 +6,7 @@ import ( "path" "github.com/containers/libpod/cmd/podmanV2/registry" + "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/version" "github.com/spf13/cobra" ) @@ -31,7 +32,14 @@ func init() { func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + fmt.Fprintln(os.Stderr, "Error:", err.Error()) + } else if registry.GetExitCode() == define.ExecErrorCodeGeneric { + // The exitCode modified from define.ExecErrorCodeGeneric, + // indicates an application + // running inside of a container failed, as opposed to the + // podman command failed. Must exit with that exit code + // otherwise command exited correctly. + registry.SetExitCode(0) } + os.Exit(registry.GetExitCode()) } -- cgit v1.2.3-54-g00ecf