-
Notifications
You must be signed in to change notification settings - Fork 1
/
geek.go
580 lines (504 loc) · 15.9 KB
/
geek.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package main
import (
"bytes"
"database/sql"
"errors"
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"log"
"os"
"os/exec"
"sort"
"strings"
"unicode"
_ "github.com/go-sql-driver/mysql"
)
var config Configuration
type Configuration struct {
AppKind string `json:"app_kind"`
AppPath string `json:"go_path"`
TemplatePath string `json:"template_path"`
DbAddress string `json:"db_address"`
DbName string `json:"db_name"`
AppName string `json:"app_name"`
// TagLabel produces tags commonly used to match db field names with Go struct members
TagLabel string `json:"tag_label"`
}
type ColumnSchema struct {
TableName string
ColumnName string
IsNullable string
DataType string
CharacterMaximumLength sql.NullInt64
NumericPrecision sql.NullInt64
NumericScale sql.NullInt64
ColumnType string
ColumnKey string
}
//生成目录
func initDirs() {
os.MkdirAll(config.AppPath+config.AppName+"/", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/dao", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/cache", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/"+"handler", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/"+"service", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/"+"setup", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/"+"redis", 0755)
os.MkdirAll(config.AppPath+config.AppName+"/"+"e", 0755)
}
func writeStructs(tables map[string][]*ColumnSchema) error {
var cacheParam = ""
var handlerParam = ""
// To store the keys in slice in sorted order
var keys []string
for k := range tables {
keys = append(keys, k)
}
//按表名排序
sort.Strings(keys)
fmt.Println(keys)
for _, tableName := range keys {
var columns = tables[tableName]
//按表生成结构体
generateModel(tableName, columns)
//生成dao CRUD
generateCacheCRUD(tableName)
//生成Handler
generateHandler(tableName)
//生成Service
generateService(tableName)
ftn := formatName(tableName)
ctn := getVarPrefix(ftn)
cacheParam += "\n\"" + ctn + "\":" + ctn + "CacheInit,"
handlerParam += "\n" + getDeclarePrefix(ftn) + "Init(r)"
}
//生成CaChe Init
generateCacheInit(cacheParam)
//生成Handler Init
generateHandlerInit(handlerParam)
return nil
}
//反射表结构
func getSchema(dbaddr, dbname string) map[string][]*ColumnSchema {
conn, err := sql.Open("mysql", dbaddr+"/information_schema")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
q := "SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE, " +
"CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLUMN_TYPE, " +
"COLUMN_KEY FROM COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION"
rows, err := conn.Query(q, dbname)
if err != nil {
log.Fatal(err)
}
tables := make(map[string][]*ColumnSchema)
for rows.Next() {
cs := ColumnSchema{}
err := rows.Scan(&cs.TableName, &cs.ColumnName, &cs.IsNullable, &cs.DataType,
&cs.CharacterMaximumLength, &cs.NumericPrecision, &cs.NumericScale,
&cs.ColumnType, &cs.ColumnKey)
if err != nil {
log.Fatal(err)
}
if _, ok := tables[cs.TableName]; !ok {
tables[cs.TableName] = make([]*ColumnSchema, 0)
}
tables[cs.TableName] = append(tables[cs.TableName], &cs)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return tables
}
func formatName(name string) string {
parts := strings.Split(name, "_")
newName := ""
for _, p := range parts {
if len(p) < 1 {
continue
}
newName = newName + strings.Replace(p, string(p[0]), strings.ToUpper(string(p[0])), 1)
}
return newName
}
func getVarPrefix(tableName string) string {
r := []rune(tableName)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func getDeclarePrefix(tableName string) string {
r1 := []rune(tableName)
r1[0] = unicode.ToUpper(r1[0])
return string(r1)
}
//生成cache Init 列表
func generateCacheInit(cacheParam string) {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/cache.tpl")
if err != nil {
fmt.Println(err)
}
b0 := strings.Replace(string(b), "{namelist}", cacheParam, -1)
b1 := strings.Replace(string(b0), "{package}", "cache", -1)
os.Mkdir(config.AppPath+config.AppName+"/cache/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/cache/cache.go", []byte(b1), os.ModeAppend|os.FileMode(0664))
}
//生成handler Init 列表
func generateHandlerInit(handlerParam string) {
var err error
var b []byte
if config.AppKind == "gin" {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/handlerInit.tpl")
if err != nil {
fmt.Println(err)
return
}
} else {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/iris_handlerInit.tpl")
if err != nil {
fmt.Println(err)
return
}
}
b0 := strings.Replace(string(b), "{namelist}", handlerParam, -1)
b1 := strings.Replace(string(b0), "{package}", "cache", -1)
os.Mkdir(config.AppPath+config.AppName+"/cache/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/"+"handler"+"/handler.go", []byte(b1), os.ModeAppend|os.FileMode(0664))
}
//生成cache CRUD
func generateCacheCRUD(tableName string) {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/cache_template.tpl")
if err != nil {
fmt.Println(err)
}
ftn := formatName(tableName)
ctn := getVarPrefix(ftn)
b0 := strings.Replace(string(b), "{package}", "cache", -1)
b1 := strings.Replace(b0, "{table}", ctn, -1)
b2 := strings.Replace(b1, "{Table}", ftn, -1)
b3 := strings.Replace(b2, "{AppName}", config.AppName, -1)
os.Mkdir(config.AppPath+config.AppName+"/cache/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/cache/"+tableName+".go", []byte(b3), os.ModeAppend|os.FileMode(0664))
}
//生成handler
func generateHandler(tableName string) {
var err error
var b []byte
if config.AppKind == "gin" {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/handler_template.tpl")
if err != nil {
fmt.Println(err)
return
}
} else {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/iris_handler_template.tpl")
if err != nil {
fmt.Println(err)
return
}
}
ftn := formatName(tableName)
ctn := getVarPrefix(ftn)
b0 := strings.Replace(string(b), "{package}", "handler", -1)
b1 := strings.Replace(b0, "{table}", ctn, -1)
b2 := strings.Replace(b1, "{Table}", ftn, -1)
b3 := strings.Replace(b2, "{AppName}", config.AppName, -1)
os.Mkdir(config.AppPath+config.AppName+"/"+"handler/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/"+"handler"+"/"+tableName+".go", []byte(b3), os.ModeAppend|os.FileMode(0664))
}
//生成service
func generateService(tableName string) {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/service_template.tpl")
if err != nil {
fmt.Println(err)
}
ftn := formatName(tableName)
ctn := getVarPrefix(ftn)
b0 := strings.Replace(string(b), "{package}", "service", -1)
b1 := strings.Replace(b0, "{table}", ctn, -1)
b2 := strings.Replace(b1, "{Table}", ftn, -1)
b3 := strings.Replace(b2, "{AppName}", config.AppName, -1)
os.Mkdir(config.AppPath+config.AppName+"/"+"service/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/"+"service"+"/"+tableName+".go", []byte(b3), os.ModeAppend|os.FileMode(0664))
}
//生成database
func generateDatabase() {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/database.tpl")
if err != nil {
log.Fatal(err)
}
b0 := strings.Replace(string(b), "{package}", "dao", -1)
ioutil.WriteFile(config.AppPath+config.AppName+"/dao/database.go", []byte(b0), os.FileMode(0644))
}
//生成redis
func generateRedis() {
//path := strings.ToLower(config.RedisPkgName)
b, err := ioutil.ReadFile(config.TemplatePath + "/template/redis.tpl")
if err != nil {
log.Fatal(err)
}
b0 := strings.Replace(string(b), "{package}", "redis", -1)
ioutil.WriteFile(config.AppPath+config.AppName+"/redis/redis.go", []byte(b0), os.FileMode(0644))
}
//生成setup
func generateSetUp() {
var err error
var b []byte
if config.AppKind == "gin" {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/setup.tpl")
if err != nil {
fmt.Println(err)
return
}
} else {
b, err = ioutil.ReadFile(config.TemplatePath + "/template/iris_setup.tpl")
if err != nil {
fmt.Println(err)
return
}
}
b0 := strings.Replace(string(b), "{package}", "setup", -1)
b1 := strings.Replace(string(b0), "{AppName}", config.AppName, -1)
ioutil.WriteFile(config.AppPath+config.AppName+"/setup/setup.go", []byte(b1), os.FileMode(0644))
}
//生成errcode
func generateErrorCode() {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/e.tpl")
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(config.AppPath+config.AppName+"/e/e.go", []byte(b), os.FileMode(0644))
}
//生成config.toml
func generateConfigExample() {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/config.tpl")
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(config.AppPath+config.AppName+"/config.toml", []byte(b), os.FileMode(0644))
}
//生成main
func generateMain() {
b, err := ioutil.ReadFile(config.TemplatePath + "/template/main.tpl")
if err != nil {
log.Fatal(err)
}
b1 := strings.Replace(string(b), "{package}", "main", -1)
b2 := strings.Replace(string(b1), "{AppName}", config.AppName, -1)
ioutil.WriteFile(config.AppPath+config.AppName+"/main.go", []byte(b2), os.FileMode(0644))
}
//生成 dao.Model
func generateModel(tableName string, columns []*ColumnSchema) {
var out bytes.Buffer
// generate model
ftn := formatName(tableName)
ctn := getVarPrefix(ftn)
out.WriteString("type ")
out.WriteString(ftn)
out.WriteString(" struct{\n")
for _, column := range columns {
//下划线转驼峰
fcn := formatName(column.ColumnName)
ccn := getVarPrefix(fcn)
goType, _, err := goType(column)
if err != nil {
log.Fatal(err)
}
out.WriteString("\t")
out.WriteString(fcn)
out.WriteString(" ")
out.WriteString(goType)
if len(config.TagLabel) > 0 || goType == "*utils.JSONTime" {
out.WriteString("\t`")
}
if len(config.TagLabel) > 0 {
if goType == "*utils.JSONTime" {
out.WriteString(config.TagLabel)
out.WriteString(":\"-\" ")
} else {
out.WriteString(config.TagLabel)
out.WriteString(":\"")
out.WriteString(ccn)
out.WriteString("\"")
}
}
if goType == "*utils.JSONTime" {
out.WriteString("sql:\"-\"")
}
out.WriteString("`\n")
}
out.WriteString("}")
b, err := ioutil.ReadFile(config.TemplatePath + "/template/model_template.tpl")
if err != nil {
fmt.Println(err)
}
bb := strings.Replace(string(b), "{model}", out.String(), -1)
b0 := strings.Replace(bb, "{package}", "dao", -1)
b1 := strings.Replace(b0, "{table}", ctn, -1)
b2 := strings.Replace(b1, "{Table}", ftn, -1)
b3 := strings.Replace(b2, "{snake}", tableName, -1)
os.Mkdir(config.AppPath+config.AppName+"/dao/", os.FileMode(0755))
ioutil.WriteFile(config.AppPath+config.AppName+"/dao/"+tableName+".go", []byte(b3), os.ModeAppend|os.FileMode(0664))
}
//数据库字段类型转go类型
func goType(col *ColumnSchema) (string, string, error) {
requiredImport := ""
// if col.IsNullable == "YES" {
// requiredImport = "db/sql"
// }
var gt string = ""
switch col.DataType {
case "char", "varchar", "enum", "text", "longtext", "mediumtext", "tinytext":
// if col.IsNullable == "YES" {
// gt = "sql.NullString"
// } else {
gt = "string"
// }
case "blob", "mediumblob", "longblob", "varbinary", "binary":
gt = "[]byte"
case "date", "time", "datetime", "timestamp":
// gt, requiredImport = "time.Time", "time"
gt, requiredImport = "*utils.JSONTime", "github.com/tx991020/utils"
case "smallint", "int", "mediumint", "bigint":
// if col.IsNullable == "YES" {
// gt = "sql.NullInt64"
// } else {
gt = "int64"
// }
case "float", "decimal", "double":
// if col.IsNullable == "YES" {
// gt = "sql.NullFloat64"
// } else {
gt = "float64"
// }
case "tinyint":
gt = "bool"
}
if gt == "" {
n := col.TableName + "." + col.ColumnName
return "", "", errors.New("No compatible datatype (" + col.DataType + ") for " + n + " found")
}
return gt, requiredImport, nil
}
func Create(c *cli.Context) error {
fmt.Println(111, c.Args())
if c.NArg() != 4 {
err := errors.New(`参数格式不对,例如 gin new go-api "root:123456@tcp(127.0.0.1:3306)" dbname `)
log.Fatal(err)
return err
}
AppKind := c.Args()[0]
fmt.Println(1111111, AppKind == "iris")
AppName := c.Args()[1]
DbAddress := c.Args()[2]
DbName := c.Args()[3]
fmt.Println(AppName, DbAddress, DbName)
var cmd *exec.Cmd
//获取本机goenv
cmd = exec.Command("/bin/sh", "-c", `go env | grep GOPATH | awk -F '"' '{print $2}'`)
path, err := cmd.Output()
if err != nil {
err := errors.New("未找到 GOPATH , 请确定已设置GOPATH")
log.Fatal(err)
return err
}
AppPath := strings.TrimSpace(string(path)) + "/src/"
TemplatePath := strings.TrimSpace(string(path)) + "/src/github.com/tx991020/geek/"
config = Configuration{AppKind, AppPath, TemplatePath, DbAddress, DbName, AppName, "json"}
tables := getSchema(config.DbAddress, config.DbName)
//生成目录
initDirs()
//生成databse配置
generateDatabase()
//生成databse配置
generateRedis()
generateSetUp()
generateErrorCode()
generateConfigExample()
generateMain()
//结构体
err1 := writeStructs(tables)
if err1 != nil {
log.Fatal(err1)
return errors.New(err1.Error())
}
//cmd = exec.Command( "/bin/sh", "-c","swag init")
//out, err2 := cmd.Output()
//if err2 != nil {
// fmt.Println(err2.Error())
//
// return err2
//}
//fmt.Println(strings.TrimSpace(string(out)))
fmt.Println("Greate ! Success!")
return nil
}
//适用于新增单表 dao,service,handler, table_name 要用数据库里表名 大小写完全一致
func GenerateSingleTable(table_name string) error {
tables := getSchema(config.DbAddress, config.DbName)
fmt.Println(tables)
columns := tables[table_name]
//按表生成结构体
generateModel(table_name, columns)
//生成dao CRUD
generateCacheCRUD(table_name)
//生成Handler
generateHandler(table_name)
//生成Service
generateService(table_name)
return nil
}
func GetPath(c *cli.Context) error {
path, _ := os.Getwd()
fmt.Println(path)
return nil
}
//生成整个项目
func CLI() {
app := cli.NewApp()
app.Name = "is a Fast and Flexible tool for managing your gin restful API Application"
app.Version = "1.0"
var createCommand = cli.Command{
Name: "new",
Usage: `geek new iris go-api "root:123456@tcp(127.0.0.1:3306)" dbname`,
ArgsUsage: "generate a gin/iris api application 第一个参数:项目Kind:iris或gin(default gin),第二个参数:项目名 第三个参数: 数据库连接 第四个参数 数据库名",
Action: Create,
}
var getEnvCommand = cli.Command{
Name: "path",
Usage: `get local path`,
ArgsUsage: "获取当前路径",
Action: GetPath,
}
app.Commands = []cli.Command{
createCommand,
getEnvCommand,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func main() {
//dbUser := flag.String("dbuser", "root", "db user name")
//dbPassword := flag.String("dbpassword", "password", "password for user name")
//dbAddress := flag.String("dbaddress", "127.0.0.1:3306", "db address")
//dbName := flag.String("dbname", "dbname", "db name")
//daoPkgPath := flag.String("daopkgpath", "dao package path", "dao package path")
//daoPkgName := flag.String("daopkgname", "dao package name", "dao package name")
//cachePkgName := flag.String("cachepkgname", "cache package name", "cache package name")
//tagLabel := flag.String("taglabel", "json", "json or xml")
//flag.Parse()
//
//config.DbUser = *dbUser
//config.DbPassword = *dbPassword
//config.DbAddress = *dbAddress
//config.DbName = *dbName
//config.DaoPkgPath = *daoPkgPath
//config.DaoPkgName = *daoPkgName
//config.CachePkgName = *cachePkgName
//config.TagLabel = *tagLabel
config = Configuration{"iris", "/Users/andy/GoLang/src/", "/Users/andy/GoLang/src/github.com/tx991020/geek/", "root:123456@tcp(127.0.0.1:3306)", "api", "seed7", "json"}
GenerateSingleTable("task")
}