diff --git a/config/config_tree.go b/config/config_tree.go index 4025deece9..1486bbd6a9 100644 --- a/config/config_tree.go +++ b/config/config_tree.go @@ -11,5 +11,58 @@ type configTree struct { // Flatten flattens the entire tree down to a single merged Config // structure. func (t *configTree) Flatten() (*Config, error) { - return t.Config, nil + // No children is easy: we're already merged! + if len(t.Children) == 0 { + return t.Config, nil + } + + // Depth-first, merge all the children first. + childConfigs := make([]*Config, len(t.Children)) + for i, ct := range t.Children { + c, err := ct.Flatten() + if err != nil { + return nil, err + } + + childConfigs[i] = c + } + + // Merge all the children in order + config := childConfigs[0] + childConfigs = childConfigs[1:] + for _, config2 := range childConfigs { + var err error + config, err = mergeConfig(config, config2) + if err != nil { + return nil, err + } + } + + // Merge the final merged child config with our own + return mergeConfig(config, t.Config) +} + +func mergeConfig(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 + } + + // TODO: merge resources + + return c, nil } diff --git a/config/import_tree.go b/config/import_tree.go index cc1bd6800c..56800080cd 100644 --- a/config/import_tree.go +++ b/config/import_tree.go @@ -80,7 +80,16 @@ func (t *importTree) ConfigTree() (*configTree, error) { Config: config, } - // TODO: Follow children and load them + // Build the config trees for the children + result.Children = make([]*configTree, len(t.Children)) + for i, ct := range t.Children { + t, err := ct.ConfigTree() + if err != nil { + return nil, err + } + + result.Children[i] = t + } return result, nil } diff --git a/config/loader_test.go b/config/loader_test.go index 426bb20989..71d0250411 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -81,6 +81,13 @@ func resourcesStr(rs []Resource) string { func variablesStr(vs map[string]Variable) string { result := "" for k, v := range vs { + if v.Default == "" { + v.Default = "<>" + } + if v.Description == "" { + v.Description = "<>" + } + result += fmt.Sprintf( "%s\n %s\n %s\n", k, @@ -105,8 +112,10 @@ foo ` const importVariablesStr = ` +bar + <> + <> foo bar bar -bar `