fmt and bug fix
This commit is contained in:
parent
6088ce8f00
commit
a8c4aadef3
44
realize.go
44
realize.go
|
@ -542,50 +542,6 @@ func setup(c *cli.Context) (err error) {
|
|||
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 {
|
||||
d.SetDef(false, realize.Green.Regular("(n)"))
|
||||
|
|
|
@ -3,14 +3,15 @@ package realize
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/go-siris/siris/core/errors"
|
||||
"go/build"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"github.com/go-siris/siris/core/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -83,22 +84,19 @@ func (r *Realize) Stop() error{
|
|||
// Start realize workflow
|
||||
func (r *Realize) Start() error {
|
||||
if len(r.Schema.Projects) > 0 {
|
||||
var wg sync.WaitGroup
|
||||
r.exit = make(chan os.Signal, 1)
|
||||
signal.Notify(r.exit, os.Interrupt)
|
||||
wg.Add(len(r.Schema.Projects))
|
||||
for k := range r.Schema.Projects {
|
||||
r.Schema.Projects[k].parent = r
|
||||
go r.Schema.Projects[k].Watch(r.exit)
|
||||
go r.Schema.Projects[k].Watch(&wg)
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-r.exit:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
wg.Wait()
|
||||
} else {
|
||||
return errors.New("there are no projects")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Prefix a given string with tool name
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -270,7 +271,7 @@ func (p *Project) Reload(path string, stop <-chan bool) {
|
|||
}
|
||||
|
||||
// Watch a project
|
||||
func (p *Project) Watch(exit chan os.Signal) {
|
||||
func (p *Project) Watch(wg *sync.WaitGroup) {
|
||||
var err error
|
||||
// change channel
|
||||
p.stop = make(chan bool)
|
||||
|
@ -331,11 +332,12 @@ L:
|
|||
}
|
||||
case err := <-p.watcher.Errors():
|
||||
p.Err(err)
|
||||
case <-exit:
|
||||
case <-p.parent.exit:
|
||||
p.After()
|
||||
break L
|
||||
}
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
// Validate a file path
|
||||
|
@ -462,17 +464,12 @@ func (p *Project) cmd(stop <-chan bool, flag string, global bool) {
|
|||
return
|
||||
case r := <-result:
|
||||
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 {
|
||||
p.stamp("error", out, msg, "")
|
||||
out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: flag}
|
||||
p.stamp("error", out, "", fmt.Sprintln(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))
|
||||
p.stamp("error", out, msg, fmt.Sprint(Red.Regular(r.Err.Error())))
|
||||
} 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,13 @@ package realize
|
|||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -137,6 +138,7 @@ func TestProject_Validate(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestProject_Watch(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
r := Realize{}
|
||||
r.Projects = append(r.Projects, Project{
|
||||
parent: &r,
|
||||
|
@ -147,5 +149,8 @@ func TestProject_Watch(t *testing.T) {
|
|||
time.Sleep(100)
|
||||
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()
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
package realize
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"flag"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSchema_Add(t *testing.T) {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package realize
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServer_Start(t *testing.T) {
|
||||
s := Server{
|
||||
Host: "localhost",
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package realize
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
"math/rand"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Rand is used for generate a random string
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
// +build !windows
|
||||
|
||||
package realize
|
||||
|
||||
import (
|
||||
|
|
|
@ -24,7 +24,6 @@ type Tool struct {
|
|||
|
||||
// Tools go
|
||||
type Tools struct {
|
||||
Fix Tool `yaml:"fix,omitempty" json:"fix,omitempty"`
|
||||
Clean Tool `yaml:"clean,omitempty" json:"clean,omitempty"`
|
||||
Vet Tool `yaml:"vet,omitempty" json:"vet,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.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
|
||||
if t.Fmt.Status {
|
||||
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.isTool = true
|
||||
|
@ -99,10 +91,13 @@ func (t *Tools) Setup() {
|
|||
|
||||
// Exec a go tool
|
||||
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)
|
||||
}
|
||||
if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") {
|
||||
} else if !strings.HasSuffix(path, ".go") {
|
||||
return
|
||||
}
|
||||
args := []string{}
|
||||
if strings.HasSuffix(path, ".go") {
|
||||
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" {
|
||||
var out, stderr bytes.Buffer
|
||||
done := make(chan error)
|
||||
args = append(t.cmd, t.Args...)
|
||||
args = append(t.cmd, args...)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
if t.Dir != "" {
|
||||
cmd.Dir, _ = filepath.Abs(t.Dir)
|
||||
|
@ -140,7 +135,6 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue