-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_generate.go
141 lines (128 loc) · 4.03 KB
/
db_generate.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
package main
import(
"os"
)
var cmdGenerate = &Command{
UsageLine: "generate [Command]",
Short: "source code generator",
Long: `
revel_grom generate scaffold [scaffoldname] [-fields=""]
example: revel_grom generate scaffold post -fields="title:string,body:text"
revel_grom generate model [modelname] [-fields=""]
example: revel_grom generate model post -fields="title:string,body:text"
revel_grom generate controller [controllerfile]
generate RESTFul controllers
example: revel_grom generate controller post
`,
}
var fields flagValue
func init() {
cmdGenerate.Run = generateCode
cmdGenerate.Flag.Var(&fields, "fields", "specify the fields want to generate.")
}
func generateCode(cmd *Command, args []string){
// get current path
curpath, _ := os.Getwd()
// check args
if len(args) < 1 {
ColorLog("[ERRO] command is missing\n")
os.Exit(2)
}
// check $GOPATH
gopath := os.Getenv("GOPATH")
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
os.Exit(2)
}
gcmd := args[0]
switch gcmd {
case "rest-scaffold":
if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate scaffold [modelname] [-fields=\"\"]\n")
os.Exit(2)
}
cmd.Flag.Parse(args[2:])
if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate scaffold [modelname] [-fields=\"title:string,body:text\"]\n")
os.Exit(2)
}
sname := args[1]
ColorLog("[INFO] Using '%s' as controller name\n", sname)
ColorLog("[INFO] Using '%s' as controller name\n", sname + "Controller")
//generate model and controller
generateModel(sname, fields.String(), curpath)
generateRestController(sname, curpath)
case "rest-controller":
if len(args) == 2 {
cname := args[1]
generateRestController(cname, curpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate controller [controllername]\n")
os.Exit(2)
}
case "scaffold":
if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate scaffold [modelname] [-fields=\"\"]\n")
os.Exit(2)
}
cmd.Flag.Parse(args[2:])
if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate scaffold [modelname] [-fields=\"title:string,body:text\"]\n")
os.Exit(2)
}
sname := args[1]
ColorLog("[INFO] Using '%s' as controller name\n", sname)
ColorLog("[INFO] Using '%s' as controller name\n", sname + "Controller")
//generate model and controller
generateModel(sname, fields.String(), curpath)
generateController(sname, curpath)
generateViews(sname, fields.String(), curpath)
case "controller":
if len(args) == 2 {
cname := args[1]
generateController(cname, curpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate controller [controllername]\n")
os.Exit(2)
}
case "model":
if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate model [modelname] [-fields=\"\"]\n")
os.Exit(2)
}
cmd.Flag.Parse(args[2:])
if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate model [modelname] [-fields=title:string,body:string]\n")
os.Exit(2)
}
sname := args[1]
generateModel(sname, fields.String(), curpath)
case "views":
if len(args) < 2 {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate model [modelname] [-fields=\"\"]\n")
os.Exit(2)
}
cmd.Flag.Parse(args[2:])
if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: revel_mgo generate model [modelname] [-fields=title:string,body:string]\n")
os.Exit(2)
}
sname := args[1]
generateViews(sname, fields.String(), curpath)
default:
ColorLog("[ERRO] command is missing\n")
os.Exit(2)
}
ColorLog("[SUCC] generate successfully created!\n")
}