commands initialization moved
This commit is contained in:
parent
7c1066d503
commit
40d34df126
118
cmd.go
118
cmd.go
|
@ -5,8 +5,6 @@ import (
|
|||
"gopkg.in/urfave/cli.v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Tool options customizable, should be moved in Cmd
|
||||
|
@ -41,7 +39,8 @@ type Cmd struct {
|
|||
}
|
||||
|
||||
// Clean duplicate projects
|
||||
func (r *realize) clean() {
|
||||
func (r *realize) clean() error {
|
||||
if len(r.Schema) > 0 {
|
||||
arr := r.Schema
|
||||
for key, val := range arr {
|
||||
if _, err := duplicates(val, arr[key+1:]); err != nil {
|
||||
|
@ -49,6 +48,9 @@ func (r *realize) clean() {
|
|||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("there are no projects")
|
||||
}
|
||||
|
||||
// Add a new project
|
||||
|
@ -95,14 +97,11 @@ func (r *realize) add(p *cli.Context) error {
|
|||
func (r *realize) run(p *cli.Context) error {
|
||||
var match bool
|
||||
// check projects and remove duplicates
|
||||
if len(r.Schema) > 0 {
|
||||
r.clean()
|
||||
}else{
|
||||
return errors.New("there are no projects")
|
||||
if err := r.clean(); err != nil {
|
||||
return err
|
||||
}
|
||||
// set gobin
|
||||
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
|
||||
if err != nil {
|
||||
if err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")); err != nil {
|
||||
return err
|
||||
}
|
||||
// loop projects
|
||||
|
@ -113,113 +112,18 @@ func (r *realize) run(p *cli.Context) error {
|
|||
}
|
||||
for k, elm := range r.Schema {
|
||||
// command start using name flag
|
||||
if p.String("name") != "" && r.Schema[k].Name != p.String("name") {
|
||||
if p.String("name") != "" && elm.Name != p.String("name") {
|
||||
continue
|
||||
}
|
||||
// validate project path, if invalid get wdir or clean current
|
||||
if !filepath.IsAbs(elm.Path){
|
||||
r.Schema[k].Path = wdir()
|
||||
}else{
|
||||
r.Schema[k].Path = filepath.Clean(elm.Path)
|
||||
}
|
||||
// env variables
|
||||
for key, item := range r.Schema[k].Environment {
|
||||
if err := os.Setenv(key, item); err != nil {
|
||||
r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
|
||||
}
|
||||
}
|
||||
// get basepath name
|
||||
r.Schema[k].name = filepath.Base(r.Schema[k].Path)
|
||||
|
||||
fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds))
|
||||
// Loop struct Cmds fields
|
||||
for i := 0; i < fields.NumField(); i++ {
|
||||
field := fields.Type().Field(i).Name
|
||||
if fields.FieldByName(field).Type().Name() == "Cmd" {
|
||||
v := fields.FieldByName(field)
|
||||
// Loop struct Cmd
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
//f := v.Type().Field(i).Name
|
||||
//fmt.Println(f)
|
||||
//if f.IsValid() {
|
||||
// if f.CanSet() {
|
||||
// fmt.Println(f.)
|
||||
// //switch f.Kind() {
|
||||
// //case reflect.Bool:
|
||||
// //case reflect.String:
|
||||
// //case reflect.Slice:
|
||||
// //}
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if elm.Cmds.Fmt.Status {
|
||||
if len(elm.Cmds.Fmt.Args) == 0 {
|
||||
elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"}
|
||||
}
|
||||
r.Schema[k].tools = append(r.Schema[k].tools, tool{
|
||||
status: elm.Cmds.Fmt.Status,
|
||||
cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method),
|
||||
options: split([]string{}, elm.Cmds.Fmt.Args),
|
||||
name: "Fmt",
|
||||
})
|
||||
}
|
||||
if elm.Cmds.Generate.Status {
|
||||
r.Schema[k].tools = append(r.Schema[k].tools, tool{
|
||||
status: elm.Cmds.Generate.Status,
|
||||
cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method),
|
||||
options: split([]string{}, elm.Cmds.Generate.Args),
|
||||
name: "Generate",
|
||||
dir: true,
|
||||
})
|
||||
}
|
||||
if elm.Cmds.Test.Status {
|
||||
r.Schema[k].tools = append(r.Schema[k].tools, tool{
|
||||
status: elm.Cmds.Test.Status,
|
||||
cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method),
|
||||
options: split([]string{}, elm.Cmds.Test.Args),
|
||||
name: "Test",
|
||||
dir: true,
|
||||
})
|
||||
}
|
||||
if elm.Cmds.Vet.Status {
|
||||
r.Schema[k].tools = append(r.Schema[k].tools, tool{
|
||||
status: elm.Cmds.Vet.Status,
|
||||
cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method),
|
||||
options: split([]string{}, elm.Cmds.Vet.Args),
|
||||
name: "Vet",
|
||||
dir: true,
|
||||
})
|
||||
}
|
||||
// default settings
|
||||
r.Schema[k].Cmds.Install = Cmd{
|
||||
Status: elm.Cmds.Install.Status,
|
||||
Args: append([]string{}, elm.Cmds.Install.Args...),
|
||||
method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method),
|
||||
name: "Install",
|
||||
startTxt: "Installing...",
|
||||
endTxt: "Installed",
|
||||
}
|
||||
r.Schema[k].Cmds.Build = Cmd{
|
||||
Status: elm.Cmds.Build.Status,
|
||||
Args: append([]string{}, elm.Cmds.Build.Args...),
|
||||
method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method),
|
||||
name: "Build",
|
||||
startTxt: "Building...",
|
||||
endTxt: "Built",
|
||||
}
|
||||
r.Schema[k].parent = r
|
||||
|
||||
match = true
|
||||
r.Schema[k].config(r)
|
||||
go r.Schema[k].watch()
|
||||
}
|
||||
if !match {
|
||||
return errors.New("there is no project with the given name")
|
||||
}
|
||||
wg.Wait()
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove a project
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"gopkg.in/urfave/cli.v2"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type loggerT struct{}
|
||||
|
@ -34,8 +34,8 @@ func TestRealize_Clean(t *testing.T) {
|
|||
r.Schema = append(r.Schema, Project{Path: "test1"})
|
||||
r.Schema = append(r.Schema, Project{Path: "test1"})
|
||||
r.clean()
|
||||
if len(r.Schema) > 2 {
|
||||
t.Error("Expected only one project")
|
||||
if len(r.Schema) != 2 {
|
||||
t.Error("Expected two projects")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
66
watcher.go
66
watcher.go
|
@ -170,6 +170,72 @@ func (p *Project) err(err error) {
|
|||
p.stamp("error", out, msg, "")
|
||||
}
|
||||
|
||||
// Config project init
|
||||
func (p *Project) config(r *realize) {
|
||||
// validate project path, if invalid get wdir or clean current
|
||||
if !filepath.IsAbs(p.Path) {
|
||||
p.Path = wdir()
|
||||
} else {
|
||||
p.Path = filepath.Clean(p.Path)
|
||||
}
|
||||
// get basepath name
|
||||
p.name = filepath.Base(p.Path)
|
||||
// env variables
|
||||
for key, item := range p.Environment {
|
||||
if err := os.Setenv(key, item); err != nil {
|
||||
p.Buffer.StdErr = append(p.Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
|
||||
}
|
||||
}
|
||||
// init commands
|
||||
if len(p.Cmds.Fmt.Args) == 0 {
|
||||
p.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"}
|
||||
}
|
||||
p.tools = append(p.tools, tool{
|
||||
status: p.Cmds.Fmt.Status,
|
||||
cmd: replace([]string{"gofmt"}, p.Cmds.Fmt.Method),
|
||||
options: split([]string{}, p.Cmds.Fmt.Args),
|
||||
name: "Fmt",
|
||||
})
|
||||
p.tools = append(p.tools, tool{
|
||||
status: p.Cmds.Generate.Status,
|
||||
cmd: replace([]string{"go", "generate"}, p.Cmds.Generate.Method),
|
||||
options: split([]string{}, p.Cmds.Generate.Args),
|
||||
name: "Generate",
|
||||
dir: true,
|
||||
})
|
||||
p.tools = append(p.tools, tool{
|
||||
status: p.Cmds.Test.Status,
|
||||
cmd: replace([]string{"go", "test"}, p.Cmds.Test.Method),
|
||||
options: split([]string{}, p.Cmds.Test.Args),
|
||||
name: "Test",
|
||||
dir: true,
|
||||
})
|
||||
p.tools = append(p.tools, tool{
|
||||
status: p.Cmds.Vet.Status,
|
||||
cmd: replace([]string{"go", "vet"}, p.Cmds.Vet.Method),
|
||||
options: split([]string{}, p.Cmds.Vet.Args),
|
||||
name: "Vet",
|
||||
dir: true,
|
||||
})
|
||||
p.Cmds.Install = Cmd{
|
||||
Status: p.Cmds.Install.Status,
|
||||
Args: append([]string{}, p.Cmds.Install.Args...),
|
||||
method: replace([]string{"go", "install"}, p.Cmds.Install.Method),
|
||||
name: "Install",
|
||||
startTxt: "Installing...",
|
||||
endTxt: "Installed",
|
||||
}
|
||||
p.Cmds.Build = Cmd{
|
||||
Status: p.Cmds.Build.Status,
|
||||
Args: append([]string{}, p.Cmds.Build.Args...),
|
||||
method: replace([]string{"go", "build"}, p.Cmds.Build.Method),
|
||||
name: "Build",
|
||||
startTxt: "Building...",
|
||||
endTxt: "Built",
|
||||
}
|
||||
p.parent = r
|
||||
}
|
||||
|
||||
// Cmd calls the method that execute commands after/before and display the results
|
||||
func (p *Project) cmd(stop <-chan bool, flag string, global bool) {
|
||||
done := make(chan bool)
|
||||
|
|
Loading…
Reference in New Issue