/
cmd_make.go
121 lines (98 loc) · 3.11 KB
/
cmd_make.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
package main
import (
"fmt"
"os"
"github.com/butbetter-id/dev/core"
"github.com/butbetter-id/dev/generate"
)
var makeCommand = &core.Command{
Name: "make",
Info: "source code generator",
Usage: `
dev make model [-tables=""] [-database=test] [-conn="root:@tcp(127.0.0.1:3306)"]
generate model source code from databases.
-tables: a list of table names separated by ',', default is empty, indicating all tables
-database: database name
-conn: the connection string used by the driver.
default for mysql: root:@tcp(127.0.0.1:3306)
dev make request [-name=test]
generate appcode based on an existing database
-name: request name
dev make handler [-methods=""]
generate appcode based on an existing database
-methods: [get|post|show|put|delete] a list of http method separated by ',', default is empty, indicating GET, POST, PUT, DELETE method.
`,
}
var name, database, conn, mode, tables, endpoint, methods core.DocVal
func init() {
makeCommand.Run = actionMake
makeCommand.Flag.Var(&database, "database", "specify the database want to use.")
makeCommand.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance.")
makeCommand.Flag.Var(&tables, "tables", "specify tables to generate datastore.")
makeCommand.Flag.Var(&endpoint, "endpoint", "specify endpoint name, will be used as folder name as well.")
makeCommand.Flag.Var(&methods, "methods", "specify HTTP method that will serve by the endpoint.")
makeCommand.Flag.Var(&name, "name", "specify project name.")
}
func actionMake(cmd *core.Command, args []string) int {
curpath, _ := os.Getwd()
if len(args) < 1 {
core.LogError("Command is missing.")
os.Exit(2)
}
core.LogInfo("")
core.LogInfo("Generating codes ...")
core.LogInfo("--------------------------------------")
called := args[0]
switch called {
case "request":
cmd.Flag.Parse(args[1:])
var tpl = &core.StubTemplate{
AppPath: curpath,
PackageName: core.GetDirName(curpath),
}
if name == "" {
core.LogError("name request must be specified.")
os.Exit(2)
}
core.LogInfo("Making a request file ...")
generate.FileRequest(name.String(), tpl)
case "model":
cmd.Flag.Parse(args[1:])
var tpl = &core.StubTemplate{
AppPath: curpath,
PackageName: core.GetDirName(curpath),
PackagePath: core.GetPackagePath(curpath),
}
c := sqlConnection()
core.LogInfo("Making a model file ...")
generate.FileModel("mysql", c, tables.String(), tpl)
case "handler":
cmd.Flag.Parse(args[1:])
var tpl = &core.StubTemplate{
AppPath: curpath,
PackageName: core.GetDirName(curpath),
}
if methods == "" {
methods = core.DocVal("get,post,put,delete,show")
}
core.LogInfo("Making a handler file ...")
generate.FileHandler(methods.String(), tpl)
default:
core.LogError("Command is missing.")
}
return 0
}
func sqlConnection() string {
if database == "" {
core.LogError("database name must be specified.")
os.Exit(2)
}
var c string
if conn == "" {
c = fmt.Sprint("root:@tcp(127.0.0.1:3306)/", database)
} else {
c = fmt.Sprint(conn, "/", database)
}
conn = core.DocVal(c)
return conn.String()
}