realize/cmd.go

250 lines
6.4 KiB
Go
Raw Normal View History

package main
import (
"errors"
"gopkg.in/urfave/cli.v2"
"os"
"path/filepath"
"time"
2017-10-22 21:22:45 +00:00
"reflect"
)
2017-10-16 19:57:09 +00:00
// Tool options customizable, should be moved in Cmd
type tool struct {
dir bool
status bool
name string
err string
2017-10-16 13:21:34 +00:00
cmd []string
options []string
}
2017-10-17 09:56:19 +00:00
// Cmds list of go commands
type Cmds struct {
Vet Cmd `yaml:"vet,omitempty" json:"vet,omitempty"`
Fmt Cmd `yaml:"fmt,omitempty" json:"fmt,omitempty"`
Test Cmd `yaml:"test,omitempty" json:"test,omitempty"`
Generate Cmd `yaml:"generate,omitempty" json:"generate,omitempty"`
2017-10-16 13:21:34 +00:00
Install Cmd `yaml:"install,omitempty" json:"install,omitempty"`
Build Cmd `yaml:"build,omitempty" json:"build,omitempty"`
Run bool `yaml:"run,omitempty" json:"run,omitempty"`
}
2017-10-17 09:56:19 +00:00
// Cmd single command fields and options
type Cmd struct {
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
2017-10-16 13:21:34 +00:00
Method string `yaml:"method,omitempty" json:"method,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
2017-10-15 20:21:29 +00:00
method []string
2017-10-16 19:57:09 +00:00
tool bool
name, startTxt, endTxt string
}
// Clean duplicate projects
func (r *realize) clean() {
arr := r.Schema
for key, val := range arr {
if _, err := duplicates(val, arr[key+1:]); err != nil {
r.Schema = append(arr[:key], arr[key+1:]...)
break
}
}
}
// Add a new project
func (r *realize) add(p *cli.Context) error {
project := Project{
2017-10-22 16:44:44 +00:00
Name: filepath.Base(filepath.Clean(p.String("path"))),
Path: filepath.Clean(p.String("path")),
Cmds: Cmds{
Vet: Cmd{
Status: p.Bool("vet"),
},
Fmt: Cmd{
Status: p.Bool("fmt"),
},
Test: Cmd{
Status: p.Bool("test"),
},
Generate: Cmd{
Status: p.Bool("generate"),
},
Build: Cmd{
Status: p.Bool("build"),
},
Install: Cmd{
Status: p.Bool("install"),
},
Run: p.Bool("run"),
},
Args: params(p),
Watcher: Watch{
Paths: []string{"/"},
2017-10-22 21:22:45 +00:00
Ignore: []string{".git",".realize","vendor"},
Exts: []string{"go"},
},
}
if _, err := duplicates(project, r.Schema); err != nil {
return err
}
r.Schema = append(r.Schema, project)
return nil
}
// Run launches the toolchain for each project
func (r *realize) run(p *cli.Context) error {
2017-10-22 16:44:44 +00:00
var match bool
2017-10-22 21:22:45 +00:00
// check projects and remove duplicates
if len(r.Schema) > 0 {
r.clean()
}else{
return errors.New("there are no projects")
}
// set gobin
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
if err != nil {
return err
}
// loop projects
if p.String("name") != "" {
wg.Add(1)
} else {
wg.Add(len(r.Schema))
}
for k, elm := range r.Schema {
// command start using name flag
if p.String("name") != "" && r.Schema[k].Name != p.String("name") {
continue
}
2017-10-22 21:22:45 +00:00
// 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: ""})
}
2017-10-22 21:22:45 +00:00
}
// get basepath name
r.Schema[k].name = filepath.Base(r.Schema[k].path)
2017-10-22 21:22:45 +00:00
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:
// //}
// }
//}
}
}
2017-10-22 21:22:45 +00:00
}
2017-10-22 21:22:45 +00:00
if elm.Cmds.Fmt.Status {
if len(elm.Cmds.Fmt.Args) == 0 {
elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"}
}
2017-10-22 21:22:45 +00:00
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",
})
}
2017-10-22 21:22:45 +00:00
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,
})
2017-10-22 16:44:44 +00:00
}
2017-10-22 21:22:45 +00:00
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
r.Schema[k].path = r.Schema[k].Path
match = true
go r.Schema[k].watch()
}
if !match {
return errors.New("there is no project with the given name")
}
2017-10-22 21:22:45 +00:00
wg.Wait()
return err
}
// Remove a project
func (r *realize) remove(p *cli.Context) error {
for key, val := range r.Schema {
if p.String("name") == val.Name {
r.Schema = append(r.Schema[:key], r.Schema[key+1:]...)
return nil
}
}
return errors.New("no project found")
}
2017-10-15 20:21:29 +00:00
// Insert current project if there isn't already one
func (r *realize) insert(c *cli.Context) error {
2017-10-12 14:28:25 +00:00
if c.Bool("no-config") {
r.Schema = []Project{}
}
if len(r.Schema) <= 0 {
if err := r.add(c); err != nil {
return err
}
}
return nil
}