-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.go
98 lines (85 loc) · 3.21 KB
/
kernel.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
package console
import (
"errors"
"fmt"
"github.com/goal-web/console/scheduling"
"github.com/goal-web/contracts"
"github.com/modood/table"
)
var CommandDontExists = errors.New("命令不存在!")
const logoText = " ▄████ ▒█████ ▄▄▄ ██▓ \n ██▒ ▀█▒▒██▒ ██▒▒████▄ ▓██▒ \n▒██░▄▄▄░▒██░ ██▒▒██ ▀█▄ ▒██░ \n░▓█ ██▓▒██ ██░░██▄▄▄▄██ ▒██░ \n░▒▓███▀▒░ ████▓▒░ ▓█ ▓██▒░██████▒\n ░▒ ▒ ░ ▒░▒░▒░ ▒▒ ▓▒█░░ ▒░▓ ░\n ░ ░ ░ ▒ ▒░ ▒ ▒▒ ░░ ░ ▒ ░\n░ ░ ░ ░ ░ ░ ▒ ░ ▒ ░ ░ \n ░ ░ ░ ░ ░ ░ ░\n "
type Kernel struct {
app contracts.Application
commands map[string]contracts.CommandProvider
schedule contracts.Schedule
exceptionHandler contracts.ExceptionHandler
}
func NewKernel(app contracts.Application, commandProviders []contracts.CommandProvider) *Kernel {
var commands = make(map[string]contracts.CommandProvider)
for _, provider := range commandProviders {
commands[provider(app).GetName()] = provider
}
return &Kernel{
app: app,
commands: commands,
schedule: scheduling.NewSchedule(app),
exceptionHandler: app.Get("exceptions.handler").(contracts.ExceptionHandler),
}
}
func (kernel *Kernel) RegisterCommand(name string, command contracts.CommandProvider) {
kernel.commands[name] = command
}
func (kernel *Kernel) GetSchedule() contracts.Schedule {
return kernel.schedule
}
func (kernel *Kernel) Schedule(schedule contracts.Schedule) {
}
type CommandItem struct {
Command string
Signature string
Description string
}
func (kernel *Kernel) Help() {
cmdTable := make([]CommandItem, 0)
for _, command := range kernel.commands {
cmd := command(kernel.app)
cmdTable = append(cmdTable, CommandItem{
Command: cmd.GetName(),
Signature: cmd.GetSignature(),
Description: cmd.GetDescription(),
})
}
fmt.Println(logoText)
table.Output(cmdTable)
}
func (kernel *Kernel) Call(cmd string, arguments contracts.CommandArguments) any {
if cmd == "" {
kernel.Help()
return nil
}
for name, provider := range kernel.commands {
if cmd == name {
command := provider(kernel.app)
if arguments.Exists("h") || arguments.Exists("help") {
fmt.Println(logoText)
fmt.Printf(" %s 命令:%s\n", command.GetName(), command.GetDescription())
fmt.Println(command.GetHelp())
return nil
}
if err := command.InjectArguments(arguments); err != nil {
kernel.exceptionHandler.Handle(&CommandArgumentException{Err: errors.New("the parameter is wrong")})
fmt.Println(err.Error())
fmt.Println(command.GetHelp())
return nil
}
return command.Handle()
}
}
return CommandDontExists
}
func (kernel *Kernel) Run(input contracts.ConsoleInput) any {
return kernel.Call(input.GetCommand(), input.GetArguments())
}
func (kernel *Kernel) Exists(name string) bool {
return kernel.commands[name] != nil
}