Refactoring functions for buffer support in projects

This commit is contained in:
conventi 2016-09-17 16:46:26 +02:00
parent 04f7a2ed43
commit 01cb2287fb
3 changed files with 30 additions and 10 deletions

View File

@ -16,7 +16,6 @@ import (
// GoRun is an implementation of the bin execution // GoRun is an implementation of the bin execution
func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
stop := make(chan bool, 1)
var build *exec.Cmd var build *exec.Cmd
if len(p.Params) != 0 { if len(p.Params) != 0 {
build = exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)), p.Params...) 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() stdout, err := build.StdoutPipe()
stderr, err := build.StderrPipe() stderr, err := build.StderrPipe()
// Read stdout and stderr in same var
outputs := io.MultiReader(stdout, stderr)
if err != nil { if err != nil {
log.Println(Red(err.Error())) log.Println(Red(err.Error()))
return err return err
@ -47,35 +44,49 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
} }
close(runner) close(runner)
in := bufio.NewScanner(outputs) execOutput := bufio.NewScanner(stdout)
go func() { execError := bufio.NewScanner(stderr)
for in.Scan() {
scanner := func(stop chan bool, output *bufio.Scanner, isError bool) {
for output.Scan() {
select { select {
default: 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"] { 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"] { if p.Watcher.Output["file"] {
path := filepath.Join(p.base, Bp.Files["output"]) path := filepath.Join(p.base, Bp.Files["output"])
f := create(path) f := create(path)
t := time.Now() 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) log.Fatal(err)
} }
} }
} }
} }
close(stop) close(stop)
}() }
stopOutput := make(chan bool, 1)
stopError := make(chan bool, 1)
go scanner(stopOutput, execOutput, false)
go scanner(stopError, execError, true)
for { for {
select { select {
case <-channel: case <-channel:
return nil return nil
case <-stop: case <-stopOutput:
return nil
case <-stopError:
return nil return nil
} }
} }
} }
// GoBuild is an implementation of the "go build" // GoBuild is an implementation of the "go build"

View File

@ -41,6 +41,7 @@ type Project struct {
Test bool `yaml:"app_test,omitempty"` Test bool `yaml:"app_test,omitempty"`
Params []string `yaml:"app_params,omitempty"` Params []string `yaml:"app_params,omitempty"`
Watcher Watcher `yaml:"app_watcher,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"`
Buffer Buffer
} }
// Watcher struct defines the livereload's logic // Watcher struct defines the livereload's logic
@ -55,6 +56,13 @@ type Watcher struct {
Output map[string]bool `yaml:"output,omitempty"` Output map[string]bool `yaml:"output,omitempty"`
} }
// Buffer struct for buffering outputs
type Buffer struct {
StdOut []string
StdLog []string
StdErr []string
}
// Initialize the application // Initialize the application
func init() { func init() {
log.SetFlags(0) log.SetFlags(0)

View File

@ -30,6 +30,7 @@ func render(c echo.Context, path string) error {
return nil return nil
} }
// Server starting
func (s *Server) Start() { func (s *Server) Start() {
e := echo.New() e := echo.New()
e.Use(middleware.Gzip()) e.Use(middleware.Gzip())