2014-05-23 17:28:19 -05:00
|
|
|
package config
|
|
|
|
|
2014-07-11 22:15:09 -05:00
|
|
|
import (
|
2014-07-11 23:04:59 -05:00
|
|
|
"fmt"
|
2014-07-20 19:52:46 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
2014-07-11 22:15:09 -05:00
|
|
|
"path/filepath"
|
2014-07-20 19:52:46 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2014-07-11 22:15:09 -05:00
|
|
|
)
|
|
|
|
|
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.
|
2014-07-20 19:52:46 -05:00
|
|
|
func LoadDir(root string) (*Config, error) {
|
|
|
|
var files, overrides []string
|
|
|
|
|
|
|
|
f, err := os.Open(root)
|
2014-07-11 22:15:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-20 19:52:46 -05:00
|
|
|
err = nil
|
|
|
|
for err != io.EOF {
|
|
|
|
var fis []os.FileInfo
|
|
|
|
fis, err = f.Readdir(128)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
// Ignore directories
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only care about files that are valid to load
|
|
|
|
name := fi.Name()
|
|
|
|
extValue := ext(name)
|
|
|
|
if extValue == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if we're dealing with an override
|
|
|
|
nameNoExt := name[:len(name)-len(extValue)]
|
|
|
|
override := nameNoExt == "override" ||
|
|
|
|
strings.HasSuffix(nameNoExt, "_override")
|
|
|
|
|
|
|
|
path := filepath.Join(root, name)
|
|
|
|
if override {
|
|
|
|
overrides = append(overrides, path)
|
|
|
|
} else {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the directory, we're done with it
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if len(files) == 0 {
|
2014-07-11 23:04:59 -05:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"No Terraform configuration files found in directory: %s",
|
2014-07-20 19:52:46 -05:00
|
|
|
root)
|
2014-07-11 23:04:59 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 22:15:09 -05:00
|
|
|
var result *Config
|
2014-07-20 19:52:46 -05:00
|
|
|
|
|
|
|
// Sort the files and overrides so we have a deterministic order
|
|
|
|
sort.Strings(files)
|
|
|
|
sort.Strings(overrides)
|
|
|
|
|
|
|
|
// Load all the regular files, append them to each other.
|
|
|
|
for _, f := range files {
|
2014-07-11 22:15:09 -05:00
|
|
|
c, err := Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != nil {
|
2014-07-19 18:44:23 -05:00
|
|
|
result, err = Append(result, c)
|
2014-07-11 22:15:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 19:52:46 -05:00
|
|
|
// Load all the overrides, and merge them into the config
|
|
|
|
for _, f := range overrides {
|
|
|
|
c, err := Load(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err = Merge(result, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:15:09 -05:00
|
|
|
return result, nil
|
|
|
|
}
|
2014-07-20 19:52:46 -05:00
|
|
|
|
|
|
|
// Ext returns the Terraform configuration extension of the given
|
|
|
|
// path, or a blank string if it is an invalid function.
|
|
|
|
func ext(path string) string {
|
|
|
|
if strings.HasSuffix(path, ".tf") {
|
|
|
|
return ".tf"
|
|
|
|
} else if strings.HasSuffix(path, ".tf.json") {
|
|
|
|
return ".tf.json"
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|