-
Notifications
You must be signed in to change notification settings - Fork 53
/
scheme.go
58 lines (47 loc) · 999 Bytes
/
scheme.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
package meta
import (
"github.com/hashicorp/go-plugin"
)
type Scheme interface {
Add(string, plugin.Plugin)
PluginMap() map[string]plugin.Plugin
Mode() PluginMode
}
type scheme struct {
SchemeOptions
plugins map[string]plugin.Plugin
}
func (s *scheme) PluginMap() map[string]plugin.Plugin {
return s.plugins
}
func (s *scheme) Add(id string, impl plugin.Plugin) {
if _, ok := s.plugins[id]; ok {
panic("plugin already added")
}
s.plugins[id] = impl
}
func (s *scheme) Mode() PluginMode {
return s.mode
}
type SchemeOptions struct {
mode PluginMode
}
type SchemeOption func(*SchemeOptions)
func (o *SchemeOptions) apply(opts ...SchemeOption) {
for _, op := range opts {
op(o)
}
}
func WithMode(mode PluginMode) SchemeOption {
return func(o *SchemeOptions) {
o.mode = mode
}
}
func NewScheme(opts ...SchemeOption) Scheme {
options := SchemeOptions{}
options.apply(opts...)
return &scheme{
SchemeOptions: options,
plugins: map[string]plugin.Plugin{},
}
}