forked from forj-oss/goforjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
97 lines (83 loc) · 2.38 KB
/
plugins.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
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
package goforjj
import (
"fmt"
"gopkg.in/yaml.v2"
)
// Plugins define a structure to store all plugins loaded.
type Plugins struct {
drivers map[string]*Driver // key: plugin type, plugin name
plugins map[string]map[string]*YamlPlugin
}
// NewPlugins create the list of plugins in memory.
func NewPlugins() (ret *Plugins) {
ret = new(Plugins)
ret.drivers = make(map[string]*Driver)
ret.plugins = make(map[string]map[string]*YamlPlugin)
return
}
// Load the instance plugin and return the driver object
// All plugin instances can share the same plugin. So that a plugin definition is loaded only once.
// But each driver are instance unique.
func (ps *Plugins) Load(instanceName, pluginName, pluginType string, loader map[string]func(*YamlPlugin) (yaml_data []byte, err error)) (driver *Driver, err error) {
// check if driver already loaded
if d, found := ps.drivers[instanceName]; found {
return d, nil
}
plugin, newStatus := ps.definePlugin(pluginName, pluginType)
var yaml_data []byte
if newStatus {
if lfunc, found := loader["master"] ; !found {
delete(ps.plugins[pluginType], pluginName)
return nil, fmt.Errorf("Internal issue. Load requires 'master' function loader")
} else {
yaml_data, err = lfunc(plugin)
}
if err != nil {
delete(ps.plugins[pluginType], pluginName)
plugin = nil
return
}
if err = yaml.Unmarshal(yaml_data, plugin); err != nil {
delete(ps.plugins[pluginType], pluginName)
return
}
}
driver = NewDriver(plugin)
ps.drivers[instanceName] = driver
// TODO: Need to load instances details now.
if lfunc, found := loader["extended"] ; !found {
return
} else {
yaml_data, err = lfunc(plugin)
}
if err != nil {
plugin = nil
return
}
if len(yaml_data) == 0 {
return
}
pluginExtend := new(YamlPluginTasksObjects)
if err = yaml.Unmarshal(yaml_data, pluginExtend); err != nil {
return
}
driver.Yaml = driver.Yaml.MergeWith(instanceName, pluginExtend)
return
}
func (ps *Plugins) definePlugin(driverName, driverType string) (plugin *YamlPlugin, new bool) {
new = true
if pn, ptFound := ps.plugins[driverType]; !ptFound {
pt := make(map[string]*YamlPlugin)
plugin = NewYamlPlugin()
pt[driverName] = plugin
ps.plugins[driverType] = pt
} else if p, pFound := pn[driverName]; !pFound {
plugin = NewYamlPlugin()
pn[driverName] = plugin
ps.plugins[driverType] = pn
} else {
plugin = p
new = false
}
return
}