Skip to content

Commit

Permalink
Kill synchronously started commands when vyrest CLI is interrupted.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsouthworth committed Jul 12, 2016
1 parent 4f5d447 commit ba910da
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/vyrest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ import (
"fmt"
"github.com/jsouthworth/vyrest"
"os"
"os/signal"
"sort"
"text/tabwriter"
)

var host, user, pass, sid string
var signalProcCh chan *vyrest.Process

func signalHandler() {
c := make(chan os.Signal, 1)
var proc *vyrest.Process
signal.Notify(c, os.Interrupt)
for {
select {
case <-c:
if proc != nil {
proc.Kill()
proc = nil
}
os.Exit(2)
case proc = <-signalProcCh:
}
}
}
func init() {
signalProcCh = make(chan *vyrest.Process)
go signalHandler()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <command> <args> \n", os.Args[0])
flag.PrintDefaults()
Expand Down Expand Up @@ -135,6 +155,7 @@ func startCmd(c *vyrest.Client, args ...string) {
func runCmd(c *vyrest.Client, args ...string) {
cmd, err := c.StartProcess(args)
handleError(err)
signalProcCh <- cmd
err = cmd.StreamOutput(os.Stdout)
handleError(err)
}
Expand Down

0 comments on commit ba910da

Please sign in to comment.