From 01cb2287fb83631ba9d6d29c6bee52cec633248d Mon Sep 17 00:00:00 2001 From: conventi Date: Sat, 17 Sep 2016 16:46:26 +0200 Subject: [PATCH] Refactoring functions for buffer support in projects --- cli/exec.go | 31 +++++++++++++++++++++---------- cli/main.go | 8 ++++++++ server/main.go | 1 + 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cli/exec.go b/cli/exec.go index 405af80..6e2450e 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -16,7 +16,6 @@ import ( // GoRun is an implementation of the bin execution func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { - stop := make(chan bool, 1) var build *exec.Cmd if len(p.Params) != 0 { build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)), p.Params...) @@ -35,8 +34,6 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) stdout, err := build.StdoutPipe() stderr, err := build.StderrPipe() - // Read stdout and stderr in same var - outputs := io.MultiReader(stdout, stderr) if err != nil { log.Println(Red(err.Error())) return err @@ -47,35 +44,49 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } close(runner) - in := bufio.NewScanner(outputs) - go func() { - for in.Scan() { + execOutput := bufio.NewScanner(stdout) + execError := bufio.NewScanner(stderr) + + scanner := func(stop chan bool, output *bufio.Scanner, isError bool) { + for output.Scan() { select { default: + if isError { + p.Buffer.StdErr = append(p.Buffer.StdErr, output.Text()) + } else { + p.Buffer.StdOut = append(p.Buffer.StdOut, output.Text()) + } if p.Watcher.Output["cli"] { - log.Println(pname(p.Name, 3), ":", BlueS(in.Text())) + log.Println(pname(p.Name, 3), ":", BlueS(output.Text())) } if p.Watcher.Output["file"] { path := filepath.Join(p.base, Bp.Files["output"]) f := create(path) t := time.Now() - if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + in.Text() + "\r\n"); err != nil { + if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil { log.Fatal(err) } } } } close(stop) - }() + } + stopOutput := make(chan bool, 1) + stopError := make(chan bool, 1) + go scanner(stopOutput, execOutput, false) + go scanner(stopError, execError, true) for { select { case <-channel: return nil - case <-stop: + case <-stopOutput: + return nil + case <-stopError: return nil } } + } // GoBuild is an implementation of the "go build" diff --git a/cli/main.go b/cli/main.go index e1cf7f9..739571e 100644 --- a/cli/main.go +++ b/cli/main.go @@ -41,6 +41,7 @@ type Project struct { Test bool `yaml:"app_test,omitempty"` Params []string `yaml:"app_params,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"` + Buffer Buffer } // Watcher struct defines the livereload's logic @@ -55,6 +56,13 @@ type Watcher struct { Output map[string]bool `yaml:"output,omitempty"` } +// Buffer struct for buffering outputs +type Buffer struct { + StdOut []string + StdLog []string + StdErr []string +} + // Initialize the application func init() { log.SetFlags(0) diff --git a/server/main.go b/server/main.go index a0998c8..860e28c 100644 --- a/server/main.go +++ b/server/main.go @@ -30,6 +30,7 @@ func render(c echo.Context, path string) error { return nil } +// Server starting func (s *Server) Start() { e := echo.New() e.Use(middleware.Gzip())