goTest, goGenerate, goFmt merged

This commit is contained in:
alessio 2016-12-17 17:15:56 +01:00
parent 9fd616f552
commit b8e728cfd1
2 changed files with 23 additions and 48 deletions

View File

@ -145,37 +145,11 @@ func (p *Project) goInstall() (string, error) {
return "", nil return "", nil
} }
// GoFmt is an implementation of the gofmt // GoTools is used for run go methods such as fmt, test, generate...
func (p *Project) goFmt(path string) (string, error) { func (p *Project) goTools(dir string, name string, cmd ...string) (string, error) {
var out, stderr bytes.Buffer var out, stderr bytes.Buffer
build := exec.Command("gofmt", "-s", "-w", "-e", path) build := exec.Command(name, cmd...)
build.Dir = p.base build.Dir = dir
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.Stdout = &out build.Stdout = &out
build.Stderr = &stderr build.Stderr = &stderr
if err := build.Run(); err != nil { if err := build.Run(); err != nil {

View File

@ -145,7 +145,7 @@ func (p *Project) fmt(path string) error {
p.sync() p.sync()
}() }()
if p.Fmt { 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)) 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} out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Fmt", Stream: stream}
p.print("error", out, msg, stream) p.print("error", out, msg, stream)
@ -161,7 +161,7 @@ func (p *Project) generate(path string) error {
p.sync() p.sync()
}() }()
if p.Generate { 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)) 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} out := BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: "Go Generate", Stream: stream}
p.print("error", out, msg, stream) p.print("error", out, msg, stream)
@ -171,6 +171,22 @@ func (p *Project) generate(path string) error {
return nil 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 // Cmd calls an wrapper for execute the commands after/before
func (p *Project) cmd(exit chan bool) { func (p *Project) cmd(exit chan bool) {
c := make(chan os.Signal, 2) 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 // Walks the file tree of a project
func (p *Project) walks(watcher *fsnotify.Watcher) error { func (p *Project) walks(watcher *fsnotify.Watcher) error {
var files, folders int64 var files, folders int64
@ -307,6 +307,7 @@ func (p *Project) pname(name string, color int) string {
return name return name
} }
// Print on files
func (p *Project) print(t string, o BufferOut, msg string, stream string) { func (p *Project) print(t string, o BufferOut, msg string, stream string) {
switch t { switch t {
case "out": case "out":