methods rewritten

This commit is contained in:
alessio 2016-07-24 00:46:25 +02:00
parent e4fe6f4758
commit 83d9f183d1
1 changed files with 74 additions and 42 deletions

View File

@ -4,25 +4,35 @@ import (
"os" "os"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"errors" "errors"
"gopkg.in/urfave/cli.v2"
"path/filepath"
"io/ioutil"
) )
type Config struct { type Config struct {
File string `yaml:"app_file,omitempty"` file string `yaml:"app_file,omitempty"`
Main []string `yaml:"app_main,omitempty"` Version string `yaml:"version,omitempty"`
Version string `yaml:"app_version,omitempty"` Projects []Project
Build bool `yaml:"app_build,omitempty"`
Watchers
} }
type Watchers struct{ type Project struct {
Before []string `yaml:"app_before,omitempty"` Run bool `yaml:"app_run,omitempty"`
After []string `yaml:"app_after,omitempty"` Build bool `yaml:"app_build,omitempty"`
Paths []string `yaml:"app_paths,omitempty"` Main string `yaml:"app_main,omitempty"`
Ext []string `yaml:"app_ext,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"`
} }
type Watcher struct{
Before []string `yaml:"before,omitempty"`
After []string `yaml:"after,omitempty"`
Paths []string `yaml:"paths,omitempty"`
Exts []string `yaml:"exts,omitempty"`
}
var file = "realize.config.yaml"
// Check files exists // Check files exists
func Check(files ...string) (result []bool, err error){ func Check(files ...string) (result []bool){
for _, val := range files { for _, val := range files {
if _, err := os.Stat(val); err == nil { if _, err := os.Stat(val); err == nil {
result = append(result,true) result = append(result,true)
@ -33,41 +43,63 @@ func Check(files ...string) (result []bool, err error){
} }
// Default value // Default value
func Init() Config{ func (h *Config) Init(params *cli.Context) {
config := Config{ h.file = file
File:"realize.config.yaml", h.Version = "1.0"
Main:[]string{"main.go"}, h.Projects = []Project{
Version:"1.0", {
Build: true, Main: params.String("main"),
Watchers: Watchers{ Run: params.Bool("run"),
Paths: []string{"/"}, Build: params.Bool("build"),
Ext: []string{"go"}, Watcher: Watcher{
Paths: []string{"/"},
Exts: []string{"go"},
},
}, },
} }
return config
}
// Create config yaml file
func (h *Config) Create() (result bool, err error){
config, err := Check(h.File)
if config[0] == false {
if w, err := os.Create(h.File); err == nil {
defer w.Close()
y, err := yaml.Marshal(h)
_, err = w.WriteString(string(y))
if err != nil {
os.Remove(h.File)
return false, err
}
return true, nil
}
return false, err
}
return false, errors.New("already exist")
} }
// Read config file // Read config file
func (h *Config) Read(field string) bool { func (h *Config) Read() error{
return true if filename, err := filepath.Abs("./"+file); err == nil{
y, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return yaml.Unmarshal(y, &h)
}else{
return err
}
}
// Create config yaml file
func (h *Config) Create() error{
config := Check(h.file)
if config[0] == false {
if w, err := os.Create(h.file); err == nil {
y, err := yaml.Marshal(h)
if err != nil {
os.Remove(h.file)
return err
}
_, err = w.WriteString(string(y))
return err
}
return errors.New("There is a problem with the file's creation")
}
return errors.New("The configuration file already exist")
}
// Add another project
func (h *Config) Add(params *cli.Context) {
//new := Project{
// Main: params.String("main"),
// Run: params.Bool("run"),
// Build: params.Bool("build"),
// Watcher: Watcher{
// Paths: []string{"/"},
// Exts: []string{"go"},
// },
//}
} }