From 04b61dfc883a967222952c3189f00d09e8b0c6ae Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 21 Aug 2016 11:10:07 +0200 Subject: [PATCH] workflow yaml add/create rewritten --- main.go | 27 +++------------------------ realize/config.go | 45 ++++++++++++++++++++------------------------- 2 files changed, 23 insertions(+), 49 deletions(-) diff --git a/main.go b/main.go index 14516f9..06c4908 100644 --- a/main.go +++ b/main.go @@ -57,27 +57,6 @@ func main() { return nil }, }, - { - Name: "start", - Category: "config", - Aliases: []string{"s"}, - Usage: "Create the initial config", - Flags: []cli.Flag{ - &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"}, - &cli.BoolFlag{Name: "bin"}, - }, - Action: func(p *cli.Context) error { - y := r.New(p) - return handle(y.Create(p)) - }, - Before: func(c *cli.Context) error { - header() - return nil - }, - }, { Name: "add", Category: "config", @@ -86,9 +65,9 @@ func main() { Flags: []cli.Flag{ &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"}, - &cli.BoolFlag{Name: "bin"}, + &cli.BoolFlag{Name: "build", Value: false, Usage:"Enable go build"}, + &cli.BoolFlag{Name: "run", Usage:"Disable go run"}, + &cli.BoolFlag{Name: "bin", Usage:"Disable go install"}, }, Action: func(p *cli.Context) error { y := r.New(p) diff --git a/realize/config.go b/realize/config.go index b16803e..2061d62 100644 --- a/realize/config.go +++ b/realize/config.go @@ -79,22 +79,26 @@ func (h *Config) Clean() { // Read, Check and remove duplicates from the config file func (h *Config) Read() error { - file, err := ioutil.ReadFile(h.file) + _, err := os.Stat(h.file) if err == nil { - if len(h.Projects) > 0 { - err = yaml.Unmarshal(file, h) - if err == nil { - h.Clean() + file, err := ioutil.ReadFile(h.file) + if err == nil { + if len(h.Projects) > 0 { + err = yaml.Unmarshal(file, h) + if err == nil { + h.Clean() + } + return err } - return err + return errors.New("There are no projects") } - return errors.New("There are no projects") + return h.Create() } return err } -// write and marshal yaml -func (h *Config) Write() error { +// Create and unmarshal yaml config file +func (h *Config) Create() error { y, err := yaml.Marshal(h) if err != nil { return err @@ -102,20 +106,6 @@ func (h *Config) Write() error { return ioutil.WriteFile(h.file, y, 0755) } -// Create config yaml file -func (h *Config) Create(params *cli.Context) error { - if h.Read() != nil { - err := h.Write() - if err != nil { - os.Remove(h.file) - } else { - fmt.Println(Green("The config file was successfully created")) - } - return err - } - return errors.New("The config file already exists, check for realize.config.yaml") -} - // Add another project func (h *Config) Add(params *cli.Context) error { err := h.Read() @@ -136,10 +126,15 @@ func (h *Config) Add(params *cli.Context) error { return err } h.Projects = append(h.Projects, new) - err = h.Write() + err = h.Create() if err == nil { fmt.Println(Green("Your project was successfully added")) } + return err + } + err = h.Create() + if err == nil{ + fmt.Println(Green("The config file was successfully created")) } return err } @@ -151,7 +146,7 @@ func (h *Config) Remove(params *cli.Context) error { for key, val := range h.Projects { if params.String("name") == val.Name { h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) - err = h.Write() + err = h.Create() if err == nil { fmt.Println(Green("Your project was successfully removed")) }