From 5450300aef446eababde29f9d8d3b18c360755ab Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 21 Aug 2016 17:12:11 +0200 Subject: [PATCH] gofmt method --- realize/project.go | 20 +++++++++++++++++--- realize/watcher.go | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/realize/project.go b/realize/project.go index 331ae72..a8fc9f7 100644 --- a/realize/project.go +++ b/realize/project.go @@ -3,6 +3,7 @@ package realize import ( "bufio" "bytes" + "io" "log" "os" "os/exec" @@ -20,6 +21,7 @@ type Project struct { Run bool `yaml:"app_run,omitempty"` Bin bool `yaml:"app_bin,omitempty"` Build bool `yaml:"app_build,omitempty"` + Fmt bool `yaml:"app_fmt,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"` } @@ -39,7 +41,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) build.Dir = p.base defer func() { if err := build.Process.Kill(); err != nil { - log.Fatal("failed to stop: ", err) + log.Fatal(Red("failed to stop: "), Red(err)) } log.Println(pname(p.Name, 2), ":", RedS("Stopped")) wr.Done() @@ -77,7 +79,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) } } -// GoBuild an implementation of the "go build" +// GoBuild is an implementation of the "go build" func (p *Project) GoBuild() error { var out bytes.Buffer @@ -90,7 +92,7 @@ func (p *Project) GoBuild() error { return nil } -// GoInstall an implementation of the "go install" +// GoInstall is an implementation of the "go install" func (p *Project) GoInstall() error { var out bytes.Buffer base, _ := os.Getwd() @@ -109,3 +111,15 @@ func (p *Project) GoInstall() error { } return nil } + +func (p *Project) GoFmt() (error, io.Writer) { + var out bytes.Buffer + build := exec.Command("gofmt", "-s", "-w", "-e", ".") + build.Dir = p.base + build.Stdout = &out + build.Stderr = &out + if err := build.Run(); err != nil { + return err, build.Stderr + } + return nil, nil +} diff --git a/realize/watcher.go b/realize/watcher.go index d8709fe..85cae20 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -69,9 +69,12 @@ func (p *Project) Watching() { } routines := func() { channel = make(chan bool) - wr.Add(1) - go p.build() - go p.install(channel, &wr) + err = p.fmt() + if err == nil { + wr.Add(1) + go p.build() + go p.install(channel, &wr) + } } end := func() { watcher.Close() @@ -167,6 +170,18 @@ func (p *Project) build() { return } +// Build call an implementation of the "gofmt" +func (p *Project) fmt() error { + if p.Fmt { + if err, msg := p.GoFmt(); err != nil { + log.Println(pname(p.Name, 1), Red("There are some errors:")) + fmt.Println(msg) + return err + } + } + return nil +} + // Ignore validates a path func (p *Project) ignore(str string) bool { for _, v := range p.Watcher.Ignore {