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"
"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)

View File

@ -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()

View File

@ -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
}
}