refactor: use early return for guards
1 file changed, 21 insertions(+), 13 deletions(-)
changed files
M command/command.go → command/command.go
@@ -58,22 +58,30 @@ } } func (cmd *Cmd) Stop() error { - if cmd.Cmd != nil && cmd.Process != nil && cmd.ProcessState == nil { - err := cmd.Process.Signal(os.Interrupt) - if err != nil { - return fmt.Errorf("error killing command: %v", err) + if cmd.Cmd == nil || cmd.Process == nil || cmd.ProcessState != nil { + return nil + } + + err := cmd.Process.Signal(os.Interrupt) + if err != nil { + return fmt.Errorf("error killing command: %v", err) + } + + if cmd.ProcessState == nil { + cmd.out.Info("[command not stopped, waiting %s seconds before killing]\n", timeout) + t := timeout / 10 + for range 10 { + time.Sleep(t) + if cmd.ProcessState != nil { + return nil + } } if cmd.ProcessState == nil { - cmd.out.Info("[command not stopped, waiting %s seconds before killing]\n", timeout) - time.Sleep(timeout) - - if cmd.ProcessState == nil { - cmd.out.Info("[command not stopped, killing]\n") - err := cmd.Process.Kill() - if err != nil { - return fmt.Errorf("error killing command: %v", err) - } + cmd.out.Info("[command not stopped, killing]\n") + err := cmd.Process.Kill() + if err != nil { + return fmt.Errorf("error killing command: %v", err) } } }