opentofu/config.go

143 lines
3.9 KiB
Go
Raw Normal View History

//go:generate go run ./scripts/generate-plugins.go
2014-05-25 19:39:44 -05:00
package main
import (
2014-08-19 11:56:50 -05:00
"fmt"
"io/ioutil"
"log"
"os"
2014-08-19 11:56:50 -05:00
"github.com/hashicorp/hcl"
"github.com/hashicorp/terraform/command"
2014-05-25 19:39:44 -05:00
)
const pluginCacheDirEnvVar = "TF_PLUGIN_CACHE_DIR"
2014-05-25 19:39:44 -05:00
// Config is the structure of the configuration for the Terraform CLI.
//
// This is not the configuration for Terraform itself. That is in the
// "config" package.
type Config struct {
2014-07-09 16:47:37 -05:00
Providers map[string]string
Provisioners map[string]string
2014-10-13 16:05:29 -05:00
DisableCheckpoint bool `hcl:"disable_checkpoint"`
DisableCheckpointSignature bool `hcl:"disable_checkpoint_signature"`
// If set, enables local caching of plugins in this directory to
// avoid repeatedly re-downloading over the Internet.
PluginCacheDir string `hcl:"plugin_cache_dir"`
2014-05-25 19:39:44 -05:00
}
// BuiltinConfig is the built-in defaults for the configuration. These
// can be overridden by user configurations.
var BuiltinConfig Config
// PluginOverrides are paths that override discovered plugins, set from
// the config file.
var PluginOverrides command.PluginOverrides
2014-07-03 14:01:20 -05:00
// ConfigFile returns the default path to the configuration file.
//
// On Unix-like systems this is the ".terraformrc" file in the home directory.
// On Windows, this is the "terraform.rc" file in the application data
// directory.
func ConfigFile() (string, error) {
2014-08-21 00:24:35 -05:00
return configFile()
}
// ConfigDir returns the configuration directory for Terraform.
func ConfigDir() (string, error) {
return configDir()
}
2014-05-25 19:39:44 -05:00
// LoadConfig loads the CLI configuration from ".terraformrc" files.
func LoadConfig(path string) (*Config, error) {
2014-08-19 11:56:50 -05:00
// Read the HCL file and prepare for parsing
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf(
"Error reading %s: %s", path, err)
2014-05-25 19:39:44 -05:00
}
2014-08-19 11:56:50 -05:00
// Parse it
obj, err := hcl.Parse(string(d))
2014-05-25 19:39:44 -05:00
if err != nil {
2014-08-19 11:56:50 -05:00
return nil, fmt.Errorf(
"Error parsing %s: %s", path, err)
2014-05-25 19:39:44 -05:00
}
// Build up the result
var result Config
2014-08-19 11:56:50 -05:00
if err := hcl.DecodeObject(&result, obj); err != nil {
2014-05-25 19:39:44 -05:00
return nil, err
}
// Replace all env vars
for k, v := range result.Providers {
result.Providers[k] = os.ExpandEnv(v)
}
for k, v := range result.Provisioners {
result.Provisioners[k] = os.ExpandEnv(v)
}
if result.PluginCacheDir != "" {
result.PluginCacheDir = os.ExpandEnv(result.PluginCacheDir)
}
2014-05-25 19:39:44 -05:00
return &result, nil
}
// EnvConfig returns a Config populated from environment variables.
//
// Any values specified in this config should override those set in the
// configuration file.
func EnvConfig() *Config {
config := &Config{}
if envPluginCacheDir := os.Getenv(pluginCacheDirEnvVar); envPluginCacheDir != "" {
// No Expandenv here, because expanding environment variables inside
// an environment variable would be strange and seems unnecessary.
// (User can expand variables into the value while setting it using
// standard shell features.)
config.PluginCacheDir = envPluginCacheDir
}
return config
}
// Merge merges two configurations and returns a third entirely
// new configuration with the two merged.
func (c1 *Config) Merge(c2 *Config) *Config {
var result Config
result.Providers = make(map[string]string)
2014-07-09 16:47:37 -05:00
result.Provisioners = make(map[string]string)
for k, v := range c1.Providers {
result.Providers[k] = v
}
for k, v := range c2.Providers {
if v1, ok := c1.Providers[k]; ok {
log.Printf("[INFO] Local %s provider configuration '%s' overrides '%s'", k, v, v1)
}
result.Providers[k] = v
}
2014-07-09 16:47:37 -05:00
for k, v := range c1.Provisioners {
result.Provisioners[k] = v
}
for k, v := range c2.Provisioners {
if v1, ok := c1.Provisioners[k]; ok {
log.Printf("[INFO] Local %s provisioner configuration '%s' overrides '%s'", k, v, v1)
}
2014-07-09 16:47:37 -05:00
result.Provisioners[k] = v
}
result.DisableCheckpoint = c1.DisableCheckpoint || c2.DisableCheckpoint
result.DisableCheckpointSignature = c1.DisableCheckpointSignature || c2.DisableCheckpointSignature
result.PluginCacheDir = c1.PluginCacheDir
if result.PluginCacheDir == "" {
result.PluginCacheDir = c2.PluginCacheDir
}
return &result
}