From 455a243951400faf8c7546862a2f4234f63e2783 Mon Sep 17 00:00:00 2001 From: Kyoichiro Yamada Date: Thu, 20 Apr 2017 17:22:09 +0900 Subject: [PATCH] append "errorOutputPattern" property for projects --- watcher/exec.go | 32 ++++++++++++++++++++++---------- watcher/main.go | 35 ++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/watcher/exec.go b/watcher/exec.go index 2797c88..943f1ec 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "sync" "time" @@ -20,6 +21,19 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) var build *exec.Cmd var params []string var path = "" + isErrorText := func(string) bool { + return false + } + errRegexp, err := regexp.Compile(p.ErrorOutputPattern) + if err != nil { + msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(err.Error())) + out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Run"} + p.print("error", out, msg, "") + } else { + isErrorText = func(t string) bool { + return errRegexp.MatchString(t) + } + } for _, param := range p.Params { arr := strings.Fields(param) params = append(params, arr...) @@ -70,16 +84,14 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) stopOutput, stopError := make(chan bool, 1), make(chan bool, 1) scanner := func(stop chan bool, output *bufio.Scanner, isError bool) { for output.Scan() { - select { - default: - msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(output.Text())) - if isError { - out := BufferOut{Time: time.Now(), Text: output.Text(), Type: "Go Run"} - p.print("error", out, msg, "") - } else { - out := BufferOut{Time: time.Now(), Text: output.Text()} - p.print("out", out, msg, "") - } + text := output.Text() + msg := fmt.Sprintln(p.pname(p.Name, 3), ":", style.Blue.Regular(text)) + if isError || isErrorText(text) { + out := BufferOut{Time: time.Now(), Text: text, Type: "Go Run"} + p.print("error", out, msg, "") + } else { + out := BufferOut{Time: time.Now(), Text: text} + p.print("out", out, msg, "") } } close(stop) diff --git a/watcher/main.go b/watcher/main.go index 2364b4e..de700ba 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -33,23 +33,24 @@ type Blueprint struct { // Project defines the informations of a single project type Project struct { - settings.Settings `yaml:"-"` - LastChangedOn time.Time `yaml:"-" json:"-"` - base string - Name string `yaml:"name" json:"name"` - Path string `yaml:"path" json:"path"` - Fmt bool `yaml:"fmt" json:"fmt"` - Generate bool `yaml:"generate" json:"generate"` - Test bool `yaml:"test" json:"test"` - Bin bool `yaml:"bin" json:"bin"` - Build bool `yaml:"build" json:"build"` - Run bool `yaml:"run" json:"run"` - Params []string `yaml:"params,omitempty" json:"params,omitempty"` - Watcher Watcher `yaml:"watcher" json:"watcher"` - Streams Streams `yaml:"streams" json:"streams"` - Buffer Buffer `yaml:"-" json:"buffer"` - parent *Blueprint - path string + settings.Settings `yaml:"-"` + LastChangedOn time.Time `yaml:"-" json:"-"` + base string + Name string `yaml:"name" json:"name"` + Path string `yaml:"path" json:"path"` + Fmt bool `yaml:"fmt" json:"fmt"` + Generate bool `yaml:"generate" json:"generate"` + Test bool `yaml:"test" json:"test"` + Bin bool `yaml:"bin" json:"bin"` + Build bool `yaml:"build" json:"build"` + Run bool `yaml:"run" json:"run"` + Params []string `yaml:"params,omitempty" json:"params,omitempty"` + Watcher Watcher `yaml:"watcher" json:"watcher"` + Streams Streams `yaml:"streams" json:"streams"` + ErrorOutputPattern string `yaml:"errorOutputPattern" json:"errorOutputPattern"` + Buffer Buffer `yaml:"-" json:"buffer"` + parent *Blueprint + path string } // Watcher struct defines the livereload's logic