podmanv2 exit code

add ability to set and get exit code.

Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2020-03-23 11:56:46 -05:00
parent 3dbf2cb5af
commit 2d4fa996ef
2 changed files with 21 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package registry package registry
import ( import (
"github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/domain/infra" "github.com/containers/libpod/pkg/domain/infra"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -21,8 +22,18 @@ var (
EngineOpts entities.EngineOptions EngineOpts entities.EngineOptions
GlobalFlags entities.EngineFlags 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 // HelpTemplate returns the help template for podman commands
// This uses the short and long options. // This uses the short and long options.
// command should not use this. // command should not use this.

View File

@ -6,6 +6,7 @@ import (
"path" "path"
"github.com/containers/libpod/cmd/podmanV2/registry" "github.com/containers/libpod/cmd/podmanV2/registry"
"github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/version" "github.com/containers/libpod/version"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -31,7 +32,14 @@ func init() {
func Execute() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Fprintln(os.Stderr, "Error:", err.Error())
os.Exit(1) } 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())
} }