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.
|
// 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
|
||||||
|
|
34
main.go
34
main.go
|
@ -107,12 +107,15 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if clicfgFile != "" {
|
||||||
usrcfg, err := LoadConfig(clicfgFile)
|
usrcfg, err := LoadConfig(clicfgFile)
|
||||||
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)
|
||||||
|
@ -120,6 +123,7 @@ func wrappedMain() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if mustExist {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("File doesn't exist, but doesn't need to. Ignoring.")
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
return configFilePath, nil
|
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
|
||||||
|
|
Loading…
Reference in New Issue