diff --git a/command/cleanup.go b/command/cleanup.go index 2e84118..ef01501 100644 --- a/command/cleanup.go +++ b/command/cleanup.go @@ -7,8 +7,10 @@ import ( "log" "os" "os/exec" + "os/signal" "strconv" "strings" + "syscall" "github.com/google/go-github/github" cli "gopkg.in/urfave/cli.v1" @@ -71,7 +73,12 @@ func CmdCleanup(c *cli.Context) (err error) { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + signalChannel := make(chan os.Signal, 1) + signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + go propagateSignalsTo(cmd, signalChannel) + err = cmd.Run() + signal.Stop(signalChannel) if err != nil { log.Println("undeploy error: ", err) @@ -108,7 +115,13 @@ func listDeployedPullRequests(listScript string) ([]string, error) { cmd := exec.Command(listScript) cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { + signalChannel := make(chan os.Signal, 1) + signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + go propagateSignalsTo(cmd, signalChannel) + + err := cmd.Run() + signal.Stop(signalChannel) + if err != nil { return nil, err } diff --git a/command/please.go b/command/please.go index 9c0932b..3fd664b 100644 --- a/command/please.go +++ b/command/please.go @@ -9,8 +9,10 @@ import ( "log" "os" "os/exec" + "os/signal" "strconv" "strings" + "syscall" "github.com/google/go-github/github" cli "gopkg.in/urfave/cli.v1" @@ -151,6 +153,10 @@ func CmdPlease(c *cli.Context) (err error) { } // Start deploy script + signalChannel := make(chan os.Signal, 1) + signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + go propagateSignalsTo(cmd, signalChannel) + err = cmd.Start() if err != nil { err2 := updateStatus(StateError, "") @@ -169,6 +175,8 @@ func CmdPlease(c *cli.Context) (err error) { // Wait on the deploy to finish err = cmd.Wait() + signal.Stop(signalChannel) + if err != nil { err2 := updateStatus(StateFailure, "") if err2 != nil { diff --git a/command/utils.go b/command/utils.go index 321f581..fd4fe62 100644 --- a/command/utils.go +++ b/command/utils.go @@ -3,6 +3,8 @@ package command import ( "context" "log" + "os" + "os/exec" "regexp" "github.com/google/go-github/github" @@ -61,3 +63,17 @@ func refString(str string) *string { func refStringList(l []string) *[]string { return &l } + +func propagateSignalsTo(cmd *exec.Cmd, signalChannel chan os.Signal) { + for sig := range signalChannel { + if cmd.Process != nil { + err := cmd.Process.Signal(sig) + if err != nil { + log.Printf("error sending signal to child process (%d): %s\n", cmd.Process.Pid, err) + } + } else { + // TODO: is this always the right thing to do if we're not running a subprocess? + os.Exit(1) + } + } +}