diff --git a/cli.go b/cli.go index 5b9795d..6dc7d40 100644 --- a/cli.go +++ b/cli.go @@ -1093,12 +1093,12 @@ func start(c *cli.Context) (err error) { project := r.Schema.New(c) // Add to projects list r.Schema.Add(project) - } - // save config - if !c.Bool("no-config") { - err = r.Settings.Write(r) - if err != nil { - return err + // save config + if !c.Bool("no-config") { + err = r.Settings.Write(r) + if err != nil { + return err + } } } // config and start server diff --git a/projects.go b/projects.go index c6d8b90..358972b 100644 --- a/projects.go +++ b/projects.go @@ -234,24 +234,33 @@ func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) { var start time.Time result := make(chan Response) go func() { - select { - case r := <-result: - if r.Err != nil { - msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(r.Err)) - out := BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: "Go Run"} - p.stamp("error", out, msg, "") - } - if r.Out != "" { - msg := fmt.Sprintln(p.pname(p.Name, 3), ":", blue.regular(r.Out)) - out := BufferOut{Time: time.Now(), Text: r.Out, Type: "Go Run"} - p.stamp("out", out, msg, "") + for { + select { + case <-stop: + return + case r := <-result: + if r.Err != nil { + msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(r.Err)) + out := BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: "Go Run"} + p.stamp("error", out, msg, "") + } + if r.Out != "" { + msg := fmt.Sprintln(p.pname(p.Name, 3), ":", blue.regular(r.Out)) + out := BufferOut{Time: time.Now(), Text: r.Out, Type: "Go Run"} + p.stamp("out", out, msg, "") + } } } }() go func() { log.Println(p.pname(p.Name, 1), ":", "Running..") start = time.Now() - p.Run(p.Path, stop) + err := p.Run(p.Path, result, stop) + if err != nil{ + msg := fmt.Sprintln(p.pname(p.Name, 2), ":", red.regular(err)) + out := BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Run"} + p.stamp("error", out, msg, "") + } }() } if done { @@ -261,13 +270,13 @@ func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) { } // Run a project -func (p *Project) Run(path string, stop <-chan bool) (response chan Response) { +func (p *Project) Run(path string, stream chan Response, stop <-chan bool) (err error) { var args []string var build *exec.Cmd var r Response defer func() { - if err := build.Process.Kill(); err != nil { - r.Err = err + if e := build.Process.Kill(); e != nil{ + err = e } }() @@ -278,8 +287,7 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) { errRegexp, err := regexp.Compile(p.ErrorOutputPattern) if err != nil { r.Err = err - response <- r - r.Err = nil + stream <- r } else { isErrorText = func(t string) bool { return errRegexp.MatchString(t) @@ -309,20 +317,17 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) { } else if _, err = os.Stat(path + RExtWin); err == nil { build = exec.Command(path+RExtWin, args...) } else { - r.Err = errors.New("project not found") - return + return errors.New("project not found") } } // scan project stream stdout, err := build.StdoutPipe() stderr, err := build.StderrPipe() if err != nil { - r.Err = err - return + return err } if err := build.Start(); err != nil { - r.Err = err - return + return err } execOutput, execError := bufio.NewScanner(stdout), bufio.NewScanner(stderr) stopOutput, stopError := make(chan bool, 1), make(chan bool, 1) @@ -331,11 +336,11 @@ func (p *Project) Run(path string, stop <-chan bool) (response chan Response) { text := output.Text() if isError && !isErrorText(text) { r.Err = errors.New(text) - response <- r + stream <- r r.Err = nil } else { r.Out = text - response <- r + stream <- r r.Out = "" } } @@ -516,9 +521,10 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) { } } +// Print with time after func (r *Response) printAfter(start time.Time, p *Project) { if r.Err != nil { - msg = fmt.Sprintln(p.pname(p.Name, 2), ":", red.bold(r.Name), red.regular(r.Err.Error())) + msg = fmt.Sprintln(p.pname(p.Name, 2), ":", red.bold(r.Name), "\n", r.Err.Error()) out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: r.Name, Stream: r.Out} p.stamp("error", out, msg, r.Out) } else { diff --git a/realize.go b/realize.go index 933cbc6..21c45d0 100644 --- a/realize.go +++ b/realize.go @@ -33,6 +33,7 @@ type ( LogWriter struct{} ) +// init check func init() { // custom log log.SetFlags(0) diff --git a/schema.go b/schema.go index 338298b..b298489 100644 --- a/schema.go +++ b/schema.go @@ -5,10 +5,12 @@ import ( "gopkg.in/urfave/cli.v2" "path/filepath" "reflect" + "fmt" ) +// Schema projects list type Schema struct { - Projects []Project `yaml:"projects" json:"projects"` + Projects []Project `yaml:"schema" json:"schema"` } // Add a project if unique diff --git a/style.go b/style.go index e35a35c..f972837 100644 --- a/style.go +++ b/style.go @@ -13,12 +13,15 @@ var ( magenta = colorBase(color.FgHiMagenta) ) +// ColorBase type type colorBase color.Attribute +// Regular font with a color func (c colorBase) regular(a ...interface{}) string { return color.New(color.Attribute(c)).Sprint(a...) } +// Bold font with a color func (c colorBase) bold(a ...interface{}) string { return color.New(color.Attribute(c), color.Bold).Sprint(a...) } diff --git a/tools.go b/tools.go index 7313a58..fd8c982 100644 --- a/tools.go +++ b/tools.go @@ -8,6 +8,7 @@ import ( "strings" ) +// Tool info type Tool struct { Args []string `yaml:"args,omitempty" json:"args,omitempty"` Method string `yaml:"method,omitempty" json:"method,omitempty"` @@ -19,6 +20,7 @@ type Tool struct { name string } +// Tools go type Tools struct { Fix Tool `yaml:"fix,omitempty" json:"fix,omitempty"` Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"` @@ -31,6 +33,7 @@ type Tools struct { Run bool `yaml:"run,omitempty" json:"run,omitempty"` } +// Setup go tools func (t *Tools) Setup() { // go clean if t.Clean.Status { @@ -92,6 +95,7 @@ func (t *Tools) Setup() { } } +// Exec a go tool func (t *Tool) Exec(path string, stop <-chan bool) (response Response) { if t.dir && filepath.Ext(path) != "" { path = filepath.Dir(path) @@ -132,6 +136,7 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) { return } +// Compile is used for build and install func (t *Tool) Compile(path string, stop <-chan bool) (response Response) { var out bytes.Buffer var stderr bytes.Buffer