Skip to content

Commit

Permalink
iam-session and kms-env return exit code of the command
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Dec 17, 2018
1 parent 5f821ea commit ddc5a90
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.11.0
6.0.0
19 changes: 19 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"log"
"os"
"os/exec"
"syscall"
)

var (
Expand All @@ -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()
}
}
10 changes: 7 additions & 3 deletions iam/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
}

if len(*command) > 0 {
executeCommand(command, conf, &creds)
os.Exit(executeCommand(command, conf, &creds))
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion kms/env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

1 comment on commit ddc5a90

@hamstah
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.