forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
209 lines (165 loc) · 5.38 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/chenhg5/go-admin/modules/connections/mysql"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
)
var (
rootPath string
outputPath string
port string
user string
password string
host string
name string
packageName string
)
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "compile":
if len(os.Args) > 2 {
compileFlag := flag.NewFlagSet(os.Args[1], flag.ExitOnError)
compileFlag.StringVar(&rootPath, "path", "./template/adminlte/resource/pages/", "compile root path")
compileFlag.StringVar(&outputPath, "path", "./template/adminlte/tmpl/template.go", "compile output path")
compileFlag.Parse(os.Args[2:])
} else {
rootPath = "./template/adminlte/resource/pages/"
outputPath = "./template/adminlte/tmpl/template.go"
}
CompileTmpl()
case "generate":
if len(os.Args) < 2 {
panic("need options")
}
generateFlag := flag.NewFlagSet(os.Args[1], flag.ExitOnError)
generateFlag.StringVar(&port, "p", "3306", "database port")
generateFlag.StringVar(&user, "u", "root", "database user")
generateFlag.StringVar(&password, "P", "root", "database password")
generateFlag.StringVar(&host, "h", "127.0.0.1", "database host")
generateFlag.StringVar(&outputPath, "o", ".", "database output path")
generateFlag.StringVar(&name, "n", "", "database name")
generateFlag.StringVar(&packageName, "Pa", "main", "package name")
generateFlag.Parse(os.Args[2:])
// step 1. test connection
db, err := sql.Open("mysql", user+
":"+password+"@tcp("+host+":"+port+")/"+name+"?charset=utf8mb4")
defer db.Close()
if err != nil {
panic(err)
}
sqlDB := &mysql.Mysql{
SqlDBmap: map[string]*sql.DB{
"default": db,
},
}
// step 2. show tables
tables, _ := sqlDB.Query("show tables")
key := "Tables_in_" + name
// step 3. show columns
// step 4. generate file
for i := 0; i < len(tables); i++ {
if tables[i][key].(string) != "goadmin_menu" && tables[i][key].(string) != "goadmin_operation_log" &&
tables[i][key].(string) != "goadmin_permissions" && tables[i][key].(string) != "goadmin_role_menu" &&
tables[i][key].(string) != "goadmin_roles" && tables[i][key].(string) != "goadmin_session" &&
tables[i][key].(string) != "goadmin_users" && tables[i][key].(string) != "goadmin_role_permissions" &&
tables[i][key].(string) != "goadmin_role_users" && tables[i][key].(string) != "goadmin_user_permissions" {
GenerateFile(tables[i][key].(string), sqlDB)
}
}
default:
panic("wrong option")
}
}
}
func CompileTmpl() {
content := `package tmpl
var List = map[string]string{`
content = GetContentFromDir(content, rootPath)
content += `}`
ioutil.WriteFile(outputPath, []byte(content), 0644)
}
func GetContentFromDir(content string, dirPath string) string {
files, _ := ioutil.ReadDir(dirPath)
for _, f := range files {
if f.IsDir() {
content = GetContentFromDir(content, dirPath+f.Name()+"/")
continue
}
b, err := ioutil.ReadFile(dirPath + f.Name())
if err != nil {
fmt.Print(err)
}
str := string(b)
suffix := path.Ext(f.Name())
onlyName := strings.TrimSuffix(f.Name(), suffix)
if suffix == ".tmpl" {
fmt.Println(dirPath + f.Name())
content += `"` + strings.Replace(dirPath, rootPath, "", -1) + onlyName + `":` + "`" + str + "`,"
}
}
return content
}
func GenerateFile(table string, db *mysql.Mysql) {
columnsModel, _ := db.Query("show columns in " + table)
content := `package ` + packageName + `
import (
"github.com/chenhg5/go-admin/template/types"
"github.com/chenhg5/go-admin/plugins/admin/models"
)
func Get` + strings.Title(table) + `Table() (` + table + `Table models.Table) {
` + table + `Table.Info.FieldList = []types.FieldStruct{`
for _, model := range columnsModel {
content += `{
Head: "` + strings.Title(model["Field"].(string)) + `",
Field: "` + model["Field"].(string) + `",
TypeName: "` + GetType(model["Type"].(string)) + `",
Sortable: false,
ExcuFun: func(model types.RowModel) interface{} {
return model.Value
},
},`
}
content += `}
` + table + `Table.Info.Table = "` + table + `"
` + table + `Table.Info.Title = "` + strings.Title(table) + `"
` + table + `Table.Info.Description = "` + strings.Title(table) + `"
` + table + `Table.Form.FormList = []types.FormStruct{`
for _, model := range columnsModel {
formType := "text"
if model["Field"].(string) == "id" {
formType = "default"
}
content += `{
Head: "` + strings.Title(model["Field"].(string)) + `",
Field: "` + model["Field"].(string) + `",
TypeName: "` + GetType(model["Type"].(string)) + `",
Default: "",
Editable: false,
FormType: "` + formType + `",
ExcuFun: func(model types.RowModel) interface{} {
return model.Value
},
},`
}
content += ` }
` + table + `Table.Form.Table = "` + table + `"
` + table + `Table.Form.Title = "` + strings.Title(table) + `"
` + table + `Table.Form.Description = "` + strings.Title(table) + `"
` + table + `Table.ConnectionDriver = "mysql"
return
}`
fmt.Println(outputPath + "/" + table + ".go")
ioutil.WriteFile(outputPath+table+".go", []byte(content), 0644)
}
func GetType(typeName string) string {
r, _ := regexp.Compile("\\(.*\\)")
typeName = r.ReplaceAllString(typeName, "")
return strings.ToLower(strings.Replace(typeName, " unsigned", "", -1))
}