terraform/config/loader.go

225 lines
5.0 KiB
Go
Raw Normal View History

package config
2014-07-12 05:15:09 +02:00
import (
"encoding/json"
"fmt"
2014-07-21 02:52:46 +02:00
"io"
"os"
2014-07-12 05:15:09 +02:00
"path/filepath"
2014-07-21 02:52:46 +02:00
"sort"
"strings"
"github.com/hashicorp/hcl"
2014-07-12 05:15:09 +02:00
)
// ErrNoConfigsFound is the error returned by LoadDir if no
// Terraform configuration files were found in the given directory.
type ErrNoConfigsFound struct {
Dir string
}
func (e ErrNoConfigsFound) Error() string {
return fmt.Sprintf(
"No Terraform configuration files found in directory: %s",
e.Dir)
}
// LoadJSON loads a single Terraform configuration from a given JSON document.
//
// The document must be a complete Terraform configuration. This function will
// NOT try to load any additional modules so only the given document is loaded.
func LoadJSON(raw json.RawMessage) (*Config, error) {
obj, err := hcl.Parse(string(raw))
if err != nil {
return nil, fmt.Errorf(
"Error parsing JSON document as HCL: %s", err)
}
// Start building the result
hclConfig := &hclConfigurable{
2015-11-08 01:53:42 +01:00
Root: obj,
}
return hclConfig.Config()
}
// LoadFile 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.
func LoadFile(path string) (*Config, error) {
importTree, err := loadTree(path)
if err != nil {
return nil, err
}
configTree, err := importTree.ConfigTree()
// Close the importTree now so that we can clear resources as quickly
// as possible.
importTree.Close()
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
2014-07-21 02:54:16 +02:00
// directory and appends them together.
//
// Special files known as "override files" can also be present, which
// are merged into the loaded configuration. That is, the non-override
// files are loaded first to create the configuration. Then, the overrides
// are merged into the configuration to create the final configuration.
//
// Files are loaded in lexical order.
2014-07-21 02:52:46 +02:00
func LoadDir(root string) (*Config, error) {
2014-09-27 00:49:51 +02:00
files, overrides, err := dirFiles(root)
2014-07-28 17:34:43 +02:00
if err != nil {
return nil, err
}
2014-07-21 02:52:46 +02:00
if len(files) == 0 {
return nil, &ErrNoConfigsFound{Dir: root}
}
// Determine the absolute path to the directory.
rootAbs, err := filepath.Abs(root)
if err != nil {
return nil, err
}
2014-07-12 05:15:09 +02:00
var result *Config
2014-07-21 02:52:46 +02: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 {
c, err := LoadFile(f)
2014-07-12 05:15:09 +02:00
if err != nil {
return nil, err
}
if result != nil {
result, err = Append(result, c)
2014-07-12 05:15:09 +02:00
if err != nil {
return nil, err
}
} else {
result = c
}
}
2014-07-21 02:52:46 +02:00
// Load all the overrides, and merge them into the config
for _, f := range overrides {
c, err := LoadFile(f)
2014-07-21 02:52:46 +02:00
if err != nil {
return nil, err
}
result, err = Merge(result, c)
if err != nil {
return nil, err
}
}
// Mark the directory
result.Dir = rootAbs
2014-07-12 05:15:09 +02:00
return result, nil
}
2014-07-21 02:52:46 +02:00
2014-09-27 00:49:51 +02:00
// IsEmptyDir returns true if the directory given has no Terraform
// configuration files.
func IsEmptyDir(root string) (bool, error) {
if _, err := os.Stat(root); err != nil && os.IsNotExist(err) {
return true, nil
}
2014-09-27 00:49:51 +02:00
fs, os, err := dirFiles(root)
if err != nil {
return false, err
}
return len(fs) == 0 && len(os) == 0, nil
}
2014-07-21 02:52:46 +02: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 ""
}
}
2014-09-27 00:49:51 +02:00
func dirFiles(dir string) ([]string, []string, error) {
f, err := os.Open(dir)
if err != nil {
return nil, nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, nil, err
}
if !fi.IsDir() {
return nil, nil, fmt.Errorf(
"configuration path must be a directory: %s",
dir)
}
var files, overrides []string
err = nil
for err != io.EOF {
var fis []os.FileInfo
fis, err = f.Readdir(128)
if err != nil && err != io.EOF {
return nil, 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 == "" || isIgnoredFile(name) {
2014-09-27 00:49:51 +02:00
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(dir, name)
if override {
overrides = append(overrides, path)
} else {
files = append(files, path)
}
}
}
return files, overrides, nil
}
// isIgnoredFile returns true or false depending on whether the
// provided file name is a file that should be ignored.
func isIgnoredFile(name string) bool {
return strings.HasPrefix(name, ".") || // Unix-like hidden files
strings.HasSuffix(name, "~") || // vim
2015-10-08 14:48:04 +02:00
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
}