From ddc5a903dcc0be2188df05ed2d3a89455aa0451a Mon Sep 17 00:00:00 2001 From: Nico Esteves Date: Mon, 17 Dec 2018 16:50:32 +0100 Subject: [PATCH] iam-session and kms-env return exit code of the command --- CHANGELOG.md | 7 +++++++ VERSION | 2 +- common/utils.go | 19 +++++++++++++++++++ iam/session/main.go | 10 +++++++--- kms/env/main.go | 6 +++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acb8daf..2669128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## v6.0.0 (17/12/2018) + +**Fix** + +* `iam-session` returns the command exit code +* `kms-env` returns the command exit code + ## v5.11.0 (03/12/2018) **New** diff --git a/VERSION b/VERSION index c68d476..09b254e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5.11.0 +6.0.0 diff --git a/common/utils.go b/common/utils.go index de268bf..0784dd6 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,6 +3,8 @@ package common import ( "log" "os" + "os/exec" + "syscall" ) var ( @@ -18,3 +20,20 @@ func FatalOnError(err error) { func Fatalln(message string) { ErrorLog.Fatalln(message) } + +func GetExitCode(cmd *exec.Cmd, err error) int { + // adapted from https://stackoverflow.com/a/40770011 + if err != nil { + // try to get the exit code + if exitError, ok := err.(*exec.ExitError); ok { + ws := exitError.Sys().(syscall.WaitStatus) + return ws.ExitStatus() + } + return -1 + + } else { + // success, exitCode should be 0 if go is ok + ws := cmd.ProcessState.Sys().(syscall.WaitStatus) + return ws.ExitStatus() + } +} diff --git a/iam/session/main.go b/iam/session/main.go index f21d7f6..74143d5 100644 --- a/iam/session/main.go +++ b/iam/session/main.go @@ -58,7 +58,7 @@ func main() { } if len(*command) > 0 { - executeCommand(command, conf, &creds) + os.Exit(executeCommand(command, conf, &creds)) } } @@ -124,7 +124,7 @@ func promptConfirm(text string) bool { return response == "y" } -func executeCommand(command *[]string, conf *aws.Config, creds *credentials.Value) { +func executeCommand(command *[]string, conf *aws.Config, creds *credentials.Value) int { env := os.Environ() var pEnv []string if conf.Credentials != nil { @@ -152,5 +152,9 @@ func executeCommand(command *[]string, conf *aws.Config, creds *credentials.Valu p.Stdin = os.Stdin p.Stderr = os.Stderr p.Stdout = os.Stdout - p.Run() + err := p.Run() + + // TODO: When https://github.com/golang/go/commit/be94dac4e945a2921b116761e41f1c22f0af2add is released, replace the below with + // os.Exit(p.ProcessState.ExitCode()) + return common.GetExitCode(p, err) } diff --git a/kms/env/main.go b/kms/env/main.go index 2fbdb49..d5ba55b 100644 --- a/kms/env/main.go +++ b/kms/env/main.go @@ -60,7 +60,11 @@ func main() { p.Stdin = os.Stdin p.Stderr = os.Stderr p.Stdout = os.Stdout - p.Run() + err := p.Run() + + // TODO: When https://github.com/golang/go/commit/be94dac4e945a2921b116761e41f1c22f0af2add is released, replace the below with + // os.Exit(p.ProcessState.ExitCode()) + os.Exit(common.GetExitCode(p, err)) } func handleEnvVar(kmsClient *kms.KMS, ssmClient *ssm.SSM, secretsManagerClient *secretsmanager.SecretsManager, key, value string) (map[string]string, error) {