realize/watcher/cmd.go

148 lines
4.6 KiB
Go
Raw Normal View History

2016-09-01 22:17:19 +00:00
package cli
2016-08-31 12:08:15 +00:00
import (
"errors"
"fmt"
"gopkg.in/urfave/cli.v2"
"path/filepath"
"strings"
)
// Watch method adds the given paths on the Watcher
func (h *Blueprint) Run() error {
2016-11-01 09:56:12 +00:00
err := h.check()
2016-08-31 12:08:15 +00:00
if err == nil {
// loop projects
wg.Add(len(h.Projects))
2016-10-14 08:47:43 +00:00
for k := range h.Projects {
h.Projects[k].parent = h
go h.Projects[k].watching()
2016-08-31 12:08:15 +00:00
}
wg.Wait()
return nil
}
return err
}
// Fast method run a project from his working directory without makes a config file
2016-11-01 09:56:12 +00:00
func (h *Blueprint) Fast(p *cli.Context) error {
2016-08-31 12:08:15 +00:00
// Takes the values from config if wd path match with someone else
2016-09-17 23:04:36 +00:00
wg.Add(1)
for i := 0; i < len(h.Projects); i++ {
v := &h.Projects[i]
2016-09-18 11:50:15 +00:00
v.parent = h
2016-11-01 09:56:12 +00:00
v.path = v.Path
2016-09-17 23:04:36 +00:00
go v.watching()
2016-08-31 12:08:15 +00:00
}
wg.Wait()
return nil
}
// Add a new project
2016-11-01 09:56:12 +00:00
func (h *Blueprint) Add(p *cli.Context) error {
project := Project{
Name: h.name(p),
Path: filepath.Clean(p.String("path")),
Build: p.Bool("build"),
2016-11-01 10:13:56 +00:00
Bin: !p.Bool("no-bin"),
Run: !p.Bool("no-run"),
Fmt: !p.Bool("no-fmt"),
2016-11-01 09:56:12 +00:00
Test: p.Bool("test"),
Params: argsParam(p),
2016-08-31 12:08:15 +00:00
Watcher: Watcher{
Paths: []string{"/"},
Ignore: []string{"vendor"},
Exts: []string{".go"},
Output: map[string]bool{
"cli": true,
"file": false,
},
},
}
2016-11-01 09:56:12 +00:00
if _, err := duplicates(project, h.Projects); err != nil {
2016-08-31 12:08:15 +00:00
return err
}
2016-11-01 09:56:12 +00:00
h.Projects = append(h.Projects, project)
2016-08-31 12:08:15 +00:00
return nil
}
// Clean duplicate projects
func (h *Blueprint) Clean() {
arr := h.Projects
for key, val := range arr {
if _, err := duplicates(val, arr[key+1:]); err != nil {
h.Projects = append(arr[:key], arr[key+1:]...)
break
}
}
}
// Inserts a new project in the list
2016-11-01 09:56:12 +00:00
func (h *Blueprint) Insert(p *cli.Context) error {
err := h.Add(p)
2016-08-31 12:08:15 +00:00
return err
}
// Remove a project
2016-11-01 09:56:12 +00:00
func (h *Blueprint) Remove(p *cli.Context) error {
for key, val := range h.Projects {
if p.String("name") == val.Name {
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
return nil
2016-08-31 12:08:15 +00:00
}
}
2016-11-01 09:56:12 +00:00
return errors.New("No project found.")
2016-08-31 12:08:15 +00:00
}
// List of all the projects
func (h *Blueprint) List() error {
2016-11-01 09:56:12 +00:00
err := h.check()
2016-08-31 12:08:15 +00:00
if err == nil {
for _, val := range h.Projects {
2016-11-01 09:56:12 +00:00
fmt.Println(h.Blue.Bold("|"), h.Blue.Bold(strings.ToUpper(val.Name)))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Base Path"), ":", h.Magenta.Regular(val.Path))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Run"), ":", h.Magenta.Regular(val.Run))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Build"), ":", h.Magenta.Regular(val.Build))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Install"), ":", h.Magenta.Regular(val.Bin))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Fmt"), ":", h.Magenta.Regular(val.Fmt))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Test"), ":", h.Magenta.Regular(val.Test))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Params"), ":", h.Magenta.Regular(val.Params))
fmt.Println(h.Magenta.Regular("|"), "\t", h.Yellow.Regular("Watcher"), ":")
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("After"), ":", h.Magenta.Regular(val.Watcher.After))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Before"), ":", h.Magenta.Regular(val.Watcher.Before))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Extensions"), ":", h.Magenta.Regular(val.Watcher.Exts))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Paths"), ":", h.Magenta.Regular(val.Watcher.Paths))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Paths ignored"), ":", h.Magenta.Regular(val.Watcher.Ignore))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Watch preview"), ":", h.Magenta.Regular(val.Watcher.Preview))
fmt.Println(h.Magenta.Regular("|"), "\t\t", h.Yellow.Regular("Output"), ":")
fmt.Println(h.Magenta.Regular("|"), "\t\t\t", h.Yellow.Regular("Cli"), ":", h.Magenta.Regular(val.Watcher.Output["cli"]))
fmt.Println(h.Magenta.Regular("|"), "\t\t\t", h.Yellow.Regular("File"), ":", h.Magenta.Regular(val.Watcher.Output["file"]))
2016-08-31 12:08:15 +00:00
}
2016-11-01 09:56:12 +00:00
return nil
2016-08-31 12:08:15 +00:00
}
return err
}
2016-10-21 15:30:12 +00:00
2016-11-01 09:56:12 +00:00
// Check if there are projects
func (h *Blueprint) check() error {
if len(h.Projects) > 0 {
h.Clean()
return nil
} else {
return errors.New("There are no projects. The config file is empty.")
}
}
2016-10-21 15:30:12 +00:00
// NameParam check the project name presence. If empty takes the working directory name
2016-11-01 09:56:12 +00:00
func (h *Blueprint) name(p *cli.Context) string {
2016-10-21 15:30:12 +00:00
var name string
2016-11-01 09:56:12 +00:00
if p.String("name") == "" && p.String("path") == "" {
return h.Wdir()
} else if p.String("path") != "/" {
name = filepath.Base(p.String("path"))
2016-10-21 15:30:12 +00:00
} else {
2016-11-01 09:56:12 +00:00
name = p.String("name")
2016-10-21 15:30:12 +00:00
}
return name
}