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
|
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)"))
|
||||||
|
|
|
@ -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 (
|
||||||
|
@ -71,11 +72,11 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop realize workflow
|
// Stop realize workflow
|
||||||
func (r *Realize) Stop() error{
|
func (r *Realize) Stop() error {
|
||||||
if r.exit != nil{
|
if r.exit != nil {
|
||||||
close(r.exit)
|
close(r.exit)
|
||||||
return nil
|
return nil
|
||||||
}else{
|
} else {
|
||||||
return errors.New("exit chan undefined")
|
return errors.New("exit chan undefined")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
} else {
|
||||||
case <-r.exit:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}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
|
||||||
|
|
|
@ -22,10 +22,10 @@ func TestRealize_Stop(t *testing.T) {
|
||||||
func TestRealize_Start(t *testing.T) {
|
func TestRealize_Start(t *testing.T) {
|
||||||
r := Realize{}
|
r := Realize{}
|
||||||
err := r.Start()
|
err := r.Start()
|
||||||
if err == nil{
|
if err == nil {
|
||||||
t.Error("Error expected")
|
t.Error("Error expected")
|
||||||
}
|
}
|
||||||
r.Projects = append(r.Projects,Project{Name:"test"})
|
r.Projects = append(r.Projects, Project{Name: "test"})
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(100)
|
time.Sleep(100)
|
||||||
close(r.exit)
|
close(r.exit)
|
||||||
|
@ -35,7 +35,7 @@ func TestRealize_Start(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err = r.Start()
|
err = r.Start()
|
||||||
if err != nil{
|
if err != nil {
|
||||||
t.Error("Unexpected error", err)
|
t.Error("Unexpected error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
@ -304,7 +305,7 @@ L:
|
||||||
close(p.stop)
|
close(p.stop)
|
||||||
p.stop = make(chan bool)
|
p.stop = make(chan bool)
|
||||||
p.Change(event)
|
p.Change(event)
|
||||||
go p.Reload( "", p.stop)
|
go p.Reload("", p.stop)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if p.Validate(event.Name, true) {
|
if p.Validate(event.Name, true) {
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,17 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProject_After(t *testing.T) /**/{
|
func TestProject_After(t *testing.T) /**/ {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
log.SetOutput(&buf)
|
log.SetOutput(&buf)
|
||||||
r := Realize{}
|
r := Realize{}
|
||||||
|
@ -85,7 +86,7 @@ func TestProject_Change(t *testing.T) {
|
||||||
r.Change = func(context Context) {
|
r.Change = func(context Context) {
|
||||||
log.Println(context.Event.Name)
|
log.Println(context.Event.Name)
|
||||||
}
|
}
|
||||||
event := fsnotify.Event{Name:"test",Op:fsnotify.Write}
|
event := fsnotify.Event{Name: "test", Op: fsnotify.Write}
|
||||||
r.Projects[0].Change(event)
|
r.Projects[0].Change(event)
|
||||||
if !strings.Contains(buf.String(), event.Name) {
|
if !strings.Contains(buf.String(), event.Name) {
|
||||||
t.Error("Unexpected error")
|
t.Error("Unexpected error")
|
||||||
|
@ -100,12 +101,12 @@ func TestProject_Reload(t *testing.T) {
|
||||||
parent: &r,
|
parent: &r,
|
||||||
})
|
})
|
||||||
input := "test/path"
|
input := "test/path"
|
||||||
r.Projects[0].watcher, _ = NewFileWatcher(false,0)
|
r.Projects[0].watcher, _ = NewFileWatcher(false, 0)
|
||||||
r.Reload = func(context Context) {
|
r.Reload = func(context Context) {
|
||||||
log.Println(context.Path)
|
log.Println(context.Path)
|
||||||
}
|
}
|
||||||
stop := make(chan bool)
|
stop := make(chan bool)
|
||||||
r.Projects[0].Reload(input,stop)
|
r.Projects[0].Reload(input, stop)
|
||||||
if !strings.Contains(buf.String(), input) {
|
if !strings.Contains(buf.String(), input) {
|
||||||
t.Error("Unexpected error")
|
t.Error("Unexpected error")
|
||||||
}
|
}
|
||||||
|
@ -113,14 +114,14 @@ func TestProject_Reload(t *testing.T) {
|
||||||
|
|
||||||
func TestProject_Validate(t *testing.T) {
|
func TestProject_Validate(t *testing.T) {
|
||||||
data := map[string]bool{
|
data := map[string]bool{
|
||||||
"": false,
|
"": false,
|
||||||
"/test/.path/": false,
|
"/test/.path/": false,
|
||||||
"./test/path/": false,
|
"./test/path/": false,
|
||||||
"/test/path/test.html": false,
|
"/test/path/test.html": false,
|
||||||
"/test/path/test.go": false,
|
"/test/path/test.go": false,
|
||||||
"/test/ignore/test.go": false,
|
"/test/ignore/test.go": false,
|
||||||
"/test/check/notexist.go": false,
|
"/test/check/notexist.go": false,
|
||||||
"/test/check/exist.go": false,
|
"/test/check/exist.go": false,
|
||||||
}
|
}
|
||||||
r := Realize{}
|
r := Realize{}
|
||||||
r.Projects = append(r.Projects, Project{
|
r.Projects = append(r.Projects, Project{
|
||||||
|
@ -130,22 +131,26 @@ func TestProject_Validate(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
for i, v := range data {
|
for i, v := range data {
|
||||||
if r.Projects[0].Validate(i,true) != v{
|
if r.Projects[0].Validate(i, true) != v {
|
||||||
t.Error("Unexpected error",i,"expected",v)
|
t.Error("Unexpected error", i, "expected", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
})
|
})
|
||||||
r.exit = make(chan os.Signal, 2)
|
r.exit = make(chan os.Signal, 2)
|
||||||
signal.Notify(r.exit, os.Interrupt)
|
signal.Notify(r.exit, os.Interrupt)
|
||||||
go func(){
|
go func() {
|
||||||
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()
|
||||||
}
|
}
|
|
@ -76,14 +76,14 @@ func (s *Schema) New(c *cli.Context) Project {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter project list by field
|
// Filter project list by field
|
||||||
func (s *Schema) Filter(field string, value interface{}) []Project{
|
func (s *Schema) Filter(field string, value interface{}) []Project {
|
||||||
result := []Project{}
|
result := []Project{}
|
||||||
for _, item := range s.Projects{
|
for _, item := range s.Projects {
|
||||||
v := reflect.ValueOf(item)
|
v := reflect.ValueOf(item)
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
if v.Type().Field(i).Name == field {
|
if v.Type().Field(i).Name == field {
|
||||||
if reflect.DeepEqual(v.Field(i).Interface(), value) {
|
if reflect.DeepEqual(v.Field(i).Interface(), value) {
|
||||||
result = append(result,item)
|
result = append(result, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
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) {
|
||||||
r := Realize{}
|
r := Realize{}
|
||||||
p := Project{Name:"test"}
|
p := Project{Name: "test"}
|
||||||
r.Add(p)
|
r.Add(p)
|
||||||
if len(r.Schema.Projects) != 1{
|
if len(r.Schema.Projects) != 1 {
|
||||||
t.Error("Unexpected error there are",len(r.Schema.Projects),"instead one")
|
t.Error("Unexpected error there are", len(r.Schema.Projects), "instead one")
|
||||||
}
|
}
|
||||||
r.Add(p)
|
r.Add(p)
|
||||||
if len(r.Schema.Projects) != 1{
|
if len(r.Schema.Projects) != 1 {
|
||||||
t.Error("Unexpected error there are",len(r.Schema.Projects),"instead one")
|
t.Error("Unexpected error there are", len(r.Schema.Projects), "instead one")
|
||||||
}
|
}
|
||||||
r.Add(Project{Name:"testing"})
|
r.Add(Project{Name: "testing"})
|
||||||
if len(r.Schema.Projects) != 2{
|
if len(r.Schema.Projects) != 2 {
|
||||||
t.Error("Unexpected error there are",len(r.Schema.Projects),"instead two")
|
t.Error("Unexpected error there are", len(r.Schema.Projects), "instead two")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,15 +29,15 @@ func TestSchema_Remove(t *testing.T) {
|
||||||
r.Schema.Projects = []Project{
|
r.Schema.Projects = []Project{
|
||||||
{
|
{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
},{
|
}, {
|
||||||
Name: "testing",
|
Name: "testing",
|
||||||
},{
|
}, {
|
||||||
Name: "testing",
|
Name: "testing",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
r.Remove("testing")
|
r.Remove("testing")
|
||||||
if len(r.Schema.Projects) != 2{
|
if len(r.Schema.Projects) != 2 {
|
||||||
t.Error("Unexpected errore there are",len(r.Schema.Projects),"instead one")
|
t.Error("Unexpected errore there are", len(r.Schema.Projects), "instead one")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,28 +55,28 @@ func TestSchema_New(t *testing.T) {
|
||||||
c := cli.NewContext(nil, set, nil)
|
c := cli.NewContext(nil, set, nil)
|
||||||
set.Parse([]string{"--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"})
|
set.Parse([]string{"--fmt", "--install", "--run", "--build", "--generate", "--test", "--vet"})
|
||||||
p := r.New(c)
|
p := r.New(c)
|
||||||
if p.Name != filepath.Base(Wdir()){
|
if p.Name != filepath.Base(Wdir()) {
|
||||||
t.Error("Unexpected error",p.Name,"instead",filepath.Base(Wdir()))
|
t.Error("Unexpected error", p.Name, "instead", filepath.Base(Wdir()))
|
||||||
}
|
}
|
||||||
if !p.Tools.Install.Status{
|
if !p.Tools.Install.Status {
|
||||||
t.Error("Install should be enabled")
|
t.Error("Install should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Fmt.Status{
|
if !p.Tools.Fmt.Status {
|
||||||
t.Error("Fmt should be enabled")
|
t.Error("Fmt should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Run.Status{
|
if !p.Tools.Run.Status {
|
||||||
t.Error("Run should be enabled")
|
t.Error("Run should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Build.Status{
|
if !p.Tools.Build.Status {
|
||||||
t.Error("Build should be enabled")
|
t.Error("Build should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Generate.Status{
|
if !p.Tools.Generate.Status {
|
||||||
t.Error("Generate should be enabled")
|
t.Error("Generate should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Test.Status{
|
if !p.Tools.Test.Status {
|
||||||
t.Error("Test should be enabled")
|
t.Error("Test should be enabled")
|
||||||
}
|
}
|
||||||
if !p.Tools.Vet.Status{
|
if !p.Tools.Vet.Status {
|
||||||
t.Error("Vet should be enabled")
|
t.Error("Vet should be enabled")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,19 +86,19 @@ func TestSchema_Filter(t *testing.T) {
|
||||||
r.Schema.Projects = []Project{
|
r.Schema.Projects = []Project{
|
||||||
{
|
{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
},{
|
}, {
|
||||||
Name: "test",
|
Name: "test",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "example",
|
Name: "example",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
result := r.Filter("Name","test")
|
result := r.Filter("Name", "test")
|
||||||
if len(result) != 2{
|
if len(result) != 2 {
|
||||||
t.Error("Expected two project")
|
t.Error("Expected two project")
|
||||||
}
|
}
|
||||||
result = r.Filter("Name","example")
|
result = r.Filter("Name", "example")
|
||||||
if len(result) != 1{
|
if len(result) != 1 {
|
||||||
t.Error("Expected one project")
|
t.Error("Expected one project")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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",
|
||||||
|
@ -39,7 +40,7 @@ func TestServer_Open(t *testing.T) {
|
||||||
"linux": "xdg-open",
|
"linux": "xdg-open",
|
||||||
}
|
}
|
||||||
key := runtime.GOOS
|
key := runtime.GOOS
|
||||||
if _, ok:= cmd[key]; !ok{
|
if _, ok := cmd[key]; !ok {
|
||||||
t.Error("System not supported")
|
t.Error("System not supported")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
// +build !windows
|
// +build !windows
|
||||||
|
|
||||||
package realize
|
package realize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -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,44 +91,46 @@ 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 {
|
||||||
path = filepath.Dir(path)
|
if filepath.Ext(path) != "" {
|
||||||
}
|
|
||||||
if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") {
|
|
||||||
args := []string{}
|
|
||||||
if strings.HasSuffix(path, ".go") {
|
|
||||||
args = append(t.Args, path)
|
|
||||||
path = filepath.Dir(path)
|
path = filepath.Dir(path)
|
||||||
}
|
}
|
||||||
if s := ext(path); s == "" || s == "go" {
|
} else if !strings.HasSuffix(path, ".go") {
|
||||||
var out, stderr bytes.Buffer
|
return
|
||||||
done := make(chan error)
|
}
|
||||||
args = append(t.cmd, t.Args...)
|
args := []string{}
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
if strings.HasSuffix(path, ".go") {
|
||||||
if t.Dir != "" {
|
args = append(t.Args, path)
|
||||||
cmd.Dir, _ = filepath.Abs(t.Dir)
|
path = filepath.Dir(path)
|
||||||
|
}
|
||||||
|
if s := ext(path); s == "" || s == "go" {
|
||||||
|
var out, stderr bytes.Buffer
|
||||||
|
done := make(chan error)
|
||||||
|
args = append(t.cmd, args...)
|
||||||
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
|
if t.Dir != "" {
|
||||||
|
cmd.Dir, _ = filepath.Abs(t.Dir)
|
||||||
|
} else {
|
||||||
|
cmd.Dir = path
|
||||||
|
}
|
||||||
|
cmd.Stdout = &out
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
// Start command
|
||||||
|
cmd.Start()
|
||||||
|
go func() { done <- cmd.Wait() }()
|
||||||
|
// Wait a result
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
// Stop running command
|
||||||
|
cmd.Process.Kill()
|
||||||
|
case err := <-done:
|
||||||
|
// Command completed
|
||||||
|
response.Name = t.name
|
||||||
|
if err != nil {
|
||||||
|
response.Err = errors.New(stderr.String() + out.String())
|
||||||
} else {
|
} else {
|
||||||
cmd.Dir = path
|
if t.Output {
|
||||||
}
|
response.Out = out.String()
|
||||||
cmd.Stdout = &out
|
|
||||||
cmd.Stderr = &stderr
|
|
||||||
// Start command
|
|
||||||
cmd.Start()
|
|
||||||
go func() { done <- cmd.Wait() }()
|
|
||||||
// Wait a result
|
|
||||||
select {
|
|
||||||
case <-stop:
|
|
||||||
// Stop running command
|
|
||||||
cmd.Process.Kill()
|
|
||||||
case err := <-done:
|
|
||||||
// Command completed
|
|
||||||
response.Name = t.name
|
|
||||||
if err != nil {
|
|
||||||
response.Err = errors.New(stderr.String() + out.String())
|
|
||||||
} else {
|
|
||||||
if t.Output {
|
|
||||||
response.Out = out.String()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,20 +6,20 @@ func TestTools_Setup(t *testing.T) {
|
||||||
tools := Tools{
|
tools := Tools{
|
||||||
Clean: Tool{
|
Clean: Tool{
|
||||||
Status: true,
|
Status: true,
|
||||||
name: "test",
|
name: "test",
|
||||||
isTool: false,
|
isTool: false,
|
||||||
Method: "test",
|
Method: "test",
|
||||||
Args: []string{"arg"},
|
Args: []string{"arg"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
tools.Setup()
|
tools.Setup()
|
||||||
if tools.Clean.name == "test"{
|
if tools.Clean.name == "test" {
|
||||||
t.Error("Unexpected value")
|
t.Error("Unexpected value")
|
||||||
}
|
}
|
||||||
if tools.Clean.Method != "test"{
|
if tools.Clean.Method != "test" {
|
||||||
t.Error("Unexpected value")
|
t.Error("Unexpected value")
|
||||||
}
|
}
|
||||||
if !tools.Clean.isTool{
|
if !tools.Clean.isTool {
|
||||||
t.Error("Unexpected value")
|
t.Error("Unexpected value")
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -56,7 +56,7 @@ func ext(path string) string {
|
||||||
for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
|
for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
|
||||||
if path[i] == '.' {
|
if path[i] == '.' {
|
||||||
ext = path[i:]
|
ext = path[i:]
|
||||||
if index := strings.LastIndex(ext,"."); index > 0{
|
if index := strings.LastIndex(ext, "."); index > 0 {
|
||||||
ext = ext[index:]
|
ext = ext[index:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,19 +64,19 @@ func TestWdir(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExt(t *testing.T){
|
func TestExt(t *testing.T) {
|
||||||
paths := map[string]string{
|
paths := map[string]string{
|
||||||
"/test/a/b/c": "",
|
"/test/a/b/c": "",
|
||||||
"/test/a/ac.go": "go",
|
"/test/a/ac.go": "go",
|
||||||
"/test/a/ac.test.go": "go",
|
"/test/a/ac.test.go": "go",
|
||||||
"/test/a/ac_test.go": "go",
|
"/test/a/ac_test.go": "go",
|
||||||
"/test/./ac_test.go": "go",
|
"/test/./ac_test.go": "go",
|
||||||
"/test/a/.test": "test",
|
"/test/a/.test": "test",
|
||||||
"/test/a/.": "",
|
"/test/a/.": "",
|
||||||
}
|
}
|
||||||
for i, v := range paths {
|
for i, v := range paths {
|
||||||
if ext(i) != v{
|
if ext(i) != v {
|
||||||
t.Error("Wrong extension",ext(i),v)
|
t.Error("Wrong extension", ext(i), v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue