-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconfig.go
More file actions
136 lines (111 loc) · 2.88 KB
/
config.go
File metadata and controls
136 lines (111 loc) · 2.88 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package plugin
import (
"encoding/json"
"reflect"
"github.com/bluemedora/bplogagent/errors"
"go.etcd.io/bbolt"
"go.uber.org/zap"
)
type Config struct {
PluginBuilder `yaml:",inline"`
}
type PluginBuilder interface {
ID() string
Type() string
Build(BuildContext) (Plugin, error)
}
// BuildContext supplies contextual resources when building a plugin.
type BuildContext struct {
Database *bbolt.DB
Logger *zap.SugaredLogger
}
// BuildPlugins will build a collection of plugins from plugin configs.
func BuildPlugins(configs []Config, context BuildContext) ([]Plugin, error) {
plugins := make([]Plugin, 0, len(configs))
for _, config := range configs {
plugin, err := config.Build(context)
if err != nil {
return plugins, errors.WithDetails(err,
"plugin_id", config.ID(),
"plugin_type", config.Type(),
)
}
plugins = append(plugins, plugin)
}
return plugins, nil
}
// configDefinitions is a registry of plugin types to plugin configs.
var configDefinitions = make(map[string]func() interface{})
// Register will register a plugin config by plugin type.
func Register(pluginType string, config PluginBuilder) {
configDefinitions[pluginType] = func() interface{} {
val := reflect.New(reflect.TypeOf(config).Elem()).Interface()
return val.(PluginBuilder)
}
}
func (c *Config) UnmarshalJSON(raw []byte) error {
var typeDecoder struct {
Type string
}
err := json.Unmarshal(raw, &typeDecoder)
if err != nil {
return err
}
if typeDecoder.Type == "" {
return ErrMissingType
}
pluginBuilderGenerator, ok := configDefinitions[typeDecoder.Type]
if !ok {
return NewErrUnknownType(typeDecoder.Type)
}
pluginBuilder := pluginBuilderGenerator()
err = json.Unmarshal(raw, pluginBuilder)
if err != nil {
return err
}
c.PluginBuilder = pluginBuilder.(PluginBuilder)
return nil
}
func (c Config) MarshalJSON() ([]byte, error) {
return json.Marshal(c.PluginBuilder)
}
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
var typeDecoder struct {
Type string
}
err := unmarshal(&typeDecoder)
if err != nil {
return err
}
if typeDecoder.Type == "" {
return ErrMissingType
}
pluginBuilderGenerator, ok := configDefinitions[typeDecoder.Type]
if !ok {
return NewErrUnknownType(typeDecoder.Type)
}
pluginBuilder := pluginBuilderGenerator()
err = unmarshal(pluginBuilder)
if err != nil {
return err
}
c.PluginBuilder = pluginBuilder.(PluginBuilder)
return nil
}
func (c Config) MarshalYAML() (interface{}, error) {
return c.PluginBuilder, nil
}
/*********
Errors
*********/
var ErrMissingType = errors.NewError(
"Missing required field `type`.",
"Ensure that all plugin configs have a `type` field set",
)
func NewErrUnknownType(pluginType string) errors.AgentError {
return errors.NewError(
"Plugin config has an unknown plugin type.",
"Ensure that all plugin configs have a known, valid type.",
"plugin_type", pluginType,
)
}