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
},
},
{
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)

View File

@ -79,6 +79,8 @@ func (h *Config) Clean() {
// Read, Check and remove duplicates from the config file
func (h *Config) Read() error {
_, err := os.Stat(h.file)
if err == nil {
file, err := ioutil.ReadFile(h.file)
if err == nil {
if len(h.Projects) > 0 {
@ -90,11 +92,13 @@ func (h *Config) Read() error {
}
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"))
}