/
Console.go
83 lines (69 loc) · 2.29 KB
/
Console.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
package components
import (
"fmt"
"github.com/cinling/cin/base"
"github.com/cinling/cin/configs"
"github.com/cinling/cin/utils"
"os"
"reflect"
"strings"
)
// 命令行组件
type Console struct {
Base
config *configs.Console
controllerDict map[string]reflect.Type // 控制器反射map
controllerActionDict map[string]map[string]bool // 控制器 => 方法 => 是否存在(注册时记录)
}
// 初始化方法
func (component *Console) Init(configInterface base.ConfigComponentInterface) {
component.Base.Init(configInterface)
configValue := reflect.ValueOf(configInterface)
var config *configs.Console
if configValue.Kind() == reflect.Ptr {
config = configValue.Interface().(*configs.Console)
} else if configValue.Kind() == reflect.Struct {
configStruct := configValue.Interface().(configs.Console)
config = &configStruct
} else {
panic("illegal config")
}
component.config = config
// 注册处理器(控制器)
component.controllerDict, component.controllerActionDict = common.getControllerDict(config.ConsoleControllerList)
}
// 运行控制台命令
func (component *Console) RunAction() {
if len(os.Args) < 2 {
fmt.Println("please input route")
return
}
route := os.Args[1]
tmpArr := strings.Split(route, "/")
controllerName := tmpArr[0]
actionName := ""
if len(tmpArr) >= 2 {
actionName = tmpArr[1]
}
controllerType := component.controllerDict[controllerName]
controllerValue := reflect.New(controllerType.Elem())
controllerInterface := controllerValue.Interface().(base.ControllerInterface)
controllerInterface.Init()
component.injectControllerValues(controllerInterface)
controllerInterface.BeforeAction(actionName)
action := controllerValue.MethodByName(utils.Url.UrlToHump(actionName))
_ = action.Call([]reflect.Value{})
controllerInterface.AfterAction(actionName, nil)
}
// 注入控制器参数
func (component *Console) injectControllerValues(controllerIns base.ControllerInterface) {
controllerName := utils.Reflect.GetClassName(controllerIns)
if controllerName == "MigrateController" {
databaseComponentIns := common.app.GetComponent(new(Database))
if databaseComponentIns == nil {
return
}
databaseComponent := databaseComponentIns.(*Database)
controllerIns.AddValue("migrateDir", databaseComponent.config.GetMigrateDir())
}
}