github.com/smithx10/nomad@v0.9.1-rc1/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  }
    11  
    12  func (p *PluginConfig) Merge(o *PluginConfig) *PluginConfig {
    13  	m := *p
    14  
    15  	if len(o.Name) != 0 {
    16  		m.Name = o.Name
    17  	}
    18  	if len(o.Args) != 0 {
    19  		m.Args = o.Args
    20  	}
    21  	if len(o.Config) != 0 {
    22  		m.Config = o.Config
    23  	}
    24  
    25  	return m.Copy()
    26  }
    27  
    28  func (p *PluginConfig) Copy() *PluginConfig {
    29  	c := *p
    30  	if i, err := copystructure.Copy(p.Config); err != nil {
    31  		panic(err.Error())
    32  	} else {
    33  		c.Config = i.(map[string]interface{})
    34  	}
    35  	return &c
    36  }
    37  
    38  // PluginConfigSetMerge merges to sets of plugin configs. For plugins with the
    39  // same name, the configs are merged.
    40  func PluginConfigSetMerge(first, second []*PluginConfig) []*PluginConfig {
    41  	findex := make(map[string]*PluginConfig, len(first))
    42  	for _, p := range first {
    43  		findex[p.Name] = p
    44  	}
    45  
    46  	sindex := make(map[string]*PluginConfig, len(second))
    47  	for _, p := range second {
    48  		sindex[p.Name] = p
    49  	}
    50  
    51  	var out []*PluginConfig
    52  
    53  	// Go through the first set and merge any value that exist in both
    54  	for pluginName, original := range findex {
    55  		second, ok := sindex[pluginName]
    56  		if !ok {
    57  			out = append(out, original.Copy())
    58  			continue
    59  		}
    60  
    61  		out = append(out, original.Merge(second))
    62  	}
    63  
    64  	// Go through the second set and add any value that didn't exist in both
    65  	for pluginName, plugin := range sindex {
    66  		_, ok := findex[pluginName]
    67  		if ok {
    68  			continue
    69  		}
    70  
    71  		out = append(out, plugin)
    72  	}
    73  
    74  	return out
    75  }