From 49378fb60f8359fc3f2eec8fe35ec6586f844646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 16 Oct 2020 13:20:23 +0000 Subject: [PATCH] Handle Ctrl-C in repo garden --- pkg/cmd/repo/garden/garden.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/repo/garden/garden.go b/pkg/cmd/repo/garden/garden.go index 2402ccbc1..01fe07b77 100644 --- a/pkg/cmd/repo/garden/garden.go +++ b/pkg/cmd/repo/garden/garden.go @@ -7,10 +7,13 @@ import ( "io" "math/rand" "net/http" + "os" "os/exec" + "os/signal" "runtime" "strconv" "strings" + "syscall" "github.com/cli/cli/api" "github.com/cli/cli/internal/ghinstance" @@ -216,6 +219,22 @@ func gardenRun(opts *GardenOptions) error { _ = exec.Command("stty", sttyFileArg, "/dev/tty", "cbreak", "min", "1").Run() _ = exec.Command("stty", sttyFileArg, "/dev/tty", "-echo").Run() + walkAway := func() { + clear(opts.IO) + fmt.Fprint(out, "\033[?25h") + _ = exec.Command("stty", sttyFileArg, "/dev/tty", strings.TrimSpace(string(oldTTYSettings))).Run() + fmt.Fprintln(out) + fmt.Fprintln(out, utils.Bold("You turn and walk away from the wildflower garden...")) + } + + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func(){ + <-c + walkAway() + os.Exit(0) + }() + var b []byte = make([]byte, 3) for { _, _ = opts.IO.In.Read(b) @@ -296,12 +315,7 @@ func gardenRun(opts *GardenOptions) error { fmt.Fprint(out, utils.Bold(sl)) } - clear(opts.IO) - fmt.Fprint(out, "\033[?25h") - _ = exec.Command("stty", sttyFileArg, "/dev/tty", strings.TrimSpace(string(oldTTYSettings))).Run() - fmt.Fprintln(out) - fmt.Fprintln(out, utils.Bold("You turn and walk away from the wildflower garden...")) - + walkAway() return nil }