2014-05-24 00:28:19 +02:00
|
|
|
package config
|
|
|
|
|
2014-07-12 05:15:09 +02:00
|
|
|
import (
|
2014-07-12 06:04:59 +02:00
|
|
|
"fmt"
|
2014-07-12 05:15:09 +02:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
// Load loads the Terraform configuration from a given file.
|
2014-05-24 00:32:34 +02:00
|
|
|
//
|
|
|
|
// This file can be any format that Terraform recognizes, and import any
|
|
|
|
// other format that Terraform recognizes.
|
2014-05-24 00:28:19 +02:00
|
|
|
func Load(path string) (*Config, error) {
|
|
|
|
importTree, err := loadTree(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configTree, err := importTree.ConfigTree()
|
2014-05-24 01:30:28 +02:00
|
|
|
|
|
|
|
// Close the importTree now so that we can clear resources as quickly
|
|
|
|
// as possible.
|
|
|
|
importTree.Close()
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return configTree.Flatten()
|
|
|
|
}
|
2014-07-12 05:15:09 +02: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
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:04:59 +02:00
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"No Terraform configuration files found in directory: %s",
|
|
|
|
path)
|
|
|
|
}
|
|
|
|
|
2014-07-12 05:15:09 +02:00
|
|
|
var result *Config
|
|
|
|
for _, f := range matches {
|
|
|
|
c, err := Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != nil {
|
2014-07-20 01:44:23 +02:00
|
|
|
result, err = Append(result, c)
|
2014-07-12 05:15:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|