2017-10-08 21:09:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2017-10-16 19:57:09 +00:00
|
|
|
// Tool options customizable, should be moved in Cmd
|
2017-10-08 21:09:45 +00:00
|
|
|
type tool struct {
|
2017-11-14 15:12:37 +00:00
|
|
|
name, err, out string
|
|
|
|
cmd, options []string
|
|
|
|
dir, status bool
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 09:56:19 +00:00
|
|
|
// Cmds list of go commands
|
2017-10-08 21:09:45 +00:00
|
|
|
type Cmds struct {
|
2017-11-05 18:28:41 +00:00
|
|
|
Fix Cmd `yaml:"fix,omitempty" json:"fix,omitempty"`
|
|
|
|
Clean Cmd `yaml:"clean,omitempty" json:"clean,omitempty"`
|
2017-10-08 21:09:45 +00:00
|
|
|
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"`
|
2017-10-08 21:09:45 +00:00
|
|
|
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
|
2017-10-08 21:09:45 +00:00
|
|
|
type Cmd struct {
|
2017-10-16 13:21:34 +00:00
|
|
|
Method string `yaml:"method,omitempty" json:"method,omitempty"`
|
2017-10-08 21:09:45 +00:00
|
|
|
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
|
2017-11-12 21:42:21 +00:00
|
|
|
Status bool `yaml:"status,omitempty" json:"status,omitempty"`
|
2017-10-16 19:57:09 +00:00
|
|
|
tool bool
|
2017-11-12 21:42:21 +00:00
|
|
|
method []string
|
2017-10-08 21:09:45 +00:00
|
|
|
name, startTxt, endTxt string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean duplicate projects
|
2017-10-31 12:28:58 +00:00
|
|
|
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 {
|
2017-11-14 18:17:34 +00:00
|
|
|
// path validation
|
|
|
|
|
2017-10-31 12:28:58 +00:00
|
|
|
r.Schema = append(arr[:key], arr[key+1:]...)
|
|
|
|
break
|
|
|
|
}
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
2017-10-31 12:28:58 +00:00
|
|
|
return nil
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
2017-10-31 12:28:58 +00:00
|
|
|
return errors.New("there are no projects")
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new project
|
2017-11-12 21:42:21 +00:00
|
|
|
func (r *realize) add(p *cli.Context) (err error) {
|
2017-11-14 18:17:34 +00:00
|
|
|
// project init
|
|
|
|
name := filepath.Base(p.String("path"))
|
|
|
|
if name == "." {
|
|
|
|
name = filepath.Base(wdir())
|
2017-11-05 20:23:32 +00:00
|
|
|
}
|
2017-10-08 21:09:45 +00:00
|
|
|
project := Project{
|
2017-11-14 18:17:34 +00:00
|
|
|
Name: name,
|
|
|
|
Path: p.String("path"),
|
2017-10-08 21:09:45 +00:00
|
|
|
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-31 12:28:58 +00:00
|
|
|
Ignore: []string{".git", ".realize", "vendor"},
|
2017-10-08 21:09:45 +00:00
|
|
|
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
|
2017-10-31 12:28:58 +00:00
|
|
|
if err := r.clean(); err != nil {
|
|
|
|
return err
|
2017-10-22 21:22:45 +00:00
|
|
|
}
|
|
|
|
// set gobin
|
2017-10-31 12:28:58 +00:00
|
|
|
if err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin")); err != nil {
|
2017-10-22 21:22:45 +00:00
|
|
|
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
|
2017-10-31 12:28:58 +00:00
|
|
|
if p.String("name") != "" && elm.Name != p.String("name") {
|
2017-10-22 21:22:45 +00:00
|
|
|
continue
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
2017-10-22 21:22:45 +00:00
|
|
|
match = true
|
2017-10-31 12:28:58 +00:00
|
|
|
r.Schema[k].config(r)
|
2017-10-22 21:22:45 +00:00
|
|
|
go r.Schema[k].watch()
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return errors.New("there is no project with the given name")
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
2017-10-22 21:22:45 +00:00
|
|
|
wg.Wait()
|
2017-10-31 12:28:58 +00:00
|
|
|
return nil
|
2017-10-08 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-10-08 21:09:45 +00:00
|
|
|
func (r *realize) insert(c *cli.Context) error {
|
2017-10-12 14:28:25 +00:00
|
|
|
if c.Bool("no-config") {
|
2017-10-08 21:09:45 +00:00
|
|
|
r.Schema = []Project{}
|
|
|
|
}
|
|
|
|
if len(r.Schema) <= 0 {
|
|
|
|
if err := r.add(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|