workflow yaml add/create rewritten

This commit is contained in:
alessio 2016-08-21 11:10:07 +02:00
parent fc41826070
commit 04b61dfc88
2 changed files with 23 additions and 49 deletions

27
main.go
View File

@ -57,27 +57,6 @@ func main() {
return nil 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", Name: "add",
Category: "config", Category: "config",
@ -86,9 +65,9 @@ func main() {
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Usage: "Project name \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.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, Usage:"Enable go build"},
&cli.BoolFlag{Name: "run"}, &cli.BoolFlag{Name: "run", Usage:"Disable go run"},
&cli.BoolFlag{Name: "bin"}, &cli.BoolFlag{Name: "bin", Usage:"Disable go install"},
}, },
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
y := r.New(p) y := r.New(p)

View File

@ -79,22 +79,26 @@ func (h *Config) Clean() {
// Read, Check and remove duplicates from the config file // Read, Check and remove duplicates from the config file
func (h *Config) Read() error { func (h *Config) Read() error {
file, err := ioutil.ReadFile(h.file) _, err := os.Stat(h.file)
if err == nil { if err == nil {
if len(h.Projects) > 0 { file, err := ioutil.ReadFile(h.file)
err = yaml.Unmarshal(file, h) if err == nil {
if err == nil { if len(h.Projects) > 0 {
h.Clean() 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 return err
} }
// write and marshal yaml // Create and unmarshal yaml config file
func (h *Config) Write() error { func (h *Config) Create() error {
y, err := yaml.Marshal(h) y, err := yaml.Marshal(h)
if err != nil { if err != nil {
return err return err
@ -102,20 +106,6 @@ func (h *Config) Write() error {
return ioutil.WriteFile(h.file, y, 0755) 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 // Add another project
func (h *Config) Add(params *cli.Context) error { func (h *Config) Add(params *cli.Context) error {
err := h.Read() err := h.Read()
@ -136,10 +126,15 @@ func (h *Config) Add(params *cli.Context) error {
return err return err
} }
h.Projects = append(h.Projects, new) h.Projects = append(h.Projects, new)
err = h.Write() err = h.Create()
if err == nil { if err == nil {
fmt.Println(Green("Your project was successfully added")) 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 return err
} }
@ -151,7 +146,7 @@ func (h *Config) Remove(params *cli.Context) error {
for key, val := range h.Projects { for key, val := range h.Projects {
if params.String("name") == val.Name { if params.String("name") == val.Name {
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...) h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
err = h.Write() err = h.Create()
if err == nil { if err == nil {
fmt.Println(Green("Your project was successfully removed")) fmt.Println(Green("Your project was successfully removed"))
} }