Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed Feb 28, 2020
1 parent 73c3991 commit ce39045
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 80 deletions.
11 changes: 0 additions & 11 deletions data/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var foreignKey bool
var funcKey bool
var ui bool
var urlTag string
var tableList string
var outFileName string

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -75,9 +74,6 @@ 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 @@ -119,13 +115,6 @@ 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"
Expand Down
37 changes: 14 additions & 23 deletions data/config/MyIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 数据库信息
Expand Down Expand Up @@ -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
}
Expand Down
49 changes: 41 additions & 8 deletions data/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"fmt"
"io/ioutil"
"os"
"path"

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

Expand Down Expand Up @@ -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
}
46 changes: 39 additions & 7 deletions data/dlg/cui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ 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 @@ -77,7 +79,7 @@ func mainLayout(g *gocui.Gui) error {
// }
}

if v, err := g.SetView(_viewDefine, division(maxX, uiPart[0]), 1, maxX-1, maxY-1); err != nil {
if v, err := g.SetView(_viewDefine, division(maxX, uiPart[0]), 1, maxX-1, maxY-3); err != nil {
if err != gocui.ErrUnknownView {
return err
}
Expand All @@ -103,6 +105,11 @@ 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 @@ -163,6 +170,24 @@ 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 @@ -244,12 +269,14 @@ 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")
// modal.SetBgColor(gocui.ColorRed)
modal.AddButton("ok", "OK", gocui.KeyEnter, func(g *gocui.Gui, v *gocui.View) error {

_handle := 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 @@ -276,11 +303,12 @@ func buttonSave(g *gocui.Gui, v *gocui.View) error {

config.SaveToFile()
modal := mycui.NewModal(g, division(maxX, uiPart[0])+5, 10, division(maxX, uiPart[0])+35).SetText("save success")
modal.AddButton("ok", "OK", gocui.KeyEnter, func(g *gocui.Gui, v *gocui.View) error {
_handle := 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 @@ -309,10 +337,11 @@ func showStruct(g *gocui.Gui, v *gocui.View) error {
l = ""
}

var out []string
var out, out1 []string
for _, v := range gPkg.Structs {
if v.Name == l {
out = v.GeneratesColor()
out1 = v.Generates()
break
}
}
Expand All @@ -321,6 +350,9 @@ 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
16 changes: 10 additions & 6 deletions data/dlg/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
)

const (
_menuDefine = "menu"
_listDefine = "list"
_viewDefine = "view"
_run = "run"
_set = "set"
_menuDefine = "menu"
_listDefine = "list"
_viewDefine = "view"
_run = "run"
_set = "set"
_clipboardBtn = "clipboardBtn"
)

var (
uiPart = []float32{4, 3} // x,y 对应列表
uiPart = []float32{4, 5} // x,y 对应列表
mainViewArr = []string{_menuDefine, _listDefine, _viewDefine} // 主菜单列表
mainIndex = 0

Expand All @@ -33,6 +34,9 @@ type listDetails struct {
btnList []*mycui.Button
}

var clipboardBtn *mycui.Button
var copyInfo string

var menuDlg *menuDetails
var form *mycui.Form
var gPkg genstruct.GenPackage
6 changes: 6 additions & 0 deletions data/dlg/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func addChinese() error {
}, &i18n.Message{
ID: "set",
Other: "设 置 🛠 ",
}, &i18n.Message{
ID: "clipboardBtn",
Other: "复 制 到 剪 切 板 ",
}, &i18n.Message{
ID: "out_dir",
Other: " 输 出 目 录 :",
Expand Down Expand Up @@ -138,6 +141,9 @@ func addEnglish() error {
}, &i18n.Message{
ID: "set",
Other: "Set 🛠 ",
}, &i18n.Message{
ID: "clipboardBtn",
Other: "Copy to clipboard",
}, &i18n.Message{
ID: "out_dir",
Other: "out dir:",
Expand Down
3 changes: 1 addition & 2 deletions data/view/cnf/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ var EImportsHead = map[string]string{

// TypeMysqlDicMp Accurate matching type.精确匹配类型
var TypeMysqlDicMp = map[string]string{
"tinyint": "int8",
"tinyint unsigned": "uint8",
"smallint": "int16",
"smallint unsigned": "uint16",
"int": "int",
Expand All @@ -24,6 +22,7 @@ var TypeMysqlDicMp = map[string]string{
"datetime": "time.Time",
"bit(1)": "int8",
"tinyint": "int8",
"tinyint unsigned": "uint8",
"tinyint(1)": "int8",
"tinyint(1) unsigned": "int8",
"json": "string",
Expand Down
12 changes: 6 additions & 6 deletions data/view/genstruct/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ func (s *GenStruct) GeneratesColor() []string {
}
p.Add("\033[32;1m " + s.Notes + " \033[0m")
p.Add("\033[34;1m type \033[0m", s.Name, "\033[34;1m struct \033[0m {")
mp := make(map[string]bool, len(s.Em))
for _, v := range s.Em {
p.Add(" \t\t" + v.GenerateColor())
if !mp[v.Name] {
mp[v.Name] = true
p.Add(" \t\t" + v.GenerateColor())
}
}
p.Add(" }")

Expand Down Expand Up @@ -231,12 +235,8 @@ func (p *GenPackage) Generate() string {
for _, v1 := range v.Generates() {
pa.Add(v1)
}
}
// -----------end

// add table name func
if config.GetIsTableName() {
for _, v := range p.Structs {
if config.GetIsTableName() { // add table name func
for _, v1 := range v.GenerateTableName() {
pa.Add(v1)
}
Expand Down
Loading

0 comments on commit ce39045

Please sign in to comment.