diff --git a/exec.go b/exec.go index 39ed4070e..533a54409 100644 --- a/exec.go +++ b/exec.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "strings" + "syscall" "time" "github.com/99designs/aws-vault/keyring" @@ -23,6 +24,7 @@ type ExecCommandInput struct { Keyring keyring.Keyring Duration time.Duration WriteEnv bool + Signals chan os.Signal } func ExecCommand(ui Ui, input ExecCommandInput) { @@ -69,15 +71,30 @@ func ExecCommand(ui Ui, input ExecCommandInput) { } } - // TODO: send kill signal to child process if received cmd := exec.Command(input.Command, input.Args...) cmd.Env = env cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err = cmd.Run(); err != nil { - ui.Error.Fatal(err) + + go func() { + sig := <-input.Signals + if cmd.Process != nil { + cmd.Process.Signal(sig) + } + }() + + var waitStatus syscall.WaitStatus + if err := cmd.Run(); err != nil { + if err != nil { + ui.Error.Println(err) + } + if exitError, ok := err.(*exec.ExitError); ok { + waitStatus = exitError.Sys().(syscall.WaitStatus) + os.Exit(waitStatus.ExitStatus()) + } } + } // write out a config excluding role switching keys diff --git a/main.go b/main.go index 74f432b6d..940dfb7bd 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "io/ioutil" "log" "os" + "os/signal" "github.com/99designs/aws-vault/keyring" - "gopkg.in/alecthomas/kingpin.v2" ) @@ -92,6 +92,9 @@ func main() { }) case exec.FullCommand(): + signals := make(chan os.Signal) + signal.Notify(signals, os.Interrupt, os.Kill) + ExecCommand(ui, ExecCommandInput{ Profile: *execProfile, Command: *execCmd, @@ -99,6 +102,7 @@ func main() { Keyring: keyring, Duration: *execSessDuration, WriteEnv: *execWriteEnv, + Signals: signals, }) case login.FullCommand():