forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Merge(c1, c2 *Config) (*Config, error) { | ||
c := new(Config) | ||
|
||
// Merge variables: Variable merging is quite simple. Set fields in | ||
// later set variables override those earlier. | ||
c.Variables = c1.Variables | ||
for k, v2 := range c2.Variables { | ||
v1, ok := c.Variables[k] | ||
if ok { | ||
if v2.Default == "" { | ||
v2.Default = v1.Default | ||
} | ||
if v2.Description == "" { | ||
v2.Description = v1.Description | ||
} | ||
} | ||
|
||
c.Variables[k] = v2 | ||
} | ||
|
||
// Merge outputs: If they collide, just take the latest one for now. In | ||
// the future, we might provide smarter merge functionality. | ||
c.Outputs = make(map[string]*Output) | ||
for k, v := range c1.Outputs { | ||
c.Outputs[k] = v | ||
} | ||
for k, v := range c2.Outputs { | ||
c.Outputs[k] = v | ||
} | ||
|
||
// Merge provider configs: If they collide, we just take the latest one | ||
// for now. In the future, we might provide smarter merge functionality. | ||
c.ProviderConfigs = make(map[string]*ProviderConfig) | ||
for k, v := range c1.ProviderConfigs { | ||
c.ProviderConfigs[k] = v | ||
} | ||
for k, v := range c2.ProviderConfigs { | ||
c.ProviderConfigs[k] = v | ||
} | ||
|
||
// Merge resources: If they collide, we just take the latest one | ||
// for now. In the future, we might provide smarter merge functionality. | ||
resources := make(map[string]*Resource) | ||
for _, r := range c1.Resources { | ||
id := fmt.Sprintf("%s[%s]", r.Type, r.Name) | ||
resources[id] = r | ||
} | ||
for _, r := range c2.Resources { | ||
id := fmt.Sprintf("%s[%s]", r.Type, r.Name) | ||
resources[id] = r | ||
} | ||
|
||
c.Resources = make([]*Resource, 0, len(resources)) | ||
for _, r := range resources { | ||
c.Resources = append(c.Resources, r) | ||
} | ||
|
||
return c, nil | ||
} |