moved from os to ioutil

This commit is contained in:
alessio 2016-07-24 01:57:07 +02:00
parent 0e78ce904d
commit f295bfa544
1 changed files with 28 additions and 22 deletions

View File

@ -5,7 +5,6 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"errors" "errors"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"path/filepath"
"io/ioutil" "io/ioutil"
) )
@ -34,7 +33,7 @@ var file = "realize.config.yaml"
// Check files exists // Check files exists
func Check(files ...string) (result []bool){ func Check(files ...string) (result []bool){
for _, val := range files { for _, val := range files {
if _, err := os.Stat(val); err == nil { if _, err := ioutil.ReadFile(val); err == nil {
result = append(result,true) result = append(result,true)
} }
result = append(result, false) result = append(result, false)
@ -61,12 +60,8 @@ func (h *Config) Init(params *cli.Context) {
// Read config file // Read config file
func (h *Config) Read() error{ func (h *Config) Read() error{
if filename, err := filepath.Abs("./"+file); err == nil{ if file, err := ioutil.ReadFile(file); err == nil{
y, err := ioutil.ReadFile(filename) return yaml.Unmarshal(file, &h)
if err != nil {
return err
}
return yaml.Unmarshal(y, &h)
}else{ }else{
return err return err
} }
@ -76,31 +71,42 @@ func (h *Config) Read() error{
func (h *Config) Create() error{ func (h *Config) Create() error{
config := Check(h.file) config := Check(h.file)
if config[0] == false { if config[0] == false {
if w, err := os.Create(h.file); err == nil { if y, err := yaml.Marshal(h); err == nil {
y, err := yaml.Marshal(h) err = ioutil.WriteFile(h.file, y, 0755)
if err != nil { if err != nil {
os.Remove(h.file) os.Remove(h.file)
return err return err
} }
_, err = w.WriteString(string(y)) return err
}else{
return err return err
} }
return errors.New("There is a problem with the file's creation")
} }
return errors.New("The configuration file already exist") return errors.New("The configuration file already exist")
} }
// Add another project // Add another project
func (h *Config) Add(params *cli.Context) { func (h *Config) Add(params *cli.Context) error{
new := Project{ config := Check(file)
Main: params.String("main"), if config[0] == true {
Run: params.Bool("run"), new := Project{
Build: params.Bool("build"), Main: params.String("main"),
Watcher: Watcher{ Run: params.Bool("run"),
Paths: []string{"/"}, Build: params.Bool("build"),
Exts: []string{"go"}, Watcher: Watcher{
}, Paths: []string{"/"},
Exts: []string{"go"},
},
}
h.Projects = append(h.Projects, new)
y, err := yaml.Marshal(h)
if err != nil {
return err
}
err = ioutil.WriteFile(file, y, 0755)
return err
} }
h.Projects = append(h.Projects, new) return errors.New("The configuration file doesn't exist")
} }