-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.go
More file actions
107 lines (93 loc) · 1.91 KB
/
Copy pathplugins.go
File metadata and controls
107 lines (93 loc) · 1.91 KB
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
package plugins
import (
"fmt"
"sync"
"github.com/Sirupsen/logrus"
)
var (
activePlugins = &plugins{plugins: make(map[string]*Plugin)}
extpointHandlers = make(map[string]func(string, *Client))
)
type plugins struct {
sync.Mutex
plugins map[string]*Plugin
}
type Manifest struct {
Implements []string
}
type Plugin struct {
Name string
Addr string
Client *Client
Manifest *Manifest
}
func (p *Plugin) Activate() error {
activePlugins.Lock()
defer activePlugins.Unlock()
_, exists := activePlugins.plugins[p.Name]
if exists {
return fmt.Errorf("Plugin already activated")
}
var m *Manifest
p.Client = NewClient(p.Addr)
err := p.Client.Call("Plugin.Activate", nil, m)
if err != nil {
return err
}
p.Manifest = m
for _, iface := range m.Implements {
handler, handled := extpointHandlers[iface]
if !handled {
continue
}
handler(p.Name, p.Client)
}
activePlugins.plugins[p.Name] = p
return nil
}
func Load() error {
registry := newLocalRegistry("")
plugins, err := registry.Plugins()
if err != nil {
return err
}
for _, plugin := range plugins {
err := plugin.Activate()
if err != nil {
// intentionally not bubbling
// activation errors up.
logrus.Warn("Plugin load error:", err)
}
}
return nil
}
func Get(name string) (*Plugin, error) {
activePlugins.Lock()
plugin, exists := activePlugins.plugins[name]
activePlugins.Unlock()
if !exists {
registry := newLocalRegistry("")
plugin, err := registry.Plugin(name)
if err != nil {
return nil, err
}
err = plugin.Activate()
if err != nil {
return nil, err
}
return plugin, nil
}
return plugin, nil
}
func Active() []*Plugin {
activePlugins.Lock()
defer activePlugins.Unlock()
var plugins []*Plugin
for _, plugin := range activePlugins.plugins {
plugins = append(plugins, plugin)
}
return plugins
}
func Handle(iface string, fn func(string, *Client)) {
extpointHandlers[iface] = fn
}