From c86bb98927240553c7d8f61c6cb281a9c9c7049d Mon Sep 17 00:00:00 2001 From: alessio Date: Tue, 15 Nov 2016 20:44:28 +0100 Subject: [PATCH] removed io writer --- watcher/exec.go | 46 +++++++++++++++------------------------------- watcher/watcher.go | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/watcher/exec.go b/watcher/exec.go index 8ed7a73..bbbd329 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -3,8 +3,6 @@ package cli import ( "bufio" "bytes" - "fmt" - "io" "log" "os" "os/exec" @@ -134,55 +132,42 @@ func (p *Project) goInstall() (string, error) { } // GoFmt is an implementation of the gofmt -func (p *Project) goFmt(path string) (io.Writer, error) { - defer func() { - p.sync() - }() - var out bytes.Buffer +func (p *Project) goFmt(path string) (string, error) { + var out, stderr bytes.Buffer build := exec.Command("gofmt", "-s", "-w", "-e", path) build.Dir = p.base build.Stdout = &out - build.Stderr = &out + build.Stderr = &stderr if err := build.Run(); err != nil { - fmt.Print("append") - p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()}) - return build.Stderr, err + return stderr.String(), err } - return nil, nil + return "", nil } // GoTest is an implementation of the go test -func (p *Project) goTest(path string) (io.Writer, error) { - defer func() { - p.sync() - }() - var out bytes.Buffer +func (p *Project) goTest(path string) (string, error) { + var out, stderr bytes.Buffer build := exec.Command("go", "test") build.Dir = path build.Stdout = &out - build.Stderr = &out + build.Stderr = &stderr if err := build.Run(); err != nil { - p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()}) - return build.Stdout, err + return stderr.String(), err } - return nil, nil + return "", nil } // GoGenerate is an implementation of the go test -func (p *Project) goGenerate(path string) (io.Writer, error) { - defer func() { - p.sync() - }() - var out bytes.Buffer +func (p *Project) goGenerate(path string) (string, error) { + var out, stderr bytes.Buffer build := exec.Command("go", "generate") build.Dir = path build.Stdout = &out - build.Stderr = &out + build.Stderr = &stderr if err := build.Run(); err != nil { - p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()}) - return build.Stdout, err + return stderr.String(), err } - return nil, nil + return "", nil } // Cmds exec a list of defined commands @@ -193,7 +178,6 @@ func (p *Project) cmds(cmds []string) (errors []error) { build := exec.Command(c[0], c[1:]...) build.Dir = p.base if err := build.Run(); err != nil { - p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error()}) errors = append(errors, err) return errors } diff --git a/watcher/watcher.go b/watcher/watcher.go index 30f0ef8..77f3ee9 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -86,11 +86,16 @@ func (p *Project) watching() { // Install calls an implementation of the "go install" func (p *Project) install(channel chan bool, wr *sync.WaitGroup) { + defer func() { + p.sync() + }() if p.Bin { log.Println(p.pname(p.Name, 1), ":", "Installing..") start := time.Now() - if std, err := p.goInstall(); err != nil { - log.Println(p.pname(p.Name, 1), ":", fmt.Sprint(p.Red.Bold(err)), std) + if stream, err := p.goInstall(); err != nil { + p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream}) + log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Install"), p.Red.Regular(err.Error())) + fmt.Println(stream) wr.Done() } else { log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Installed")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) @@ -114,11 +119,16 @@ func (p *Project) install(channel chan bool, wr *sync.WaitGroup) { // Build calls an implementation of the "go build" func (p *Project) build() { + defer func() { + p.sync() + }() if p.Build { log.Println(p.pname(p.Name, 1), ":", "Building..") start := time.Now() - if std, err := p.goBuild(); err != nil { - log.Println(p.pname(p.Name, 1), ":", fmt.Sprint(p.Red.Bold(err)), std) + if stream, err := p.goBuild(); err != nil { + p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream}) + log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Build"), p.Red.Regular(err.Error())) + fmt.Println(stream) } else { log.Println(p.pname(p.Name, 5), ":", p.Green.Regular("Builded")+" after", p.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) } @@ -128,9 +138,13 @@ func (p *Project) build() { // Fmt calls an implementation of the "go fmt" func (p *Project) fmt(path string) error { + defer func() { + p.sync() + }() if p.Fmt { if stream, err := p.goFmt(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("go Fmt"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) + p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream}) + log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Fmt"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err } @@ -140,9 +154,13 @@ func (p *Project) fmt(path string) error { // Generate calls an implementation of the "go generate" func (p *Project) generate(path string) error { + defer func() { + p.sync() + }() if p.Generate { if stream, err := p.goGenerate(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("go Generate"), p.Red.Bold("there are some errors in"), ":", p.Magenta.Bold(path)) + p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream}) + log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Generate"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err } @@ -157,6 +175,7 @@ func (p *Project) cmd(exit chan bool) { cast := func(commands []string) { if errs := p.cmds(commands); errs != nil { for _, err := range errs { + log.Println(p.pname(p.Name, 2), p.Red.Bold(err)) } } @@ -181,9 +200,13 @@ func (p *Project) cmd(exit chan bool) { // Test calls an implementation of the "go test" func (p *Project) test(path string) error { + defer func() { + p.sync() + }() if p.Test { if stream, err := p.goTest(path); err != nil { - log.Println(p.pname(p.Name, 1), p.Red.Bold("go Test fails in "), ":", p.Magenta.Bold(path)) + p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream}) + log.Println(p.pname(p.Name, 1), p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path)) fmt.Println(stream) return err }