fmt and bug fix

This commit is contained in:
asoseil 2017-12-04 15:53:59 +01:00
parent 6088ce8f00
commit a8c4aadef3
14 changed files with 140 additions and 188 deletions

View File

@ -542,50 +542,6 @@ func setup(c *cli.Context) (err error) {
return nil return nil
}, },
}, },
{
Before: func(d interact.Context) error {
d.SetDef(false, realize.Green.Regular("(n)"))
return nil
},
Quest: interact.Quest{
Options: realize.Yellow.Regular("[y/n]"),
Msg: "Enable go fix",
Resolve: func(d interact.Context) bool {
val, _ := d.Ans().Bool()
return val
},
},
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef("", realize.Green.Regular("(none)"))
return nil
},
Quest: interact.Quest{
Options: realize.Yellow.Regular("[string]"),
Msg: "Fix additional arguments",
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().String()
if err != nil {
return d.Err()
}
if val != "" {
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fix.Args = append(r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fix.Args, val)
}
return nil
},
},
},
Action: func(d interact.Context) interface{} {
val, err := d.Ans().Bool()
if err != nil {
return d.Err()
}
r.Schema.Projects[len(r.Schema.Projects)-1].Tools.Fix.Status = val
return nil
},
},
{ {
Before: func(d interact.Context) error { Before: func(d interact.Context) error {
d.SetDef(false, realize.Green.Regular("(n)")) d.SetDef(false, realize.Green.Regular("(n)"))

View File

@ -3,14 +3,15 @@ package realize
import ( import (
"fmt" "fmt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/go-siris/siris/core/errors"
"go/build" "go/build"
"log" "log"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"github.com/go-siris/siris/core/errors"
) )
var ( var (
@ -83,22 +84,19 @@ func (r *Realize) Stop() error{
// Start realize workflow // Start realize workflow
func (r *Realize) Start() error { func (r *Realize) Start() error {
if len(r.Schema.Projects) > 0 { if len(r.Schema.Projects) > 0 {
var wg sync.WaitGroup
r.exit = make(chan os.Signal, 1) r.exit = make(chan os.Signal, 1)
signal.Notify(r.exit, os.Interrupt) signal.Notify(r.exit, os.Interrupt)
wg.Add(len(r.Schema.Projects))
for k := range r.Schema.Projects { for k := range r.Schema.Projects {
r.Schema.Projects[k].parent = r r.Schema.Projects[k].parent = r
go r.Schema.Projects[k].Watch(r.exit) go r.Schema.Projects[k].Watch(&wg)
} }
for { wg.Wait()
select {
case <-r.exit:
return nil
}
}
return nil
} else { } else {
return errors.New("there are no projects") return errors.New("there are no projects")
} }
return nil
} }
// Prefix a given string with tool name // Prefix a given string with tool name

View File

@ -15,6 +15,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
@ -270,7 +271,7 @@ func (p *Project) Reload(path string, stop <-chan bool) {
} }
// Watch a project // Watch a project
func (p *Project) Watch(exit chan os.Signal) { func (p *Project) Watch(wg *sync.WaitGroup) {
var err error var err error
// change channel // change channel
p.stop = make(chan bool) p.stop = make(chan bool)
@ -331,11 +332,12 @@ L:
} }
case err := <-p.watcher.Errors(): case err := <-p.watcher.Errors():
p.Err(err) p.Err(err)
case <-exit: case <-p.parent.exit:
p.After() p.After()
break L break L
} }
} }
wg.Done()
} }
// Validate a file path // Validate a file path
@ -462,17 +464,12 @@ func (p *Project) cmd(stop <-chan bool, flag string, global bool) {
return return
case r := <-result: case r := <-result:
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold("Command"), Green.Bold("\"")+r.Name+Green.Bold("\"")) msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold("Command"), Green.Bold("\"")+r.Name+Green.Bold("\""))
out = BufferOut{Time: time.Now(), Text: r.Name, Type: flag}
if r.Err != nil { if r.Err != nil {
p.stamp("error", out, msg, "")
out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: flag} out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: flag}
p.stamp("error", out, "", fmt.Sprintln(Red.Regular(r.Err.Error()))) p.stamp("error", out, msg, fmt.Sprint(Red.Regular(r.Err.Error())))
}
if r.Out != "" {
out = BufferOut{Time: time.Now(), Text: r.Out, Type: flag}
p.stamp("log", out, "", fmt.Sprintln(r.Out))
} else { } else {
p.stamp("log", out, msg, "") out = BufferOut{Time: time.Now(), Text: r.Out, Type: flag}
p.stamp("log", out, msg, fmt.Sprint(r.Out))
} }
} }
} }

View File

