From e8f87d76d466e7bf26fed93ee6780975ff70d863 Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 21 Aug 2016 09:16:01 +0200 Subject: [PATCH] less parameters and more appropriate values for start/add --- main.go | 25 ++++++++++++++++++------- realize/config.go | 27 ++++++++++++++++++--------- realize/watcher.go | 2 +- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 74b647f..99a2093 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,8 @@ import ( r "github.com/tockins/realize/realize" "gopkg.in/urfave/cli.v2" "os" - "log" + "fmt" + "strings" ) func main() { @@ -13,7 +14,7 @@ func main() { handle := func(err error) error { if err != nil { - log.Println(r.Red(err.Error())) + fmt.Println(r.Red(err.Error())) return nil } return nil @@ -23,6 +24,16 @@ func main() { app.Information() } + wd := func() string{ + dir, err :=os.Getwd() + if err != nil{ + fmt.Println(r.Red(err)) + return "/" + } + wd := strings.Split(dir, "/") + return wd[len(wd)-1] + } + cli := &cli.App{ Name: app.Name, Version: app.Version, @@ -52,8 +63,8 @@ func main() { Aliases: []string{"s"}, Usage: "Create the initial config", Flags: []cli.Flag{ - &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, - &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: "/", Usage: "Project base path \t"}, + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Project name \t"}, + &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: wd(), Usage: "Project base path \t"}, &cli.BoolFlag{Name: "build", Value: false}, &cli.BoolFlag{Name: "run", Value: true}, &cli.BoolFlag{Name: "bin", Value: true}, @@ -73,8 +84,8 @@ func main() { Aliases: []string{"a"}, Usage: "Add another project", Flags: []cli.Flag{ - &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, - &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: "/", Usage: "Project base path \t"}, + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Usage: "Project name \t"}, + &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: wd(), Usage: "Project base path \t"}, &cli.BoolFlag{Name: "build", Value: false}, &cli.BoolFlag{Name: "run", Value: true}, &cli.BoolFlag{Name: "bin", Value: true}, @@ -94,7 +105,7 @@ func main() { Aliases: []string{"r"}, Usage: "Remove a project", Flags: []cli.Flag{ - &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""}, }, Action: func(p *cli.Context) error { y := r.New(p) diff --git a/realize/config.go b/realize/config.go index edd1ff7..378e9a7 100644 --- a/realize/config.go +++ b/realize/config.go @@ -16,6 +16,16 @@ type Config struct { Projects []Project } +func projectName(params *cli.Context) string{ + var name string + if params.String("name") == "" { + name = params.String("base") + }else{ + name = params.String("name") + } + return name +} + // New method puts the cli params in the struct func New(params *cli.Context) *Config { return &Config{ @@ -23,7 +33,7 @@ func New(params *cli.Context) *Config { Version: AppVersion, Projects: []Project{ { - Name: params.String("name"), + Name: projectName(params), Path: params.String("base"), Run: params.Bool("run"), Build: params.Bool("build"), @@ -39,21 +49,20 @@ func New(params *cli.Context) *Config { } // Duplicates check projects with same name or same combinations of main/path -func Duplicates(value Project, arr []Project) bool { +func Duplicates(value Project, arr []Project) error { for _, val := range arr { if value.Path == val.Path || value.Name == val.Name { - fmt.Println(Red("There is a duplicate of '"+val.Name+"'. Check your config file!")) - return true + return errors.New("There is a duplicate of '"+val.Name+"'. Check your config file!") } } - return false + return nil } // Clean duplicate projects func (h *Config) Clean() { arr := h.Projects for key, val := range arr { - if Duplicates(val, arr[key+1:]) { + if err := Duplicates(val, arr[key+1:]); err != nil { h.Projects = append(arr[:key], arr[key+1:]...) break } @@ -104,7 +113,7 @@ func (h *Config) Add(params *cli.Context) error { err := h.Read() if err == nil { new := Project{ - Name: params.String("name"), + Name: projectName(params), Path: params.String("base"), Run: params.Bool("run"), Build: params.Bool("build"), @@ -115,8 +124,8 @@ func (h *Config) Add(params *cli.Context) error { Ignore: watcherIgnores, }, } - if Duplicates(new, h.Projects) { - return errors.New("There is already one project with same path or name") + if err := Duplicates(new, h.Projects); err != nil { + return err } h.Projects = append(h.Projects, new) err = h.Write() diff --git a/realize/watcher.go b/realize/watcher.go index 5cb5f53..a75ac2f 100644 --- a/realize/watcher.go +++ b/realize/watcher.go @@ -141,7 +141,7 @@ func (p *Project) install(channel chan bool,wr *sync.WaitGroup) { for { select { case <-runner: - log.Println(Greenl(p.Name), ":", Green("Has been run") + " after", Magenta(time.Since(start))) + log.Println(Greenl(p.Name), ":", Green("Has been run") + " after", Magenta(time.Since(start).Nanoseconds())) return } }