flags updated

This commit is contained in:
alessio 2017-09-17 23:37:39 +02:00
parent 164987d2aa
commit 6dda3c76d1
4 changed files with 178 additions and 197 deletions

View File

@ -57,13 +57,13 @@ func main() {
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path."},
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Run a project by its name."},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test."},
&cli.BoolFlag{Name: "fmt", Aliases: []string{"f"}, Value: false, Usage: "Enable go fmt."},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate."},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Watch by polling instead of Watch by fsnotify."},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Watch by polling instead of watch by fsnotify."},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
&cli.BoolFlag{Name: "open", Aliases: []string{"o"}, Value: false, Usage: "Open server directly in the default browser."},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Value: false, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
&cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install."},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."},
&cli.BoolFlag{Name: "run", Aliases: []string{"nr"}, Value: false, Usage: "Enable go run"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
},
Action: func(p *cli.Context) error {
@ -96,13 +96,11 @@ func main() {
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path."},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test."},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate."},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build."},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Watch by polling instead of Watch by fsnotify."},
&cli.BoolFlag{Name: "server", Aliases: []string{"s"}, Value: false, Usage: "Enable server and open into the default browser."},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Value: false, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-fmt", Aliases: []string{"nf"}, Value: false, Usage: "Disable go fmt."},
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
&cli.BoolFlag{Name: "install", Aliases: []string{"i"}, Value: false, Usage: "Enable go install"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: false, Usage: "Enable go run"},
},
Action: func(p *cli.Context) error {
if err := r.Blueprint.Add(p); err != nil {
@ -1143,13 +1141,6 @@ func before(*cli.Context) error {
return nil
}
// Check for the noconf option
func noconf(c *cli.Context, s *settings.Settings) {
if c.Bool("no-config") {
s.Make = false
}
}
// Check for polling option
func polling(c *cli.Context, s *settings.Legacy) {
if c.Bool("legacy") {
@ -1157,6 +1148,13 @@ func polling(c *cli.Context, s *settings.Legacy) {
}
}
// Check for the noconf option
func noconf(c *cli.Context, s *settings.Settings) {
if c.Bool("no-config") {
s.Make = false
}
}
// Insert a project if there isn't already one
func insert(c *cli.Context, b *watcher.Blueprint) error {
if len(b.Projects) <= 0 {

View File

@ -11,128 +11,6 @@ import (
"time"
)
// Run launches the toolchain for each project
func (h *Blueprint) Run(p *cli.Context) error {
err := h.check()
if err == nil {
// loop projects
if p.String("name") != "" {
wg.Add(1)
} else {
wg.Add(len(h.Projects))
}
for k, element := range h.Projects {
if p.String("name") != "" && h.Projects[k].Name != p.String("name") {
continue
}
if element.Cmds.Fmt.Status {
h.Projects[k].tools.Fmt = tool{
status: &h.Projects[k].Cmds.Fmt.Status,
cmd: "gofmt",
options: arguments([]string{}, element.Cmds.Fmt.Args),
name: "Go Fmt",
}
}
if element.Cmds.Generate.Status {
h.Projects[k].tools.Generate = tool{
status: &h.Projects[k].Cmds.Generate.Status,
cmd: "go",
options: arguments([]string{"generate"}, element.Cmds.Generate.Args),
name: "Go Generate",
}
}
if element.Cmds.Test.Status {
h.Projects[k].tools.Test = tool{
status: &h.Projects[k].Cmds.Test.Status,
cmd: "go",
options: arguments([]string{"test"}, element.Cmds.Test.Args),
name: "Go Test",
}
}
if element.Cmds.Vet {
h.Projects[k].tools.Vet = tool{
status: &h.Projects[k].Cmds.Vet,
cmd: "go",
options: []string{"vet"},
name: "Go Vet",
}
}
h.Projects[k].parent = h
h.Projects[k].Settings = *h.Settings
h.Projects[k].path = h.Projects[k].Path
// env variables
for key, item := range h.Projects[k].Environment {
if err := os.Setenv(key, item); err != nil {
h.Projects[k].Buffer.StdErr = append(h.Projects[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
}
}
// base path of the project
wd, err := os.Getwd()
if err != nil {
return err
}
if element.path == "." || element.path == "/" {
h.Projects[k].base = wd
h.Projects[k].path = element.Wdir()
} else if filepath.IsAbs(element.path) {
h.Projects[k].base = element.path
} else {
h.Projects[k].base = filepath.Join(wd, element.path)
}
if h.Legacy.Interval != 0 {
go h.Projects[k].watchByPolling()
} else {
go h.Projects[k].watchByNotify()
}
}
wg.Wait()
return nil
}
return err
}
// Add a new project
func (h *Blueprint) Add(p *cli.Context) error {
project := Project{
Name: h.Name(p.String("name"), p.String("path")),
Path: h.Path(p.String("path")),
Cmds: Cmds{
Vet: p.Bool("vet"),
Fmt: Cmd{
Status: !p.Bool("no-fmt"),
},
Test: Cmd{
Status: !p.Bool("test"),
},
Generate: Cmd{
Status: !p.Bool("generate"),
},
Build: Cmd{
Status: p.Bool("build"),
},
Install: Cmd{
Status: !p.Bool("no-install"),
},
Run: !p.Bool("no-run"),
},
Args: argsParam(p),
Watcher: Watcher{
Paths: []string{"/"},
Ignore: []string{"vendor"},
Exts: []string{".go"},
Preview: p.Bool("preview"),
},
}
if _, err := duplicates(project, h.Projects); err != nil {
return err
}
h.Projects = append(h.Projects, project)
return nil
}
// Clean duplicate projects
func (h *Blueprint) Clean() {
arr := h.Projects
@ -144,17 +22,6 @@ func (h *Blueprint) Clean() {
}
}
// Remove a project
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
}
}
return errors.New("No project found.")
}
// List of all the projects
func (h *Blueprint) List() error {
err := h.check()
@ -212,3 +79,137 @@ func (h *Blueprint) check() error {
}
return errors.New("There are no projects.")
}
// Add a new project
func (h *Blueprint) Add(p *cli.Context) error {
project := Project{
Name: h.Name(p.String("name"), p.String("path")),
Path: h.Path(p.String("path")),
Cmds: Cmds{
Vet: 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: argsParam(p),
Watcher: Watcher{
Paths: []string{"/"},
Ignore: []string{"vendor"},
Exts: []string{".go"},
},
}
if _, err := duplicates(project, h.Projects); err != nil {
return err
}
h.Projects = append(h.Projects, project)
return nil
}
// Run launches the toolchain for each project
func (h *Blueprint) Run(p *cli.Context) error {
err := h.check()
if err == nil {
// loop projects
if p.String("name") != "" {
wg.Add(1)
} else {
wg.Add(len(h.Projects))
}
for k, element := range h.Projects {
if p.String("name") != "" && h.Projects[k].Name != p.String("name") {
continue
}
if element.Cmds.Fmt.Status {
if len(element.Cmds.Fmt.Args) == 0{
element.Cmds.Fmt.Args = []string{"-s", "-w", "-e"}
}
h.Projects[k].tools.Fmt = tool{
status: element.Cmds.Fmt.Status,
cmd: "gofmt",
options: arguments([]string{}, element.Cmds.Fmt.Args),
name: "Go Fmt",
}
}
if element.Cmds.Generate.Status {
h.Projects[k].tools.Generate = tool{
status: element.Cmds.Generate.Status,
cmd: "go",
options: arguments([]string{"generate"}, element.Cmds.Generate.Args),
name: "Go Generate",
}
}
if element.Cmds.Test.Status {
h.Projects[k].tools.Test = tool{
status: element.Cmds.Test.Status,
cmd: "go",
options: arguments([]string{"test"}, element.Cmds.Test.Args),
name: "Go Test",
}
}
if element.Cmds.Vet {
h.Projects[k].tools.Vet = tool{
status: element.Cmds.Vet,
cmd: "go",
options: []string{"vet"},
name: "Go Vet",
}
}
h.Projects[k].parent = h
h.Projects[k].Settings = *h.Settings
h.Projects[k].path = h.Projects[k].Path
// env variables
for key, item := range h.Projects[k].Environment {
if err := os.Setenv(key, item); err != nil {
h.Projects[k].Buffer.StdErr = append(h.Projects[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
}
}
// base path of the project
wd, err := os.Getwd()
if err != nil {
return err
}
if element.path == "." || element.path == "/" {
h.Projects[k].base = wd
h.Projects[k].path = element.Wdir()
} else if filepath.IsAbs(element.path) {
h.Projects[k].base = element.path
} else {
h.Projects[k].base = filepath.Join(wd, element.path)
}
if h.Legacy.Interval != 0 {
go h.Projects[k].watchByPolling()
} else {
go h.Projects[k].watchByNotify()
}
}
wg.Wait()
return nil
}
return err
}
// Remove a project
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
}
}
return errors.New("No project found.")
}

View File

@ -29,30 +29,12 @@ type Blueprint struct {
Sync chan string `yaml:"-"`
}
// Project defines the informations of a single project
type Project struct {
settings.Settings `yaml:"-"`
LastChangedOn time.Time `yaml:"-" json:"-"`
base string
Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"`
Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"`
Cmds Cmds `yaml:"commands" json:"commands"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Watcher Watcher `yaml:"watcher" json:"watcher"`
Buffer Buffer `yaml:"-" json:"buffer"`
ErrorOutputPattern string `yaml:"errorOutputPattern,omitempty" json:"errorOutputPattern,omitempty"`
parent *Blueprint
path string
tools tools
}
type tools struct {
Fmt, Test, Generate, Vet tool
}
type tool struct {
status *bool
status bool
cmd string
options []string
name string

View File

@ -12,6 +12,25 @@ import (
"strings"
)
// getEnvPath returns the first path found in env or empty string
func getEnvPath(env string) string {
path := filepath.SplitList(os.Getenv(env))
if len(path) == 0 {
return ""
}
return path[0]
}
// Check if a string is inArray
func inArray(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
// Argsparam parse one by one the given argumentes
func argsParam(params *cli.Context) []string {
argsN := params.NArg()
@ -25,6 +44,15 @@ func argsParam(params *cli.Context) []string {
return nil
}
// Split each arguments in multiple fields
func arguments(args, fields []string) []string {
for _, arg := range fields {
arr := strings.Fields(arg)
args = append(args, arr...)
}
return args
}
// Duplicates check projects with same name or same combinations of main/path
func duplicates(value Project, arr []Project) (Project, error) {
for _, val := range arr {
@ -35,35 +63,7 @@ func duplicates(value Project, arr []Project) (Project, error) {
return Project{}, nil
}
// Check if a string is inArray
func inArray(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
// Rewrite the layout of the log timestamp
func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Fprint(style.Output, style.Yellow.Regular("[")+time.Now().Format("15:04:05")+style.Yellow.Regular("]")+string(bytes))
}
// getEnvPath returns the first path found in env or empty string
func getEnvPath(env string) string {
path := filepath.SplitList(os.Getenv(env))
if len(path) == 0 {
return ""
}
return path[0]
}
// Split each arguments in multiple fields
func arguments(args, fields []string) []string {
for _, arg := range fields {
arr := strings.Fields(arg)
args = append(args, arr...)
}
return args
}