gofmt method

This commit is contained in:
alessio 2016-08-21 17:12:11 +02:00
parent 9ef0d84ab6
commit 5450300aef
2 changed files with 35 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package realize
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -20,6 +21,7 @@ type Project struct {
Run bool `yaml:"app_run,omitempty"` Run bool `yaml:"app_run,omitempty"`
Bin bool `yaml:"app_bin,omitempty"` Bin bool `yaml:"app_bin,omitempty"`
Build bool `yaml:"app_build,omitempty"` Build bool `yaml:"app_build,omitempty"`
Fmt bool `yaml:"app_fmt,omitempty"`
Watcher Watcher `yaml:"app_watcher,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 build.Dir = p.base
defer func() { defer func() {
if err := build.Process.Kill(); err != nil { 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")) log.Println(pname(p.Name, 2), ":", RedS("Stopped"))
wr.Done() 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 { func (p *Project) GoBuild() error {
var out bytes.Buffer var out bytes.Buffer
@ -90,7 +92,7 @@ func (p *Project) GoBuild() error {
return nil return nil
} }
// GoInstall an implementation of the "go install" // GoInstall is an implementation of the "go install"
func (p *Project) GoInstall() error { func (p *Project) GoInstall() error {
var out bytes.Buffer var out bytes.Buffer
base, _ := os.Getwd() base, _ := os.Getwd()
@ -109,3 +111,15 @@ func (p *Project) GoInstall() error {
} }
return nil 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
}

View File

@ -69,10 +69,13 @@ func (p *Project) Watching() {
} }
routines := func() { routines := func() {
channel = make(chan bool) channel = make(chan bool)
err = p.fmt()
if err == nil {
wr.Add(1) wr.Add(1)
go p.build() go p.build()
go p.install(channel, &wr) go p.install(channel, &wr)
} }
}
end := func() { end := func() {
watcher.Close() watcher.Close()
wg.Done() wg.Done()
@ -167,6 +170,18 @@ func (p *Project) build() {
return 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 // Ignore validates a path
func (p *Project) ignore(str string) bool { func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore { for _, v := range p.Watcher.Ignore {