@ -3,12 +3,13 @@ package realize
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/fsnotify/fsnotify"
"log" "log"
"os" "os"
"strings"
"testing"
"github.com/fsnotify/fsnotify"
"os/signal" "os/signal"
"strings"
"sync"
"testing"
"time" "time"
) )
@ -137,6 +138,7 @@ func TestProject_Validate(t *testing.T) {
} }
func TestProject_Watch(t *testing.T) { func TestProject_Watch(t *testing.T) {
var wg sync.WaitGroup
r := Realize{} r := Realize{}
r.Projects = append(r.Projects, Project{ r.Projects = append(r.Projects, Project{
parent: &r, parent: &r,
@ -147,5 +149,8 @@ func TestProject_Watch(t *testing.T) {
time.Sleep(100) time.Sleep(100)
close(r.exit) close(r.exit)
}() }()
r.Projects[0].Watch(r.exit) wg.Add(1)
// test before after and file change
r.Projects[0].Watch(&wg)
wg.Wait()
} }

View File

@ -1,10 +1,10 @@
package realize package realize
import ( import (
"testing"
"flag" "flag"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"path/filepath" "path/filepath"
"testing"
) )
func TestSchema_Add(t *testing.T) { func TestSchema_Add(t *testing.T) {

View File

@ -1,10 +1,11 @@
package realize package realize
import ( import (
"testing"
"net/http" "net/http"
"runtime" "runtime"
"testing"
) )
func TestServer_Start(t *testing.T) { func TestServer_Start(t *testing.T) {
s := Server{ s := Server{
Host: "localhost", Host: "localhost",

View File

@ -1,11 +1,11 @@
package realize package realize
import ( import (
"io/ioutil"
"math/rand"
"os"
"testing" "testing"
"time" "time"
"math/rand"
"io/ioutil"
"os"
) )
// Rand is used for generate a random string // Rand is used for generate a random string

View File

@ -1,4 +1,5 @@
// +build !windows // +build !windows
package realize package realize
import ( import (

View File

@ -24,7 +24,6 @@ type Tool struct {
// Tools go // Tools go
type Tools struct { type Tools struct {
Fix Tool `yaml:"fix,omitempty" json:"fix,omitempty"`
Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"` Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"`
Vet Tool `yaml:"vet,omitempty" json:"vet,omitempty"` Vet Tool `yaml:"vet,omitempty" json:"vet,omitempty"`
Fmt Tool `yaml:"fmt,omitempty" json:"fmt,omitempty"` Fmt Tool `yaml:"fmt,omitempty" json:"fmt,omitempty"`
@ -52,17 +51,10 @@ func (t *Tools) Setup() {
t.Generate.cmd = replace([]string{"go", "generate"}, t.Generate.Method) t.Generate.cmd = replace([]string{"go", "generate"}, t.Generate.Method)
t.Generate.Args = split([]string{}, t.Generate.Args) t.Generate.Args = split([]string{}, t.Generate.Args)
} }
// go fix
if t.Fix.Status {
t.Fix.name = "Fix"
t.Fix.isTool = true
t.Fix.cmd = replace([]string{"go fix"}, t.Fix.Method)
t.Fix.Args = split([]string{}, t.Fix.Args)
}
// go fmt // go fmt
if t.Fmt.Status { if t.Fmt.Status {
if len(t.Fmt.Args) == 0 { if len(t.Fmt.Args) == 0 {
t.Fmt.Args = []string{"-s", "-w", "-e", "./"} t.Fmt.Args = []string{"-s", "-w", "-e"}
} }
t.Fmt.name = "Fmt" t.Fmt.name = "Fmt"
t.Fmt.isTool = true t.Fmt.isTool = true
@ -99,10 +91,13 @@ func (t *Tools) Setup() {
// Exec a go tool // Exec a go tool
func (t *Tool) Exec(path string, stop <-chan bool) (response Response) { func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
if t.dir && filepath.Ext(path) != "" { if t.dir {
if filepath.Ext(path) != "" {
path = filepath.Dir(path) path = filepath.Dir(path)
} }
if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") { } else if !strings.HasSuffix(path, ".go") {
return
}
args := []string{} args := []string{}
if strings.HasSuffix(path, ".go") { if strings.HasSuffix(path, ".go") {
args = append(t.Args, path) args = append(t.Args, path)
@ -111,7 +106,7 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
if s := ext(path); s == "" || s == "go" { if s := ext(path); s == "" || s == "go" {
var out, stderr bytes.Buffer var out, stderr bytes.Buffer
done := make(chan error) done := make(chan error)
args = append(t.cmd, t.Args...) args = append(t.cmd, args...)
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
if t.Dir != "" { if t.Dir != "" {
cmd.Dir, _ = filepath.Abs(t.Dir) cmd.Dir, _ = filepath.Abs(t.Dir)
@ -140,7 +135,6 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
} }
} }
} }
}
return return
} }