From c2da6af758b58967bc82db751ad172b7b894e5a9 Mon Sep 17 00:00:00 2001 From: alessio Date: Tue, 26 Jul 2016 19:04:13 +0200 Subject: [PATCH] Command projects list --- README.md | 3 +- glide.lock | 12 ++++++- glide.yaml | 1 + main.go | 26 ++++++++++++++- realize/config.go | 82 ++++++++++++++++++++++++++++++++++++----------- 5 files changed, 102 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 26c2973..3f5d7b4 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ Run, build and watch file changes with custom paths ### To do - [x] Command start - default config gile - [x] Command add - new project on the config file -- [ ] Command remove - remove project from the config file +- [x] Command remove - remove project from the config file - [ ] Command watch - watch changes +- [x] Command list - print projects list - [ ] Live reload for web project - [x] Remove duplicate projects diff --git a/glide.lock b/glide.lock index 110a662..9343ecf 100644 --- a/glide.lock +++ b/glide.lock @@ -1,6 +1,16 @@ hash: ef1aaff11d9be4abdfb2f2152fe8bcb49e255fe27796ab61b938144a0098f883 -updated: 2016-07-25T00:16:27.960277787+02:00 +updated: 2016-07-26T18:14:33.237501635+02:00 imports: +- name: github.com/fatih/color + version: 87d4004f2ab62d0d255e0a38f1680aa534549fe3 +- name: github.com/mattn/go-colorable + version: 9056b7a9f2d1f2d96498d6d146acd1f9d5ed3d59 +- name: github.com/mattn/go-isatty + version: 56b76bdf51f7708750eac80fa38b952bb9f32639 +- name: golang.org/x/sys + version: a646d33e2ee3172a661fc09bca23bb4889a41bc8 + subpackages: + - unix - name: gopkg.in/urfave/cli.v2 version: ee2e8aa5b22f55f006c27fd35765c62249a39aad - name: gopkg.in/yaml.v2 diff --git a/glide.yaml b/glide.yaml index 8ee28c4..7e4cfbd 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,3 +2,4 @@ package: github.com/tockins/realize import: - package: gopkg.in/urfave/cli.v2 - package: gopkg.in/yaml.v2 +- package: github.com/fatih/color diff --git a/main.go b/main.go index ba7d60b..f0c63cd 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func main() { Aliases: []string{"s"}, Usage: "create the initial config file", Flags: []cli.Flag{ + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go"}, &cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: true}, &cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true}, @@ -56,16 +57,39 @@ func main() { Aliases: []string{"s"}, Usage: "add another project in config file", Flags: []cli.Flag{ + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go"}, &cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: true}, &cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true}, }, Action: func(params *cli.Context) error { y := realize.New(params) - y.Read() return handle(y.Add(params)) }, }, + { + Name: "remove", + Category: "config", + Aliases: []string{"s"}, + Usage: "remove a project in config file", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, + }, + Action: func(params *cli.Context) error { + y := realize.New(params) + return handle(y.Remove(params)) + }, + }, + { + Name: "list", + Category: "config", + Aliases: []string{"s"}, + Usage: "projects list", + Action: func(params *cli.Context) error { + y := realize.New(params) + return handle(y.List()) + }, + }, }, //Flags: []cli.Flag { // &cli.StringFlag{ diff --git a/realize/config.go b/realize/config.go index 010c70d..5436dc2 100644 --- a/realize/config.go +++ b/realize/config.go @@ -6,6 +6,8 @@ import ( "errors" "gopkg.in/urfave/cli.v2" "io/ioutil" + "github.com/fatih/color" + "fmt" ) type Config struct { @@ -15,10 +17,10 @@ type Config struct { } type Project struct { + Name string `yaml:"app_name,omitempty"` Run bool `yaml:"app_run,omitempty"` Build bool `yaml:"app_build,omitempty"` Main string `yaml:"app_main,omitempty"` - Name string `yaml:"app_name,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"` } @@ -36,6 +38,7 @@ func New(params *cli.Context) *Config{ Version: "1.0", Projects: []Project{ { + Name: params.String("name"), Main: params.String("main"), Run: params.Bool("run"), Build: params.Bool("build"), @@ -49,9 +52,9 @@ func New(params *cli.Context) *Config{ } // check for duplicates -func Duplicates(value string, arr []Project) bool{ +func Duplicates(value Project, arr []Project) bool{ for _, val := range arr{ - if value == val.Main{ + if value.Main == val.Main || value.Name == val.Name{ return true } } @@ -62,7 +65,7 @@ func Duplicates(value string, arr []Project) bool{ func (h *Config) Clean() { arr := h.Projects for key, val := range arr { - if Duplicates(val.Main, arr[key+1:]) { + if Duplicates(val, arr[key+1:]) { h.Projects = append(arr[:key], arr[key+1:]...) break } @@ -82,15 +85,20 @@ func (h *Config) Read() error{ } } +// write and marshal +func (h *Config) Write() error{ + y, err := yaml.Marshal(h) + if err != nil { + return err + } + return ioutil.WriteFile(h.file, y, 0755) +} + // Create config yaml file func (h *Config) Create(params *cli.Context) error{ if h.Read() != nil { - if y, err := yaml.Marshal(h); err == nil { - err = ioutil.WriteFile(h.file, y, 0755) - if err != nil { - os.Remove(h.file) - return err - } + if err := h.Write(); err != nil { + os.Remove(h.file) return err }else{ return err @@ -101,8 +109,9 @@ func (h *Config) Create(params *cli.Context) error{ // Add another project func (h *Config) Add(params *cli.Context) error{ - if h.Read() == nil { + if err := h.Read(); err == nil { new := Project{ + Name: params.String("name"), Main: params.String("main"), Run: params.Bool("run"), Build: params.Bool("build"), @@ -111,17 +120,52 @@ func (h *Config) Add(params *cli.Context) error{ Exts: []string{"go"}, }, } - if Duplicates(new.Main, h.Projects) { - return errors.New("There is already one project with same main path") + if Duplicates(new, h.Projects) { + return errors.New("There is already one project with same path or name") } h.Projects = append(h.Projects, new) - y, err := yaml.Marshal(h) - if err != nil { - return err - } - return ioutil.WriteFile(h.file, y, 0755) + return h.Write() + }else{ + return err + } +} + +// remove a project in list +func (h *Config) Remove(params *cli.Context) error{ + if err := h.Read(); err == nil { + for key, val := range h.Projects { + if params.String("name") == val.Name { + h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) + return h.Write() + } + } + return errors.New("No project found") + }else{ + return err + } +} + +// List of projects +func (h *Config) List() error{ + if err := h.Read(); err == nil { + green := color.New(color.FgGreen, color.Bold).SprintFunc() + greenl := color.New(color.FgHiGreen).SprintFunc() + red := color.New(color.FgRed).SprintFunc() + for _, val := range h.Projects { + fmt.Println(green("|"), green(val.Name)) + fmt.Println(greenl("|"),"\t", green("Main Path:"), red(val.Main)) + fmt.Println(greenl("|"),"\t", green("Run:"), red(val.Run)) + fmt.Println(greenl("|"),"\t", green("Build:"), red(val.Build)) + fmt.Println(greenl("|"),"\t", green("Watcher:")) + fmt.Println(greenl("|"),"\t\t", green("After:"), red(val.Watcher.After)) + fmt.Println(greenl("|"),"\t\t", green("Before:"), red(val.Watcher.Before)) + fmt.Println(greenl("|"),"\t\t", green("Extensions:"), red(val.Watcher.Exts)) + fmt.Println(greenl("|"),"\t\t", green("Paths:"), red(val.Watcher.Paths)) + } + return nil + }else{ + return err } - return errors.New("The configuration file doesn't exist") }