opentofu/config/config_tree.go

44 lines
1.0 KiB
Go
Raw Normal View History

package config
// configTree represents a tree of configurations where the root is the
// first file and its children are the configurations it has imported.
type configTree struct {
Path string
Config *Config
Children []*configTree
}
// Flatten flattens the entire tree down to a single merged Config
// structure.
func (t *configTree) Flatten() (*Config, error) {
2014-05-23 18:09:41 -05:00
// 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
2014-07-18 19:05:22 -05:00
config, err = Merge(config, config2)
2014-05-23 18:09:41 -05:00
if err != nil {
return nil, err
}
}
// Merge the final merged child config with our own
2014-07-18 19:05:22 -05:00
return Merge(config, t.Config)
}