From 608a1a64d01c06491dfc1dc012f36d2f1ad68d26 Mon Sep 17 00:00:00 2001 From: alessio Date: Sat, 17 Sep 2016 21:07:22 +0200 Subject: [PATCH] watcher exit --- cli/exec.go | 9 +++------ cli/watcher.go | 10 ++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cli/exec.go b/cli/exec.go index 6e2450e..81b9345 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -44,8 +44,8 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } close(runner) - execOutput := bufio.NewScanner(stdout) - execError := bufio.NewScanner(stderr) + execOutput, execError := bufio.NewScanner(stdout), bufio.NewScanner(stderr) + stopOutput, stopError := make(chan bool, 1), make(chan bool, 1) scanner := func(stop chan bool, output *bufio.Scanner, isError bool) { for output.Scan() { @@ -60,7 +60,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) log.Println(pname(p.Name, 3), ":", BlueS(output.Text())) } if p.Watcher.Output["file"] { - path := filepath.Join(p.base, Bp.Files["output"]) + path := filepath.Join(p.base, B.Files["output"]) f := create(path) t := time.Now() if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil { @@ -71,8 +71,6 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } close(stop) } - stopOutput := make(chan bool, 1) - stopError := make(chan bool, 1) go scanner(stopOutput, execOutput, false) go scanner(stopError, execError, true) @@ -86,7 +84,6 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) return nil } } - } // GoBuild is an implementation of the "go build" diff --git a/cli/watcher.go b/cli/watcher.go index 334d06b..9437bd6 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -25,7 +25,7 @@ func (p *Project) watching() { if err != nil { log.Println(strings.ToUpper(pname(p.Name, 1)), ":", Red(err.Error())) } - channel := make(chan bool, 1) + channel, exit := make(chan bool, 1), make(chan bool, 1) if err != nil { log.Println(pname(p.Name, 1), ":", Red(err.Error())) } @@ -35,7 +35,7 @@ func (p *Project) watching() { } defer end() - p.cmd() + p.cmd(exit) err = p.walks(watcher) if err != nil { fmt.Println(pname(p.Name, 1), ":", Red(err.Error())) @@ -83,6 +83,8 @@ func (p *Project) watching() { } case err := <-watcher.Errors: log.Println(Red(err.Error())) + case <-exit: + return } } } @@ -141,7 +143,7 @@ func (p *Project) fmt(path string) error { } // Cmd calls an wrapper for execute the commands after/before -func (p *Project) cmd() { +func (p *Project) cmd(exit chan bool) { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) cast := func(commands []string) { @@ -163,7 +165,7 @@ func (p *Project) cmd() { if len(p.Watcher.After) > 0 { cast(p.Watcher.After) } - os.Exit(1) + close(exit) } } }()