method for clean by duplicate projects

This commit is contained in:
alessio 2016-07-25 01:02:18 +02:00
parent b8221695ba
commit 9a93840beb
4 changed files with 65 additions and 41 deletions

View File

@ -2,3 +2,12 @@
### Overview ### Overview
Run, build and watch file changes with custom paths 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
- [ ] Command watch - watch changes
- [ ] Live reload for web project
- [x] Remove duplicate projects

8
glide.lock generated
View File

@ -1,8 +1,8 @@
hash: 7bade7c00b648e1da872c366bddaf5fd79edb0b33b68a552e40d1856932f06da hash: ef1aaff11d9be4abdfb2f2152fe8bcb49e255fe27796ab61b938144a0098f883
updated: 2016-07-12T22:45:49.351419881+02:00 updated: 2016-07-25T00:16:27.960277787+02:00
imports: imports:
- name: gopkg.in/urfave/cli.v2 - name: gopkg.in/urfave/cli.v2
version: 2b959bd8919d1110df44ec9a839e25a739906c3c version: ee2e8aa5b22f55f006c27fd35765c62249a39aad
- name: gopkg.in/yaml.v2 - name: gopkg.in/yaml.v2
version: a83829b6f1293c91addabc89d0571c246397bbf4 version: e4d366fc3c7938e2958e662b4258c7a89e1f0e3e
devImports: [] devImports: []

12
main.go
View File

@ -46,9 +46,8 @@ func main() {
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true}, &cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true},
}, },
Action: func(params *cli.Context) error { Action: func(params *cli.Context) error {
y := realize.Config{} y := realize.New(params)
y.Init(params) return handle(y.Create(params))
return handle(y.Create())
}, },
}, },
{ {
@ -62,10 +61,9 @@ func main() {
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true}, &cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true},
}, },
Action: func(params *cli.Context) error { Action: func(params *cli.Context) error {
y := realize.Config{} y := realize.New(params)
err := y.Read() y.Read()
y.Add(params) return handle(y.Add(params))
return handle(err)
}, },
}, },
}, },

View File

@ -18,6 +18,7 @@ type Project struct {
Run bool `yaml:"app_run,omitempty"` Run bool `yaml:"app_run,omitempty"`
Build bool `yaml:"app_build,omitempty"` Build bool `yaml:"app_build,omitempty"`
Main string `yaml:"app_main,omitempty"` Main string `yaml:"app_main,omitempty"`
Name string `yaml:"app_name,omitempty"`
Watcher Watcher `yaml:"app_watcher,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"`
} }
@ -28,23 +29,12 @@ type Watcher struct{
Exts []string `yaml:"exts,omitempty"` Exts []string `yaml:"exts,omitempty"`
} }
var file = "realize.config.yaml"
// Check file exists and clean by duplicates
func (h *Config) Check() error{
// clean duplicates
_, err := ioutil.ReadFile(h.file);
if err != nil {
return err
}
return nil
}
// Default value // Default value
func (h *Config) Init(params *cli.Context) { func New(params *cli.Context) *Config{
h.file = file return &Config{
h.Version = "1.0" file: "realize.config.yaml",
h.Projects = []Project{ Version: "1.0",
Projects: []Project{
{ {
Main: params.String("main"), Main: params.String("main"),
Run: params.Bool("run"), Run: params.Bool("run"),
@ -54,13 +44,39 @@ func (h *Config) Init(params *cli.Context) {
Exts: []string{"go"}, Exts: []string{"go"},
}, },
}, },
},
} }
} }
// Read config file // check for duplicates
func Duplicates(value string, arr []Project) bool{
for _, val := range arr{
if value == val.Main{
return true
}
}
return false
}
// Remove duplicate projects
func (h *Config) Clean() {
arr := h.Projects
for key, val := range arr {
if Duplicates(val.Main, arr[key+1:]) {
h.Projects = append(arr[:key], arr[key+1:]...)
break
}
}
}
// Check, Read and remove duplicates from the config file
func (h *Config) Read() error{ func (h *Config) Read() error{
if file, err := ioutil.ReadFile(file); err == nil{ if file, err := ioutil.ReadFile(h.file); err == nil{
return yaml.Unmarshal(file, &h) err = yaml.Unmarshal(file, h)
if err == nil {
h.Clean()
}
return err
}else{ }else{
return err return err
} }
@ -68,8 +84,7 @@ func (h *Config) Read() error{
// Create config yaml file // Create config yaml file
func (h *Config) Create(params *cli.Context) error{ func (h *Config) Create(params *cli.Context) error{
h.Init(params) if h.Read() != nil {
if h.Check() == nil {
if y, err := yaml.Marshal(h); err == nil { if y, err := yaml.Marshal(h); err == nil {
err = ioutil.WriteFile(h.file, y, 0755) err = ioutil.WriteFile(h.file, y, 0755)
if err != nil { if err != nil {
@ -81,12 +96,12 @@ func (h *Config) Create(params *cli.Context) error{
return err return err
} }
} }
return errors.New("The configuration file already exist") 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{
if h.Check() == nil { if h.Read() == nil {
new := Project{ new := Project{
Main: params.String("main"), Main: params.String("main"),
Run: params.Bool("run"), Run: params.Bool("run"),
@ -96,13 +111,15 @@ func (h *Config) Add(params *cli.Context) error{
Exts: []string{"go"}, Exts: []string{"go"},
}, },
} }
if Duplicates(new.Main, h.Projects) {
return errors.New("There is already one project with same main path")
}
h.Projects = append(h.Projects, new) h.Projects = append(h.Projects, new)
y, err := yaml.Marshal(h) y, err := yaml.Marshal(h)
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(file, y, 0755) return ioutil.WriteFile(h.file, y, 0755)
return err
} }
return errors.New("The configuration file doesn't exist") return errors.New("The configuration file doesn't exist")
} }