This commit is contained in:
xxjwxc
2020-02-29 00:19:25 +08:00
parent 73c399165e
commit ce3904575d
11 changed files with 132 additions and 80 deletions

View File

@@ -9,21 +9,20 @@ import (
// Config custom config struct
type Config struct {
CfgBase `yaml:"base"`
MySQLInfo MysqlDbInfo `yaml:"mysql_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签gormt,db
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
SingularTable bool `yaml:"singular_table"`
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
TableList map[string]struct{} `yaml:"-"`
OutFileName string `yaml:"-"`
MySQLInfo MysqlDbInfo `yaml:"mysql_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签gormt,db
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
SingularTable bool `yaml:"singular_table"`
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
OutFileName string `yaml:"-"`
}
// MysqlDbInfo mysql database information. mysql 数据库信息
@@ -145,14 +144,6 @@ func SetIsTableName(b bool) {
_map.IsTableName = b
}
func SetTableList(m map[string]struct{}) {
_map.TableList = m
}
func GetTableList() map[string]struct{} {
return _map.TableList
}
func SetOutFileName(f string) {
_map.OutFileName = f
}

View File

@@ -3,6 +3,8 @@ package config
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/xxjwxc/public/dev"
"github.com/xxjwxc/public/tools"
@@ -18,33 +20,65 @@ type CfgBase struct {
IsDev bool `json:"is_dev" yaml:"is_dev"` // Is it a development version?是否是开发版本
}
var _map = Config{}
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
func init() {
configPath = path.Join(tools.GetModelPath(), "config.yml")
onInit()
dev.OnSetDev(_map.IsDev)
}
func onInit() {
path := tools.GetModelPath()
err := InitFile(path + "/config.yml")
err := InitFile(configPath)
if err != nil {
fmt.Println("InitFile: ", err.Error())
fmt.Println("Load config file error: ", 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 toml error: ", err.Error())
fmt.Println("read config file error: ", err.Error())
return err
}
return nil
}
@@ -72,9 +106,8 @@ func SaveToFile() error {
if err != nil {
return err
}
tools.WriteFile(tools.GetModelPath()+"/config.yml", []string{
tools.WriteFile(configPath, []string{
string(d),
}, true)
return nil
}