github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/structs/config/plugins.go (about) 1 package config 2 3 import "github.com/mitchellh/copystructure" 4 5 // PluginConfig is used to configure a plugin explicitly 6 type PluginConfig struct { 7 Name string `hcl:",key"` 8 Args []string `hcl:"args"` 9 Config map[string]interface{} `hcl:"config"` 10 // ExtraKeysHCL is used by hcl to surface unexpected keys 11 ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"` 12 } 13 14 func (p *PluginConfig) Merge(o *PluginConfig) *PluginConfig { 15 m := *p 16 17 if len(o.Name) != 0 { 18 m.Name = o.Name 19 } 20 if len(o.Args) != 0 { 21 m.Args = o.Args 22 } 23 if len(o.Config) != 0 { 24 m.Config = o.Config 25 } 26 27 return m.Copy() 28 } 29 30 func (p *PluginConfig) Copy() *PluginConfig { 31 c := *p 32 if i, err := copystructure.Copy(p.Config); err != nil { 33 panic(err.Error()) 34 } else { 35 c.Config = i.(map[string]interface{}) 36 } 37 return &c 38 } 39 40 // PluginConfigSetMerge merges to sets of plugin configs. For plugins with the 41 // same name, the configs are merged. 42 func PluginConfigSetMerge(first, second []*PluginConfig) []*PluginConfig { 43 findex := make(map[string]*PluginConfig, len(first)) 44 for _, p := range first { 45 findex[p.Name] = p 46 } 47 48 sindex := make(map[string]*PluginConfig, len(second)) 49 for _, p := range second { 50 sindex[p.Name] = p 51 } 52 53 var out []*PluginConfig 54 55 // Go through the first set and merge any value that exist in both 56 for pluginName, original := range findex { 57 second, ok := sindex[pluginName] 58 if !ok { 59 out = append(out, original.Copy()) 60 continue 61 } 62 63 out = append(out, original.Merge(second)) 64 } 65 66 // Go through the second set and add any value that didn't exist in both 67 for pluginName, plugin := range sindex { 68 _, ok := findex[pluginName] 69 if ok { 70 continue 71 } 72 73 out = append(out, plugin) 74 } 75 76 return out 77 }