cliconfig: Ignore config dir if TF_CLI_CONFIG_FILE envvar is set

When we originally introduced this environment variable it was intended to
solve for the use-case where a particular invocation of Terraform needs
a different CLI configuration than usual, such as if Terraform is being
run as part of an automated test suite or other sort of automated
situation with different needs than normal use.

However, we accidentally had it only override the original singleton CLI
config file, while leaving the CLI configuration directory still enabled.
Now we'll take the CLI configuration out of the equation too, so that only
the single specified configuration file and any other environment-sourced
settings will be included.
This commit is contained in:
Martin Atkins 2020-04-21 16:36:06 -07:00
parent 94b87e056b
commit b8856c677c

View File

@ -113,12 +113,23 @@ func LoadConfig() (*Config, tfdiags.Diagnostics) {
} }
} }
if configDir, err := ConfigDir(); err == nil { // Unless the user has specifically overridden the configuration file
if info, err := os.Stat(configDir); err == nil && info.IsDir() { // location using an environment variable, we'll also load what we find
dirConfig, dirDiags := loadConfigDir(configDir) // in the config directory. We skip the config directory when source
diags = diags.Append(dirDiags) // file override is set because we interpret the environment variable
config = config.Merge(dirConfig) // being set as an intention to ignore the default set of CLI config
// files because we're doing something special, like running Terraform
// in automation with a locally-customized configuration.
if cliConfigFileOverride() == "" {
if configDir, err := ConfigDir(); err == nil {
if info, err := os.Stat(configDir); err == nil && info.IsDir() {
dirConfig, dirDiags := loadConfigDir(configDir)
diags = diags.Append(dirDiags)
config = config.Merge(dirConfig)
}
} }
} else {
log.Printf("[DEBUG] Not reading CLI config directory because config location is overridden by environment variable")
} }
if envConfig := EnvConfig(); envConfig != nil { if envConfig := EnvConfig(); envConfig != nil {
@ -357,11 +368,7 @@ func (c1 *Config) Merge(c2 *Config) *Config {
func cliConfigFile() (string, error) { func cliConfigFile() (string, error) {
mustExist := true mustExist := true
configFilePath := os.Getenv("TF_CLI_CONFIG_FILE") configFilePath := cliConfigFileOverride()
if configFilePath == "" {
configFilePath = os.Getenv("TERRAFORM_CONFIG")
}
if configFilePath == "" { if configFilePath == "" {
var err error var err error
configFilePath, err = ConfigFile() configFilePath, err = ConfigFile()
@ -388,3 +395,11 @@ func cliConfigFile() (string, error) {
log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.") log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.")
return "", nil return "", nil
} }
func cliConfigFileOverride() string {
configFilePath := os.Getenv("TF_CLI_CONFIG_FILE")
if configFilePath == "" {
configFilePath = os.Getenv("TERRAFORM_CONFIG")
}
return configFilePath
}