github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/config/config_tree.go (about)

     1  package config
     2  
     3  // configTree represents a tree of configurations where the root is the
     4  // first file and its children are the configurations it has imported.
     5  type configTree struct {
     6  	Path     string
     7  	Config   *Config
     8  	Children []*configTree
     9  }
    10  
    11  // Flatten flattens the entire tree down to a single merged Config
    12  // structure.
    13  func (t *configTree) Flatten() (*Config, error) {
    14  	// No children is easy: we're already merged!
    15  	if len(t.Children) == 0 {
    16  		return t.Config, nil
    17  	}
    18  
    19  	// Depth-first, merge all the children first.
    20  	childConfigs := make([]*Config, len(t.Children))
    21  	for i, ct := range t.Children {
    22  		c, err := ct.Flatten()
    23  		if err != nil {
    24  			return nil, err
    25  		}
    26  
    27  		childConfigs[i] = c
    28  	}
    29  
    30  	// Merge all the children in order
    31  	config := childConfigs[0]
    32  	childConfigs = childConfigs[1:]
    33  	for _, config2 := range childConfigs {
    34  		var err error
    35  		config, err = Merge(config, config2)
    36  		if err != nil {
    37  			return nil, err
    38  		}
    39  	}
    40  
    41  	// Merge the final merged child config with our own
    42  	return Merge(config, t.Config)
    43  }