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
1 changed files with 25 additions and 10 deletions

View File

@ -113,12 +113,23 @@ func LoadConfig() (*Config, tfdiags.Diagnostics) {
}
}
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)
// Unless the user has specifically overridden the configuration file
// location using an environment variable, we'll also load what we find
// in the config directory. We skip the config directory when source
// file override is set because we interpret the environment variable
// 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 {
@ -357,11 +368,7 @@ func (c1 *Config) Merge(c2 *Config) *Config {
func cliConfigFile() (string, error) {
mustExist := true
configFilePath := os.Getenv("TF_CLI_CONFIG_FILE")
if configFilePath == "" {
configFilePath = os.Getenv("TERRAFORM_CONFIG")
}
configFilePath := cliConfigFileOverride()
if configFilePath == "" {
var err error
configFilePath, err = ConfigFile()
@ -388,3 +395,11 @@ func cliConfigFile() (string, error) {
log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.")
return "", nil
}
func cliConfigFileOverride() string {
configFilePath := os.Getenv("TF_CLI_CONFIG_FILE")
if configFilePath == "" {
configFilePath = os.Getenv("TERRAFORM_CONFIG")
}
return configFilePath
}