diff --git a/watcher/exec.go b/watcher/exec.go index 4523819..9cadb2b 100644 --- a/watcher/exec.go +++ b/watcher/exec.go @@ -145,37 +145,11 @@ func (p *Project) goInstall() (string, error) { return "", nil } -// GoFmt is an implementation of the gofmt -func (p *Project) goFmt(path string) (string, error) { +// GoTools is used for run go methods such as fmt, test, generate... +func (p *Project) goTools(dir string, name string, cmd ...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 = &stderr - if err := build.Run(); err != nil { - return stderr.String(), err - } - return "", nil -} - -// GoTest is an implementation of the go test -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 = &stderr - if err := build.Run(); err != nil { - return stderr.String(), err - } - return "", nil -} - -// GoGenerate is an implementation of the go test -func (p *Project) goGenerate(path string) (string, error) { - var out, stderr bytes.Buffer - build := exec.Command("go", "generate") - build.Dir = path + build := exec.Command(name, cmd...) + build.Dir = dir build.Stdout = &out build.Stderr = &stderr if err := build.Run(); err != nil { diff --git a/watcher/watcher.go b/watcher/watcher.go index a977713..637c2f6 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -145,7 +145,7 @@ func (p *Project) fmt(path string) error { p.sync() }() if p.Fmt { - if stream, err := p.goFmt(path); err != nil { + if stream, err := p.goTools(p.base, "gofmt", "-s", "-w", "-e", path); err != nil { msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Fmt"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream} p.print("error", out, msg, stream) @@ -161,7 +161,7 @@ func (p *Project) generate(path string) error { p.sync() }() if p.Generate { - if stream, err := p.goGenerate(path); err != nil { + if stream, err := p.goTools(path, "go", "generate"); err != nil { msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Generate"), p.Red.Regular("there are some errors in"), ":", p.Magenta.Bold(path)) out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream} p.print("error", out, msg, stream) @@ -171,6 +171,22 @@ func (p *Project) generate(path string) error { return nil } +// 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.goTools(path, "go", "test"); err != nil { + msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path)) + out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream} + p.print("error", out, msg, stream) + return err + } + } + return nil +} + // Cmd calls an wrapper for execute the commands after/before func (p *Project) cmd(exit chan bool) { c := make(chan os.Signal, 2) @@ -202,22 +218,6 @@ 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 { - msg := fmt.Sprintln(p.pname(p.Name, 2), ":", p.Red.Bold("Go Test"), p.Red.Regular("there are some errors in "), ":", p.Magenta.Bold(path)) - out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Test", Stream: stream} - p.print("error", out, msg, stream) - return err - } - } - return nil -} - // Walks the file tree of a project func (p *Project) walks(watcher *fsnotify.Watcher) error { var files, folders int64 @@ -307,6 +307,7 @@ func (p *Project) pname(name string, color int) string { return name } +// Print on files func (p *Project) print(t string, o BufferOut, msg string, stream string) { switch t { case "out":