forked from domego/gopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (192 loc) · 4.36 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/domego/gokits/log"
"github.com/domego/gopt/gens/common"
)
const (
exec = "gopt"
version = "1.0.0"
)
var (
command string
rootPath string
)
var (
appName string
appPort int
typesFile string
errorsFile string
ormFile string
apiFile string
template string
)
var commandMap map[string]*Command
type CommandFuncType func(name, desc string)
type Command struct {
Name string
Desc string
Func CommandFuncType
}
func init() {
genutils.Asset = Asset
// 初始化参数值
flag.StringVar(&errorsFile, "errors", "errors.yaml", "errors config file")
flag.StringVar(&typesFile, "types", "types.yaml", "types config file")
flag.StringVar(&ormFile, "orm", "db.yaml", "database config file")
flag.StringVar(&apiFile, "api", "api.yaml", "gin controller config file")
flag.StringVar(&appName, "name", "", "gin app name")
flag.StringVar(&template, "template", "vue", "api.js template")
flag.IntVar(&appPort, "port", 0, "gin server port")
}
func showHelp(name, desc string) {
commands := make([]string, 0, len(commandMap))
for _, v := range commandMap {
commands = append(commands, fmt.Sprintf("%s\t%s", v.Name, v.Desc))
}
printHelp(fmt.Sprintf("Usage: %s <command> [options]", exec), commands,
[]string{
"-errors\terrors config file, default: errors.yaml",
"-types\ttypes config file, default: types.yaml",
"-orm\tdatabase config file, default: db.yaml",
"-api\tgin controller config file, default: api.yaml",
"-template\tapi.js template, default: vue",
}, nil)
}
func initCommands() {
for i, v := range os.Args {
switch i {
case 1:
command = v
}
}
// 初始化命令列表
commandMap = map[string]*Command{
"version": &Command{
Name: "version",
Desc: "show version",
Func: showVersion,
},
"help": &Command{
Name: "help",
Desc: "show help",
Func: showHelp,
},
"gen_types": &Command{
Name: "gen_types",
Desc: "generate structs code from yaml config file",
Func: genTypes,
},
"gen_errors": &Command{
Name: "gen_errors",
Desc: "generate structs code from yaml config file",
Func: genErrors,
},
"gen_gin_server": &Command{
Name: "gen_gin_server",
Desc: "generate gin server code from template",
Func: genGinServer,
},
"gen_orm": &Command{
Name: "gen_orm",
Desc: "generate database orm code from template",
Func: genORM,
},
"gen_gin_api": &Command{
Name: "gen_gin_api",
Desc: "generate gin controller code from template",
Func: genGinController,
},
"gen_js_api": &Command{
Name: "gen_js_api",
Desc: "generate api.js code from template",
Func: genJavascriptAPI,
},
"gen_api_doc": &Command{
Name: "gen_api_doc",
Desc: "generate api_doc document from template",
Func: genAPIDoc,
},
}
}
func checkArgs() bool {
if command == "" {
showHelp("help", commandMap["help"].Desc)
return false
} else if command == "gen_gin_server" {
if appName == "" {
log.Fatalf("name can not be empty")
}
if appPort <= 0 {
log.Fatalf("port can not be empty")
}
}
return true
}
func printHelp(usage string, commands, options, examples []string) {
fmt.Println()
fmt.Println(usage)
if len(commands) > 0 {
fmt.Println("\nCommands:\n")
for _, s := range commands {
fmt.Println(fmt.Sprintf("\t%s", s))
}
}
if len(options) > 0 {
fmt.Println("\nOptions:\n")
for _, s := range options {
fmt.Println(fmt.Sprintf("\t%s", s))
}
}
if len(examples) > 0 {
fmt.Println("\nExamples:\n")
for _, s := range examples {
fmt.Println(fmt.Sprintf("\t%s", s))
}
}
fmt.Println()
}
func showVersion(name, desc string) {
fmt.Println(version)
}
func parseRootDir() {
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("%s", err)
}
pathList := strings.Split(os.Getenv("GOPATH"), ":")
for _, path := range pathList {
if strings.HasPrefix(currentDir, path) {
var prefix string
if strings.HasSuffix(path, "/") {
prefix = path + "src/"
} else {
prefix = path + "/src/"
}
currentDir = currentDir[len(prefix):]
}
rootPath = currentDir
}
}
func main() {
parseRootDir()
initCommands()
if len(os.Args) < 2 {
showHelp("help", commandMap["help"].Desc)
return
}
flag.CommandLine.Parse(os.Args[2:])
if !checkArgs() {
return
}
c := commandMap[command]
if c == nil {
showHelp("help", commandMap["help"].Desc)
return
} else {
c.Func(c.Name, c.Desc)
}
}