-
Notifications
You must be signed in to change notification settings - Fork 19
/
registry.go
47 lines (40 loc) · 1.43 KB
/
registry.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
package plugin
import "fmt"
// Registry is a registry of all Aptomi engine plugins
type Registry interface {
GetDeployPlugin(codeType string) (DeployPlugin, error)
GetPostProcessingPlugins() []PostProcessPlugin
}
// RegistryFactory returns plugins registry on demand
type RegistryFactory func() Registry
type defaultRegistry struct {
deployPlugins map[string]DeployPlugin
postProcessPlugins []PostProcessPlugin
}
// NewRegistry creates a registry of aptomi engine plugins
func NewRegistry(deployPlugins []DeployPlugin, postProcessPlugins []PostProcessPlugin) Registry {
deployPluginsMap := make(map[string]DeployPlugin, len(deployPlugins))
for _, plugin := range deployPlugins {
for _, codeType := range plugin.GetSupportedCodeTypes() {
if _, exist := deployPluginsMap[codeType]; exist {
// todo(slukjanov): is it ok to panic here?
panic("More than one plugin registered for the same codeType: " + codeType)
}
deployPluginsMap[codeType] = plugin
}
}
return &defaultRegistry{
deployPlugins: deployPluginsMap,
postProcessPlugins: postProcessPlugins,
}
}
func (reg *defaultRegistry) GetDeployPlugin(codeType string) (DeployPlugin, error) {
plugin, exist := reg.deployPlugins[codeType]
if !exist {
return nil, fmt.Errorf("can't find deploy plugin for codeType: %s", codeType)
}
return plugin, nil
}
func (reg *defaultRegistry) GetPostProcessingPlugins() []PostProcessPlugin {
return reg.postProcessPlugins
}