main: clean up the code surrounding config file loading
This commit is contained in:
parent
2edc7a5c9b
commit
2e2f6bf0f4
|
@ -1,9 +0,0 @@
|
|||
package main
|
||||
|
||||
// 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) {
|
||||
return configFile()
|
||||
}
|
|
@ -45,6 +45,15 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return configFile()
|
||||
}
|
||||
|
||||
// LoadConfig loads the CLI configuration from ".terraformrc" files.
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
// Read the HCL file and prepare for parsing
|
||||
|
|
44
main.go
44
main.go
|
@ -107,19 +107,23 @@ func wrappedMain() int {
|
|||
HelpWriter: os.Stdout,
|
||||
}
|
||||
|
||||
// Load the configuration file if we have one, that can be used to
|
||||
// define extra providers and provisioners.
|
||||
clicfgFile, err := cliConfigFile()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
usrcfg, err := LoadConfig(clicfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err)
|
||||
return 1
|
||||
}
|
||||
if clicfgFile != "" {
|
||||
usrcfg, err := LoadConfig(clicfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
config = *config.Merge(usrcfg)
|
||||
config = *config.Merge(usrcfg)
|
||||
}
|
||||
|
||||
// Initialize the TFConfig settings for the commands...
|
||||
ContextOpts.Providers = config.ProviderFactories()
|
||||
|
@ -143,27 +147,25 @@ func cliConfigFile() (string, error) {
|
|||
mustExist = false
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error detecting default CLI config file path: %s", err)
|
||||
log.Printf(
|
||||
"[ERROR] Error detecting default CLI config file path: %s",
|
||||
err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Attempting to open CLI config file: %s", configFilePath)
|
||||
log.Printf("[DEBUG] Attempting to open CLI config file: %s", configFilePath)
|
||||
f, err := os.Open(configFilePath)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if mustExist {
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Println("File doesn't exist, but doesn't need to. Ignoring.")
|
||||
return "", nil
|
||||
if err == nil {
|
||||
f.Close()
|
||||
return configFilePath, nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return configFilePath, nil
|
||||
if mustExist || !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.")
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// copyOutput uses output prefixes to determine whether data on stdout
|
||||
|
|
Loading…
Reference in New Issue