main: clean up the code surrounding config file loading

This commit is contained in:
Mitchell Hashimoto 2014-08-19 10:58:23 -07:00
parent 2edc7a5c9b
commit 2e2f6bf0f4
5 changed files with 32 additions and 30 deletions

View File

@ -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()
}

View File

@ -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. // LoadConfig loads the CLI configuration from ".terraformrc" files.
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {
// Read the HCL file and prepare for parsing // Read the HCL file and prepare for parsing

44
main.go
View File

@ -107,19 +107,23 @@ func wrappedMain() int {
HelpWriter: os.Stdout, HelpWriter: os.Stdout,
} }
// Load the configuration file if we have one, that can be used to
// define extra providers and provisioners.
clicfgFile, err := cliConfigFile() clicfgFile, err := cliConfigFile()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err) fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err)
return 1 return 1
} }
usrcfg, err := LoadConfig(clicfgFile) if clicfgFile != "" {
if err != nil { usrcfg, err := LoadConfig(clicfgFile)
fmt.Fprintf(os.Stderr, "Error loading CLI configuration: \n\n%s\n", err) if err != nil {
return 1 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... // Initialize the TFConfig settings for the commands...
ContextOpts.Providers = config.ProviderFactories() ContextOpts.Providers = config.ProviderFactories()
@ -143,27 +147,25 @@ func cliConfigFile() (string, error) {
mustExist = false mustExist = false
if err != nil { 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) f, err := os.Open(configFilePath)
if err != nil { if err == nil {
if !os.IsNotExist(err) { f.Close()
return "", err return configFilePath, nil
}
if mustExist {
return "", err
}
log.Println("File doesn't exist, but doesn't need to. Ignoring.")
return "", 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 // copyOutput uses output prefixes to determine whether data on stdout