增加新特性,并修复一些问题
This commit is contained in:
@@ -3,8 +3,6 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/xxjwxc/public/dev"
|
||||
"github.com/xxjwxc/public/tools"
|
||||
@@ -20,65 +18,33 @@ type CfgBase struct {
|
||||
IsDev bool `json:"is_dev" yaml:"is_dev"` // Is it a development version?是否是开发版本
|
||||
}
|
||||
|
||||
var _map = Config{
|
||||
CfgBase: CfgBase{
|
||||
IsDev: false,
|
||||
},
|
||||
MySQLInfo: MysqlDbInfo{
|
||||
Host: "127.0.0.1",
|
||||
Port: 3306,
|
||||
Username: "root",
|
||||
Password: "root",
|
||||
Database: "test",
|
||||
},
|
||||
OutDir: "./model",
|
||||
URLTag: "json",
|
||||
Language: "中 文",
|
||||
DbTag: "gorm",
|
||||
Simple: false,
|
||||
IsWEBTag: false,
|
||||
SingularTable: true,
|
||||
IsForeignKey: true,
|
||||
IsOutSQL: false,
|
||||
IsOutFunc: true,
|
||||
IsGUI: false,
|
||||
}
|
||||
|
||||
var configPath string
|
||||
var _map = Config{}
|
||||
|
||||
func init() {
|
||||
configPath = path.Join(tools.GetModelPath(), "config.yml")
|
||||
onInit()
|
||||
dev.OnSetDev(_map.IsDev)
|
||||
}
|
||||
|
||||
func onInit() {
|
||||
err := InitFile(configPath)
|
||||
path := tools.GetModelPath()
|
||||
err := InitFile(path + "/config.yml")
|
||||
if err != nil {
|
||||
fmt.Println("Load config file error: ", err.Error())
|
||||
fmt.Println("InitFile: ", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// InitFile default value from file .
|
||||
func InitFile(filename string) error {
|
||||
if _, e := os.Stat(filename); e != nil {
|
||||
fmt.Println("init default config file: ", filename)
|
||||
if err := SaveToFile(); err == nil {
|
||||
fmt.Println("done,please restart.")
|
||||
} else {
|
||||
fmt.Println("shit,fail", err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
bs, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(bs, &_map); err != nil {
|
||||
fmt.Println("read config file error: ", err.Error())
|
||||
fmt.Println("read toml error: ", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -106,8 +72,9 @@ func SaveToFile() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tools.WriteFile(configPath, []string{
|
||||
tools.WriteFile(tools.GetModelPath()+"/config.yml", []string{
|
||||
string(d),
|
||||
}, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user