-
Notifications
You must be signed in to change notification settings - Fork 27
/
hook_storage.go
82 lines (66 loc) · 1.76 KB
/
hook_storage.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
package modules
import (
"sort"
"sync"
"github.com/flant/addon-operator/pkg/module_manager/models/hooks"
sh_op_types "github.com/flant/shell-operator/pkg/hook/types"
)
// HooksStorage keep module hooks in order
type HooksStorage struct {
registered bool
lock sync.RWMutex
byBinding map[sh_op_types.BindingType][]*hooks.ModuleHook
byName map[string]*hooks.ModuleHook
}
func newHooksStorage() *HooksStorage {
return &HooksStorage{
registered: false,
byBinding: make(map[sh_op_types.BindingType][]*hooks.ModuleHook),
byName: make(map[string]*hooks.ModuleHook),
}
}
func (hs *HooksStorage) AddHook(hk *hooks.ModuleHook) {
hs.lock.Lock()
defer hs.lock.Unlock()
hName := hk.GetName()
hs.byName[hName] = hk
for _, binding := range hk.GetHookConfig().Bindings() {
hs.byBinding[binding] = append(hs.byBinding[binding], hk)
}
}
func (hs *HooksStorage) getHooks(bt ...sh_op_types.BindingType) []*hooks.ModuleHook {
hs.lock.RLock()
defer hs.lock.RUnlock()
if len(bt) > 0 {
t := bt[0]
res, ok := hs.byBinding[t]
if !ok {
return []*hooks.ModuleHook{}
}
sort.Slice(res, func(i, j int) bool {
return res[i].Order(t) < res[j].Order(t)
})
return res
}
// return all hooks
res := make([]*hooks.ModuleHook, 0, len(hs.byName))
for _, h := range hs.byName {
res = append(res, h)
}
sort.Slice(res, func(i, j int) bool {
return res[i].GetName() < res[j].GetName()
})
return res
}
func (hs *HooksStorage) getHookByName(name string) *hooks.ModuleHook {
hs.lock.RLock()
defer hs.lock.RUnlock()
return hs.byName[name]
}
func (hs *HooksStorage) clean() {
hs.lock.Lock()
defer hs.lock.Unlock()
hs.byBinding = make(map[sh_op_types.BindingType][]*hooks.ModuleHook)
hs.byName = make(map[string]*hooks.ModuleHook)
hs.registered = false
}