-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
77 lines (62 loc) · 1.74 KB
/
registry.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
package cli
import "sort"
// OnInitFunc 定义组件的初始化函数
type OnInitFunc func(c *Context) error
////////////////////////////////////////////////////////////////////////////////
// HandlerRegistration ... 定义一条命令的注册信息
type HandlerRegistration struct {
Name string
Handler HandlerFunc
Help Help
OnInit OnInitFunc
}
// HandlerRegistry ... 该接口表示一个命令注册对象
type HandlerRegistry interface {
GetHandlers() []*HandlerRegistration
}
////////////////////////////////////////////////////////////////////////////////
// FilterRegistration ... 定义一条命令的注册信息
type FilterRegistration struct {
Name string
Filter Filter
Order int
OnInit OnInitFunc
}
// FilterRegistry ... 该接口表示一个命令注册对象
// [inject:".cli-filter-registry"]
type FilterRegistry interface {
GetFilters() []*FilterRegistration
}
////////////////////////////////////////////////////////////////////////////////
// FilterRegistrationSorter ...
type FilterRegistrationSorter struct {
items []*FilterRegistration
reverse bool
}
// Sort ...
func (inst *FilterRegistrationSorter) Sort(list []*FilterRegistration, reverse bool) {
child := &FilterRegistrationSorter{
items: list,
reverse: reverse,
}
sort.Sort(child)
}
func (inst *FilterRegistrationSorter) Len() int {
return len(inst.items)
}
func (inst *FilterRegistrationSorter) Less(i1, i2 int) bool {
o1 := inst.items[i1]
o2 := inst.items[i2]
b := inst.reverse
if o1.Order < o2.Order {
b = !b
}
return b
}
func (inst *FilterRegistrationSorter) Swap(i1, i2 int) {
o1 := inst.items[i1]
o2 := inst.items[i2]
inst.items[i1] = o2
inst.items[i2] = o1
}
////////////////////////////////////////////////////////////////////////////////