omitempty, config create method

This commit is contained in:
alessio 2016-07-12 19:51:11 +02:00
parent c0124db081
commit b3858f3b3e
2 changed files with 49 additions and 28 deletions

11
main.go
View File

@ -6,13 +6,14 @@ import (
"github.com/tockins/realize/realize" "github.com/tockins/realize/realize"
) )
type person struct {
name string
age int
}
func main() { func main() {
t := realize.Config{ t := realize.Init()
App_file:"realize.config.yaml",
App_version:"1.0",
App_main:[]string{"main.go"},
}
t.Create() t.Create()
//app := &cli.App{ //app := &cli.App{

View File

@ -6,24 +6,57 @@ import (
) )
type Config struct { type Config struct {
App_file string File string `yaml:"app_file,omitempty"`
App_main []string Main []string `yaml:"app_main,omitempty"`
App_version string Version string `yaml:"app_version,omitempty"`
App_build bool Build bool `yaml:"app_build,omitempty"`
App_run struct { Watchers
before, after, paths, ext []string
} }
type Watchers struct{
Before []string `yaml:"app_before,omitempty"`
After []string `yaml:"app_after,omitempty"`
Paths []string `yaml:"app_paths,omitempty"`
Ext []string `yaml:"app_ext,omitempty"`
}
// Check files exists
func Check(files ...string) []bool{
var result []bool
for _, val := range files {
if _, err := os.Stat(val); err == nil {
result = append(result,true)
}
result = append(result, false)
}
return result
}
// Default value
func Init() Config{
config := Config{
File:"realize.config.yaml",
Main:[]string{"main.go"},
Version:"1.0",
Build: true,
Watchers: Watchers{
Paths: []string{"/"},
Ext: []string{"go"},
},
}
return config
} }
// Create config yaml file // Create config yaml file
func (h *Config) Create() bool{ func (h *Config) Create() bool{
var config = Check(h.App_file) var config = Check(h.File)
if config[0] == false { if config[0] == false {
if w, err := os.Create(h.App_file); err == nil { if w, err := os.Create(h.File); err == nil {
defer w.Close() defer w.Close()
y, err := yaml.Marshal(&h) y, err := yaml.Marshal(h)
if err != nil { if err != nil {
defer panic(err) panic(err)
} }
w.WriteString(string(y)) w.WriteString(string(y))
return true return true
@ -39,16 +72,3 @@ func (h *Config) Read(field string) bool {
return true return true
} }
// Check files exists
func Check(files ...string) []bool{
var result []bool
for _, val := range files {
if _, err := os.Stat(val); err == nil {
result = append(result,true)
}
result = append(result, false)
}
return result
}