forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
48 lines (37 loc) · 933 Bytes
/
plugin.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
package plugin
import "fmt"
type PluginLoader func(p interface{}) error
var registry = map[string]PluginLoader{}
func Bundle(
bundles ...map[string][]interface{},
) map[string][]interface{} {
ret := map[string][]interface{}{}
for _, bundle := range bundles {
for name, plugins := range bundle {
ret[name] = append(ret[name], plugins...)
}
}
return ret
}
func MakePlugin(key string, ifc interface{}) map[string][]interface{} {
return map[string][]interface{}{
key: {ifc},
}
}
func MustRegisterLoader(name string, l PluginLoader) {
err := RegisterLoader(name, l)
if err != nil {
panic(err)
}
}
func RegisterLoader(name string, l PluginLoader) error {
if l := registry[name]; l != nil {
return fmt.Errorf("plugin loader '%v' already registered", name)
}
registry[name] = l
return nil
}
func LoadPlugins(path string) error {
// TODO: add flag to enable/disable plugins?
return loadPlugins(path)
}