Skip to content

Commit

Permalink
Add proper signal handling and exit codes for exec
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Oct 6, 2015
1 parent 2eaa666 commit 2810f0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 20 additions & 3 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"strings"
"syscall"
"time"

"github.com/99designs/aws-vault/keyring"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"io/ioutil"
"log"
"os"
"os/signal"

"github.com/99designs/aws-vault/keyring"

"gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -92,13 +92,17 @@ func main() {
})

case exec.FullCommand():
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, os.Kill)

ExecCommand(ui, ExecCommandInput{
Profile: *execProfile,
Command: *execCmd,
Args: *execCmdArgs,
Keyring: keyring,
Duration: *execSessDuration,
WriteEnv: *execWriteEnv,
Signals: signals,
})

case login.FullCommand():
Expand Down

0 comments on commit 2810f0a

Please sign in to comment.