less parameters and more appropriate values for start/add

This commit is contained in:
alessio 2016-08-21 09:16:01 +02:00
parent 7bf980913a
commit e8f87d76d4
3 changed files with 37 additions and 17 deletions

25
main.go
View File

@ -4,7 +4,8 @@ import (
r "github.com/tockins/realize/realize" r "github.com/tockins/realize/realize"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"os" "os"
"log" "fmt"
"strings"
) )
func main() { func main() {
@ -13,7 +14,7 @@ func main() {
handle := func(err error) error { handle := func(err error) error {
if err != nil { if err != nil {
log.Println(r.Red(err.Error())) fmt.Println(r.Red(err.Error()))
return nil return nil
} }
return nil return nil
@ -23,6 +24,16 @@ func main() {
app.Information() 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{ cli := &cli.App{
Name: app.Name, Name: app.Name,
Version: app.Version, Version: app.Version,
@ -52,8 +63,8 @@ func main() {
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "Create the initial config", Usage: "Create the initial config",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "", Usage: "Project name \t"},
&cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: "/", Usage: "Project base path \t"}, &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: wd(), Usage: "Project base path \t"},
&cli.BoolFlag{Name: "build", Value: false}, &cli.BoolFlag{Name: "build", Value: false},
&cli.BoolFlag{Name: "run", Value: true}, &cli.BoolFlag{Name: "run", Value: true},
&cli.BoolFlag{Name: "bin", Value: true}, &cli.BoolFlag{Name: "bin", Value: true},
@ -73,8 +84,8 @@ func main() {
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "Add another project", Usage: "Add another project",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Usage: "Project name \t"},
&cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: "/", Usage: "Project base path \t"}, &cli.StringFlag{Name: "base", Aliases: []string{"b"}, Value: wd(), Usage: "Project base path \t"},
&cli.BoolFlag{Name: "build", Value: false}, &cli.BoolFlag{Name: "build", Value: false},
&cli.BoolFlag{Name: "run", Value: true}, &cli.BoolFlag{Name: "run", Value: true},
&cli.BoolFlag{Name: "bin", Value: true}, &cli.BoolFlag{Name: "bin", Value: true},
@ -94,7 +105,7 @@ func main() {
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "Remove a project", Usage: "Remove a project",
Flags: []cli.Flag{ 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 { Action: func(p *cli.Context) error {
y := r.New(p) y := r.New(p)

View File

@ -16,6 +16,16 @@ type Config struct {
Projects []Project 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 // New method puts the cli params in the struct
func New(params *cli.Context) *Config { func New(params *cli.Context) *Config {
return &Config{ return &Config{
@ -23,7 +33,7 @@ func New(params *cli.Context) *Config {
Version: AppVersion, Version: AppVersion,
Projects: []Project{ Projects: []Project{
{ {
Name: params.String("name"), Name: projectName(params),
Path: params.String("base"), Path: params.String("base"),
Run: params.Bool("run"), Run: params.Bool("run"),
Build: params.Bool("build"), 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 // 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 { for _, val := range arr {
if value.Path == val.Path || value.Name == val.Name { if value.Path == val.Path || value.Name == val.Name {
fmt.Println(Red("There is a duplicate of '"+val.Name+"'. Check your config file!")) return errors.New("There is a duplicate of '"+val.Name+"'. Check your config file!")
return true
} }
} }
return false return nil
} }
// Clean duplicate projects // Clean duplicate projects
func (h *Config) Clean() { func (h *Config) Clean() {
arr := h.Projects arr := h.Projects
for key, val := range arr { 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:]...) h.Projects = append(arr[:key], arr[key+1:]...)
break break
} }
@ -104,7 +113,7 @@ func (h *Config) Add(params *cli.Context) error {
err := h.Read() err := h.Read()
if err == nil { if err == nil {
new := Project{ new := Project{
Name: params.String("name"), Name: projectName(params),
Path: params.String("base"), Path: params.String("base"),
Run: params.Bool("run"), Run: params.Bool("run"),
Build: params.Bool("build"), Build: params.Bool("build"),
@ -115,8 +124,8 @@ func (h *Config) Add(params *cli.Context) error {
Ignore: watcherIgnores, Ignore: watcherIgnores,
}, },
} }
if Duplicates(new, h.Projects) { if err := Duplicates(new, h.Projects); err != nil {
return errors.New("There is already one project with same path or name") return err
} }
h.Projects = append(h.Projects, new) h.Projects = append(h.Projects, new)
err = h.Write() err = h.Write()

View File

@ -141,7 +141,7 @@ func (p *Project) install(channel chan bool,wr *sync.WaitGroup) {
for { for {
select { select {
case <-runner: 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 return
} }
} }