gofmt method
This commit is contained in:
parent
9ef0d84ab6
commit
5450300aef
|
@ -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
|
||||
}
|
||||
|
|
|
@ -69,10 +69,13 @@ func (p *Project) Watching() {
|
|||
}
|
||||
routines := func() {
|
||||
channel = make(chan bool)
|
||||
err = p.fmt()
|
||||
if err == nil {
|
||||
wr.Add(1)
|
||||
go p.build()
|
||||
go p.install(channel, &wr)
|
||||
}
|
||||
}
|
||||
end := func() {
|
||||
watcher.Close()
|
||||
wg.Done()
|
||||
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue