增加新特性,并修复一些问题

This commit is contained in:
LLLLancelot
2020-02-28 04:03:08 +08:00
parent 2bd6daaf8d
commit 9e60d56227
17 changed files with 176 additions and 131 deletions

View File

@@ -9,18 +9,21 @@ 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"` //
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:"-"`
}
// MysqlDbInfo mysql database information. mysql 数据库信息
@@ -132,6 +135,32 @@ func SetIsGUI(b bool) {
_map.IsGUI = b
}
// GetIsTableName if is table name .
func GetIsTableName() bool {
return _map.IsTableName
}
// SetIsTableName if is table name .
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
}
func GetOutFileName() string {
return _map.OutFileName
}
// GetURLTag get url tag.
func GetURLTag() string {
if _map.URLTag != "json" && _map.URLTag != "url" {

View File

@@ -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
}