realize/cmd.go

158 lines
3.6 KiB
Go
Raw Normal View History

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
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 {
Fix Cmd `yaml:"fix,omitempty" json:"fix,omitempty"`
Clean Cmd `yaml:"clean,omitempty" json:"clean,omitempty"`
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
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 {
r.Schema = append(arr[:key], arr[key+1:]...)
break
}
}
2017-10-31 12:28:58 +00:00
return nil
}
2017-10-31 12:28:58 +00:00
return errors.New("there are no projects")
}
// Add a new project
func (r *realize) add(p *cli.Context) error {
2017-11-05 20:23:32 +00:00
path, err := filepath.Abs(p.String("path"))
2017-11-05 21:38:26 +00:00
if err != nil {
2017-11-05 20:23:32 +00:00
return err
}
project := Project{
2017-10-22 16:44:44 +00:00
Name: filepath.Base(filepath.Clean(p.String("path"))),
2017-11-05 20:23:32 +00:00
Path: 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-31 12:28:58 +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
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-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-22 21:22:45 +00:00
wg.Wait()
2017-10-31 12:28:58 +00:00
return nil
}
// 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
}