omitempty, config create method
This commit is contained in:
parent
c0124db081
commit
b3858f3b3e
11
main.go
11
main.go
|
@ -6,13 +6,14 @@ import (
|
|||
"github.com/tockins/realize/realize"
|
||||
)
|
||||
|
||||
type person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
t := realize.Config{
|
||||
App_file:"realize.config.yaml",
|
||||
App_version:"1.0",
|
||||
App_main:[]string{"main.go"},
|
||||
}
|
||||
t := realize.Init()
|
||||
t.Create()
|
||||
|
||||
//app := &cli.App{
|
||||
|
|
|
@ -6,24 +6,57 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
App_file string
|
||||
App_main []string
|
||||
App_version string
|
||||
App_build bool
|
||||
App_run struct {
|
||||
before, after, paths, ext []string
|
||||
File string `yaml:"app_file,omitempty"`
|
||||
Main []string `yaml:"app_main,omitempty"`
|
||||
Version string `yaml:"app_version,omitempty"`
|
||||
Build bool `yaml:"app_build,omitempty"`
|
||||
Watchers
|
||||
}
|
||||
|
||||
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
|
||||
func (h *Config) Create() bool{
|
||||
var config = Check(h.App_file)
|
||||
var config = Check(h.File)
|
||||
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()
|
||||
y, err := yaml.Marshal(&h)
|
||||
y, err := yaml.Marshal(h)
|
||||
if err != nil {
|
||||
defer panic(err)
|
||||
panic(err)
|
||||
}
|
||||
w.WriteString(string(y))
|
||||
return true
|
||||
|
@ -39,16 +72,3 @@ func (h *Config) Read(field string) bool {
|
|||
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
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue