/
plugins.go
77 lines (63 loc) · 1.7 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
package config
import "github.com/mitchellh/copystructure"
// PluginConfig is used to configure a plugin explicitly
type PluginConfig struct {
Name string `hcl:",key"`
Args []string `hcl:"args"`
Config map[string]interface{} `hcl:"config"`
// ExtraKeysHCL is used by hcl to surface unexpected keys
ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"`
}
func (p *PluginConfig) Merge(o *PluginConfig) *PluginConfig {
m := *p
if len(o.Name) != 0 {
m.Name = o.Name
}
if len(o.Args) != 0 {
m.Args = o.Args
}
if len(o.Config) != 0 {
m.Config = o.Config
}
return m.Copy()
}
func (p *PluginConfig) Copy() *PluginConfig {
c := *p
if i, err := copystructure.Copy(p.Config); err != nil {
panic(err.Error())
} else {
c.Config = i.(map[string]interface{})
}
return &c
}
// PluginConfigSetMerge merges to sets of plugin configs. For plugins with the
// same name, the configs are merged.
func PluginConfigSetMerge(first, second []*PluginConfig) []*PluginConfig {
findex := make(map[string]*PluginConfig, len(first))
for _, p := range first {
findex[p.Name] = p
}
sindex := make(map[string]*PluginConfig, len(second))
for _, p := range second {
sindex[p.Name] = p
}
var out []*PluginConfig
// Go through the first set and merge any value that exist in both
for pluginName, original := range findex {
second, ok := sindex[pluginName]
if !ok {
out = append(out, original.Copy())
continue
}
out = append(out, original.Merge(second))
}
// Go through the second set and add any value that didn't exist in both
for pluginName, plugin := range sindex {
_, ok := findex[pluginName]
if ok {
continue
}
out = append(out, plugin)
}
return out
}