This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
376 lines (342 loc) · 7.2 KB
/
application.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
package console
import (
"errors"
"fmt"
"github.com/mix-go/bean"
"github.com/mix-go/console/argv"
"github.com/mix-go/console/flag"
"github.com/mix-go/event"
"os"
"strings"
)
var (
// App 全局APP
App *Application
// Version 版本号
Version = "1.0.24"
// LastError 最后的错误
LastError interface{}
)
// NewApplication 创建应用
func NewApplication(definition ApplicationDefinition, dispatcherName, errorName string) *Application {
App = &Application{
ApplicationDefinition: definition,
DispatcherName: dispatcherName,
ErrorName: errorName,
}
App.Init()
return App
}
// ApplicationDefinition 应用定义
type ApplicationDefinition struct {
// 应用名称
Name string
// 应用版本
Version string
// 应用调试
Debug bool
// 依赖配置
Beans []bean.BeanDefinition
// 命令集合
Commands []CommandDefinition
}
// Application 应用
type Application struct {
// App 定义
ApplicationDefinition
// Event Dispatcher
DispatcherName string
Dispatcher event.Dispatcher
// Error
ErrorName string
Error Error
// 基础路径
BasePath string
// 应用上下文
Context *bean.ApplicationContext
// 是否单命令
Singleton bool
// 默认命令
DefaultCommand string
}
// CommandDefinition 命令定义
type CommandDefinition struct {
// 命令名称
Name string
// 使用描述
Usage string
// 选项
Options []OptionDefinition
// 命令
Command Command
// 是否单命令
Singleton bool
// 是否为默认命令
Default bool
}
// Command 命令接口
type Command interface {
Main()
}
// OptionDefinition 命令选项
type OptionDefinition struct {
Names []string
Usage string
}
// Init 初始化
func (t *Application) Init() {
t.Context = bean.NewApplicationContext(t.Beans)
t.Dispatcher = t.Context.Get(t.DispatcherName).(event.Dispatcher)
t.Error = t.Context.Get(t.ErrorName).(Error)
t.BasePath = argv.Program().Dir
for _, c := range t.Commands {
if c.Singleton {
t.Singleton = true
}
if c.Default {
t.DefaultCommand = c.Name
}
}
}
// Get 快速获取实例
func (t *Application) Get(name string) interface{} {
return t.Context.Get(name)
}
// Run 执行
func (t *Application) Run() {
defer func() {
if err := recover(); err != nil {
switch err.(type) {
case *NotFoundError, *UnsupportError:
fmt.Println(err)
return
}
t.Error.Handle(err)
}
}()
if len(t.Commands) == 0 {
panic(errors.New("command cannot be empty"))
}
command := argv.Command()
if command == "" {
if flag.Match("h", "help").Bool(false) {
t.globalHelp()
return
}
if flag.Match("v", "version").Bool(false) {
t.version()
return
}
options := flag.Options().Map()
if len(options) == 0 {
if t.DefaultCommand != "" {
os.Args = append(os.Args, t.DefaultCommand)
argv.Parse()
flag.Parse()
t.Run()
} else {
t.globalHelp()
}
return
}
if t.Singleton {
t.call()
return
}
f := ""
for k := range options {
f = k
break
}
p := argv.Program().Path
panic(NewNotFoundError(fmt.Errorf("flag provided but not defined: '%s', see '%s --help'.", f, p)))
} else if flag.Match("help").Bool(false) {
t.commandHelp()
return
}
t.call()
}
// 执行命令
func (t *Application) call() {
// 命令行选项效验
t.validateOptions()
// 提取命令
var d *CommandDefinition
command := argv.Command()
if t.Singleton {
// 单命令
for _, c := range t.Commands {
if c.Singleton {
d = &c
break
}
}
if d == nil {
panic(errors.New("singleton command not found"))
}
} else {
for _, c := range t.Commands {
if c.Name == command {
d = &c
break
}
}
}
if d == nil {
panic(NewNotFoundError(fmt.Errorf("'%s' is not command, see '%s --help'.", command, argv.Program().Path)))
}
// 获取命令
c := d.Command
// 触发执行命令前置事件
e := &CommandBeforeExecuteEvent{
Command: c,
}
t.Dispatcher.Dispatch(e)
// 执行命令
c.Main()
}
// 命令行选项效验
func (t *Application) validateOptions() {
var options []OptionDefinition
if !t.Singleton {
for _, v := range t.Commands {
if v.Name == argv.Command() {
options = v.Options
break
}
}
} else {
for _, v := range t.Commands {
if v.Singleton {
options = v.Options
break
}
}
}
if len(options) == 0 {
return
}
var flags []string
for _, o := range options {
for _, v := range o.Names {
if len(v) == 1 {
flags = append(flags, fmt.Sprintf("-%s", v))
} else {
flags = append(flags, fmt.Sprintf("--%s", v))
}
}
}
inArray := func(value string, values []string) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
for f := range flag.Options().Map() {
if !inArray(f, flags) {
p := argv.Program().Path
c := argv.Command()
if c != "" {
c = fmt.Sprintf(" %s", c)
}
panic(NewNotFoundError(fmt.Errorf("flag provided but not defined: '%s', see '%s%s --help'.", f, p, c)))
}
}
}
// 全局帮助
func (t *Application) globalHelp() {
program := argv.Program().Path
fg := ""
if !t.Singleton {
fg = " [OPTIONS] COMMAND"
}
fmt.Println(fmt.Sprintf("Usage: %s%s [opt...]", program, fg))
if !t.Singleton {
t.printCommands()
} else {
t.printCommandOptions()
}
t.printGlobalOptions()
fmt.Println("")
fg = ""
if !t.Singleton {
fg = " COMMAND"
}
fmt.Println("")
fmt.Println(fmt.Sprintf("Run '%s%s --help' for more information on a command.", program, fg))
fmt.Println("")
fmt.Println("Developed with Mix Go framework. (openmix.org/mix-go)")
}
// 命令帮助
func (t *Application) commandHelp() {
program := argv.Program().Path
command := argv.Command()
fmt.Println(fmt.Sprintf("Usage: %s %s [opt...]", program, command))
t.printCommandOptions()
fmt.Println("")
fmt.Println("Developed with Mix Go framework. (openmix.org/mix-go)")
}
// 打印全局选项
func (t *Application) printGlobalOptions() {
tabs := "\t"
fmt.Println("")
fmt.Println("Global Options:")
fmt.Println(fmt.Sprintf(" -h, --help%sPrint usage", tabs))
fmt.Println(fmt.Sprintf(" -v, --version%sPrint version information", tabs))
}
// 打印命令
func (t *Application) printCommands() {
fmt.Println("")
fmt.Println("Commands:")
for _, v := range t.Commands {
command := v.Name
usage := v.Usage
fmt.Println(fmt.Sprintf(" %s\t%s", command, usage))
}
}
// 打印命令选项
func (t *Application) printCommandOptions() {
var options []OptionDefinition
if !t.Singleton {
for _, v := range t.Commands {
if v.Name == argv.Command() {
options = v.Options
break
}
}
} else {
for _, v := range t.Commands {
if v.Singleton {
options = v.Options
break
}
}
}
if len(options) == 0 {
return
}
fmt.Println("")
fmt.Println("Command Options:")
for _, o := range options {
var flags []string
for _, v := range o.Names {
if len(v) == 1 {
flags = append(flags, fmt.Sprintf("-%s", v))
} else {
flags = append(flags, fmt.Sprintf("--%s", v))
}
}
fg := strings.Join(flags, ", ")
usage := o.Usage
fmt.Println(fmt.Sprintf(" %s\t%s", fg, usage))
}
}
// 版本号
func (t *Application) version() {
appName := t.Name
appVersion := t.Version
frameworkVersion := Version
fmt.Println(fmt.Sprintf("%s %s, framework %s", appName, appVersion, frameworkVersion))
}