From 365096d5b0f0eb89959184e4d6c04301f70a4391 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 3 Jun 2019 22:02:44 +1000 Subject: [PATCH] subprocesses that can be safely interrupted --- pkg/commands/service.go | 1 + pkg/gui/keybindings.go | 12 ++++++------ pkg/gui/subprocess.go | 34 ++++++---------------------------- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/pkg/commands/service.go b/pkg/commands/service.go index c611321a..90e2c6de 100644 --- a/pkg/commands/service.go +++ b/pkg/commands/service.go @@ -67,6 +67,7 @@ func (s *Service) ViewLogs() (*exec.Cmd, error) { command := utils.ApplyTemplate(templateString, s) cmd := s.OSCommand.ExecutableFromString(command) + // so long as this is commented in, the child process does not receive the interrupt cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, Pgid: 0, diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 1664466c..354ecd89 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -65,12 +65,12 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Modifier: gocui.ModNone, Handler: gui.quit, }, - // { - // ViewName: "", - // Key: gocui.KeyCtrlC, - // Modifier: gocui.ModNone, - // Handler: gui.quit, - // }, + { + ViewName: "", + Key: gocui.KeyCtrlC, + Modifier: gocui.ModNone, + Handler: gui.quit, + }, { ViewName: "", Key: gocui.KeyEsc, diff --git a/pkg/gui/subprocess.go b/pkg/gui/subprocess.go index d1c1a9c1..78729329 100644 --- a/pkg/gui/subprocess.go +++ b/pkg/gui/subprocess.go @@ -1,9 +1,7 @@ package gui import ( - "bufio" "fmt" - "io" "io/ioutil" "os" "os/signal" @@ -37,40 +35,18 @@ func (gui *Gui) RunWithSubprocesses() error { func (gui *Gui) runCommand() error { gui.SubProcess.Stdout = os.Stdout gui.SubProcess.Stderr = os.Stdout - // gui.SubProcess.Stdin = os.Stdin - - reader := bufio.NewReader(os.Stdin) + gui.SubProcess.Stdin = os.Stdin c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) go func() { + signal.Notify(c, os.Interrupt) <-c + signal.Stop(c) + gui.SubProcess.Process.Kill() }() - w, err := gui.SubProcess.StdinPipe() - if err != nil { - return err - } - - go func() { - for { - input, err := reader.ReadByte() - gui.Log.Warn("byte received") - gui.Log.Warn(input) - if input == 3 { - gui.SubProcess.Process.Kill() - break - } - if err != nil && err == io.EOF { - gui.SubProcess.Process.Kill() - break - } - w.Write([]byte{input}) - } - }() - fmt.Fprintf(os.Stdout, "\n%s\n\n", utils.ColoredString("+ "+strings.Join(gui.SubProcess.Args, " "), color.FgBlue)) if err := gui.SubProcess.Run(); err != nil { @@ -79,6 +55,8 @@ func (gui *Gui) runCommand() error { gui.Log.Error(err) } + signal.Stop(c) + gui.SubProcess.Stdout = ioutil.Discard gui.SubProcess.Stderr = ioutil.Discard gui.SubProcess.Stdin = nil