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
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
updated: 2016-07-12T22:45:49.351419881+02:00
hash: ef1aaff11d9be4abdfb2f2152fe8bcb49e255fe27796ab61b938144a0098f883
updated: 2016-07-25T00:16:27.960277787+02:00
imports:
- name: gopkg.in/urfave/cli.v2
version: 2b959bd8919d1110df44ec9a839e25a739906c3c
version: ee2e8aa5b22f55f006c27fd35765c62249a39aad
- name: gopkg.in/yaml.v2
version: a83829b6f1293c91addabc89d0571c246397bbf4
version: e4d366fc3c7938e2958e662b4258c7a89e1f0e3e
devImports: []

12
main.go
View File

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

View File

@ -18,6 +18,7 @@ type Project struct {
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"`
}
@ -28,39 +29,54 @@ type Watcher struct{
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
func (h *Config) Init(params *cli.Context) {
h.file = file
h.Version = "1.0"
h.Projects = []Project{
{
Main: params.String("main"),
Run: params.Bool("run"),
Build: params.Bool("build"),
Watcher: Watcher{
func New(params *cli.Context) *Config{
return &Config{
file: "realize.config.yaml",
Version: "1.0",
Projects: []Project{
{
Main: params.String("main"),
Run: params.Bool("run"),
Build: params.Bool("build"),
Watcher: Watcher{
Paths: []string{"/"},
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{
if file, err := ioutil.ReadFile(file); err == nil{
return yaml.Unmarshal(file, &h)
if file, err := ioutil.ReadFile(h.file); err == nil{
err = yaml.Unmarshal(file, h)
if err == nil {
h.Clean()
}
return err
}else{
return err
}
@ -68,8 +84,7 @@ func (h *Config) Read() error{
// Create config yaml file
func (h *Config) Create(params *cli.Context) error{
h.Init(params)
if h.Check() == nil {
if h.Read() != nil {
if y, err := yaml.Marshal(h); err == nil {
err = ioutil.WriteFile(h.file, y, 0755)
if err != nil {
@ -81,12 +96,12 @@ func (h *Config) Create(params *cli.Context) error{
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
func (h *Config) Add(params *cli.Context) error{
if h.Check() == nil {
if h.Read() == nil {
new := Project{
Main: params.String("main"),
Run: params.Bool("run"),
@ -96,13 +111,15 @@ 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")
}
h.Projects = append(h.Projects, new)
y, err := yaml.Marshal(h)
if err != nil {
return err
}
err = ioutil.WriteFile(file, y, 0755)
return err
return ioutil.WriteFile(h.file, y, 0755)
}
return errors.New("The configuration file doesn't exist")
}