2014-05-23 17:28:19 -05:00
|
|
|
package config
|
|
|
|
|
2014-07-11 22:15:09 -05:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2014-05-23 17:28:19 -05:00
|
|
|
// Load loads the Terraform configuration from a given file.
|
2014-05-23 17:32:34 -05:00
|
|
|
//
|
|
|
|
// This file can be any format that Terraform recognizes, and import any
|
|
|
|
// other format that Terraform recognizes.
|
2014-05-23 17:28:19 -05:00
|
|
|
func Load(path string) (*Config, error) {
|
|
|
|
importTree, err := loadTree(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configTree, err := importTree.ConfigTree()
|
2014-05-23 18:30:28 -05:00
|
|
|
|
|
|
|
// Close the importTree now so that we can clear resources as quickly
|
|
|
|
// as possible.
|
|
|
|
importTree.Close()
|
|
|
|
|
2014-05-23 17:28:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return configTree.Flatten()
|
|
|
|
}
|
2014-07-11 22:15:09 -05:00
|
|
|
|
|
|
|
// LoadDir loads all the Terraform configuration files in a single
|
|
|
|
// directory and merges them together.
|
|
|
|
func LoadDir(path string) (*Config, error) {
|
|
|
|
matches, err := filepath.Glob(filepath.Join(path, "*.tf"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *Config
|
|
|
|
for _, f := range matches {
|
|
|
|
c, err := Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != nil {
|
|
|
|
result, err = mergeConfig(result, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|