diff --git a/cliconfig.go b/cliconfig.go deleted file mode 100644 index 76b00cb6c..000000000 --- a/cliconfig.go +++ /dev/null @@ -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() -} diff --git a/config.go b/config.go index c749e477d..5257c382a 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/cliconfig_unix.go b/config_unix.go similarity index 100% rename from cliconfig_unix.go rename to config_unix.go diff --git a/cliconfig_windows.go b/config_windows.go similarity index 100% rename from cliconfig_windows.go rename to config_windows.go diff --git a/main.go b/main.go index 991c6be44..acbd17b05 100644 --- a/main.go +++ b/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