Refactor restart logic into a function

This commit is contained in:
Max Claus Nunes 2018-06-18 23:53:49 -03:00
parent 853e82c957
commit 99cc752b38
1 changed files with 17 additions and 9 deletions

26
main.go
View File

@ -172,15 +172,7 @@ func runGaper(cfg *Config) error {
select {
case event := <-watcher.Events:
logger.Debug("Detected new changed file: ", event)
if err = runner.Kill(); err != nil {
return fmt.Errorf("kill error: %v", err)
}
if err = builder.Build(); err != nil {
return fmt.Errorf("build error: %v", err)
}
if _, err = runner.Run(); err != nil {
return fmt.Errorf("run error: %v", err)
}
restart(builder, runner)
case err := <-watcher.Errors:
return fmt.Errorf("error on watching files: %v", err)
default:
@ -190,6 +182,22 @@ func runGaper(cfg *Config) error {
}
}
func restart(builder Builder, runner Runner) error {
if err := runner.Kill(); err != nil {
return fmt.Errorf("kill error: %v", err)
}
if err := builder.Build(); err != nil {
return fmt.Errorf("build error: %v", err)
}
if _, err := runner.Run(); err != nil {
return fmt.Errorf("run error: %v", err)
}
return nil
}
func shutdown(runner Runner) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)