Skip to content

Commit

Permalink
增加新特性,并修复一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LLLLancelot committed Feb 27, 2020
1 parent 2bd6daa commit 9e60d56
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 131 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ is_out_func : true # Whether to output function
is_url_tag : true # Whether to mark web or not
is_foreign_key : true # Whether to mark foreign key or not
is_gui : false # Whether to operate on gui
is_table_name : false # Whether to out GetTableName function
mysql_info :
host : "127.0.0.1"
Expand Down Expand Up @@ -198,5 +199,3 @@ go generate
## Stargazers over time

[![Stargazers over time](https://starchart.cc/xxjwxc/gormt.svg)](https://starchart.cc/xxjwxc/gormt)

## 20200228 llllancelot foked
1 change: 1 addition & 0 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ is_out_func : true # 是否输出 快捷函数
is_url_tag : true # 是否打web标记
is_foreign_key : true # 是否导出外键关联
is_gui : false # 是否ui模式显示
is_table_name : false # 是否直接生成表名函数
mysql_info:
host : 127.0.0.1
port : 3306
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ is_out_func : true # 是否输出 快捷函数
is_url_tag : true # 是否打web标记
is_foreign_key : true # 是否导出外键关联
is_gui : false # 是否ui模式显示
is_table_name : false # 是否直接生成表名函数
mysql_info:
host : 127.0.0.1
port : 3306
Expand Down
21 changes: 21 additions & 0 deletions data/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/xxjwxc/public/tools"

Expand All @@ -21,6 +22,8 @@ var foreignKey bool
var funcKey bool
var ui bool
var urlTag string
var tableList string
var outFileName string

var rootCmd = &cobra.Command{
Use: "main",
Expand Down Expand Up @@ -72,6 +75,11 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&urlTag, "url", "l", "", "url标签(json,url)")
rootCmd.MarkFlagRequired("url tag")

rootCmd.PersistentFlags().StringVarP(&tableList, "tablelist", "t", "", "目标table列表,以','隔开")
rootCmd.MarkFlagRequired("table list")

rootCmd.Flags().StringVar(&outFileName, "outfilename", "", "输出文件名,默认以数据库名称命名")

rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号")
}

Expand Down Expand Up @@ -111,6 +119,19 @@ func MergeMysqlDbInfo() {
if len(urlTag) > 0 {
config.SetURLTag(urlTag)
}
if len(tableList) > 0 {
m := make(map[string]struct{})
for _, v := range strings.Split(tableList, ",") {
m[v] = struct{}{}
}
config.SetTableList(m)
}
if len(outFileName) > 0 {
if !strings.HasSuffix(outFileName, ".go") {
outFileName += ".go"
}
config.SetOutFileName(outFileName)
}

config.SetMysqlDbInfo(&tmp)

Expand Down
53 changes: 41 additions & 12 deletions data/config/MyIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 数据库信息
Expand Down Expand Up @@ -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" {
Expand Down
49 changes: 8 additions & 41 deletions data/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package config
import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/xxjwxc/public/dev"
"github.com/xxjwxc/public/tools"
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
49 changes: 10 additions & 39 deletions data/dlg/cui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"fmt"
"log"
"strconv"
"strings"

"github.com/xxjwxc/public/tools"

"github.com/xxjwxc/gormt/data/config"

"github.com/jroimartin/gocui"
"github.com/xxjwxc/public/myclipboard"
"github.com/xxjwxc/public/mycui"
)

Expand Down Expand Up @@ -79,7 +77,7 @@ func mainLayout(g *gocui.Gui) error {
// }
}

if v, err := g.SetView(_viewDefine, division(maxX, uiPart[0]), 1, maxX-1, maxY-3); err != nil {
if v, err := g.SetView(_viewDefine, division(maxX, uiPart[0]), 1, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
Expand All @@ -105,11 +103,6 @@ func nemuLayOut(g *gocui.Gui) {
AddHandler(gocui.KeyArrowUp, menuDlg.prevButton).AddHandler(gocui.KeyArrowDown, menuDlg.nextButton).
AddHandler(gocui.KeyEnter, enterSet).AddHandler(gocui.MouseLeft, enterSet))

maxX, maxY := g.Size() // division(maxY, uiPart[1])
clipboardBtn = mycui.NewButton(g, _clipboardBtn, SLocalize(_clipboardBtn), division(maxX, uiPart[0])+2, maxY-3, 5).
AddHandler(gocui.KeyEnter, enterClipboard).AddHandler(gocui.MouseLeft, enterClipboard)
clipboardBtn.Draw()

menuDlg.Draw()
menuFocusButton(g)
}
Expand Down Expand Up @@ -170,24 +163,6 @@ func addlog(g *gocui.Gui, str string) error {
return err
}

func enterClipboard(g *gocui.Gui, v *gocui.View) error {
myclipboard.Set(copyInfo)

maxX, _ := g.Size()
modal := mycui.NewModal(g, division(maxX, uiPart[0])+5, 10, division(maxX, uiPart[0])+35).
SetTextColor(gocui.ColorRed).SetText("copy success \n 已 复 制 到 剪 切 板 ")
modal.Mouse = true
// modal.SetBgColor(gocui.ColorRed)
_handle := func(g *gocui.Gui, v *gocui.View) error {
modal.Close()
return nil
}
modal.AddButton("ok", "OK", gocui.KeyEnter, _handle).AddHandler(gocui.MouseLeft, _handle)
modal.Draw()

return nil
}

func enterRun(g *gocui.Gui, v *gocui.View) error {
setlog(g, "run .... ing")
generate(g, v)
Expand Down Expand Up @@ -230,6 +205,8 @@ func enterSet(g *gocui.Gui, v *gocui.View) error {
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsForeignKey())))
form.AddSelect("is_gui", SLocalize("is_gui"), formPart[0], formPart[2]).
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsGUI())))
form.AddSelect("is_table_name", SLocalize("is_table_name"), formPart[0], formPart[2]).
AddOptions(SLocalize("true"), SLocalize("false")).SetSelected(SLocalize(tools.AsString(config.GetIsTableName())))
form.AddSelect("url_tag", SLocalize("url_tag"), formPart[0], formPart[2]).
AddOptions("json", "url").SetSelected(tools.AsString(config.GetURLTag()))
form.AddSelect("db_tag", SLocalize("db_tag"), formPart[0], formPart[2]).
Expand Down Expand Up @@ -267,14 +244,12 @@ func buttonSave(g *gocui.Gui, v *gocui.View) error {
port, err := strconv.Atoi(mp["db_port"])
if err != nil {
modal := mycui.NewModal(g, division(maxX, uiPart[0])+5, 10, division(maxX, uiPart[0])+35).SetTextColor(gocui.ColorRed).SetText("port error")

_handle := func(g *gocui.Gui, v *gocui.View) error {
// modal.SetBgColor(gocui.ColorRed)
modal.AddButton("ok", "OK", gocui.KeyEnter, func(g *gocui.Gui, v *gocui.View) error {
modal.Close()
form.SetCurrentItem(form.GetCurrentItem())
return nil
}
// modal.SetBgColor(gocui.ColorRed)
modal.AddButton("ok", "OK", gocui.KeyEnter, _handle).AddHandler(gocui.MouseLeft, _handle)
})

modal.Draw()
return nil
Expand All @@ -294,18 +269,18 @@ func buttonSave(g *gocui.Gui, v *gocui.View) error {
config.SetIsOutFunc(getBool(mp["is_out_func"]))
config.SetForeignKey(getBool(mp["is_foreign_key"]))
config.SetIsGUI(getBool(mp["is_gui"]))
config.SetIsTableName(getBool(mp["is_table_name"]))
config.SetURLTag(mp["url_tag"])
config.SetDBTag(mp["db_tag"])
config.SetLG(mp["language"])

config.SaveToFile()
modal := mycui.NewModal(g, division(maxX, uiPart[0])+5, 10, division(maxX, uiPart[0])+35).SetText("save success")
_handle := func(g *gocui.Gui, v *gocui.View) error {
modal.AddButton("ok", "OK", gocui.KeyEnter, func(g *gocui.Gui, v *gocui.View) error {
modal.Close()
buttonCancel(g, v)
return nil
}
modal.AddButton("ok", "OK", gocui.KeyEnter, _handle).AddHandler(gocui.MouseLeft, _handle)
})
modal.Draw()

return nil
Expand Down Expand Up @@ -334,11 +309,10 @@ func showStruct(g *gocui.Gui, v *gocui.View) error {
l = ""
}

var out, out1 []string
var out []string
for _, v := range gPkg.Structs {
if v.Name == l {
out = v.GeneratesColor()
out1 = v.Generates()
break
}
}
Expand All @@ -347,9 +321,6 @@ func showStruct(g *gocui.Gui, v *gocui.View) error {
for _, v := range out {
addlog(g, v)
}

copyInfo = strings.Join(out1, "\n")

return nil
}

Expand Down
Loading

0 comments on commit 9e60d56

Please sign in to comment.