diff --git a/config.go b/config.go index 1b2e7ced3..37c302bee 100644 --- a/config.go +++ b/config.go @@ -41,6 +41,11 @@ func ConfigFile() (string, error) { return configFile() } +// ConfigDir returns the configuration directory for Terraform. +func ConfigDir() (string, error) { + return configDir() +} + // LoadConfig loads the CLI configuration from ".terraformrc" files. func LoadConfig(path string) (*Config, error) { // Read the HCL file and prepare for parsing @@ -76,6 +81,17 @@ func (c *Config) Discover() error { return err } + // Look in the plugins directory. This will override any found + // in the current directory. + dir, err := ConfigDir() + if err != nil { + log.Printf("[ERR] Error loading config directory: %s", err) + } else { + if err := c.discover(filepath.Join(dir, "plugins")); err != nil { + return err + } + } + // Next, look in the same directory as the executable. Any conflicts // will overwrite those found in our current directory. exePath, err := osext.Executable() diff --git a/config_unix.go b/config_unix.go index ade2c0fdb..c51ea5ec4 100644 --- a/config_unix.go +++ b/config_unix.go @@ -13,7 +13,7 @@ import ( ) func configFile() (string, error) { - dir, err := configDir() + dir, err := homeDir() if err != nil { return "", err } @@ -22,6 +22,15 @@ func configFile() (string, error) { } func configDir() (string, error) { + dir, err := homeDir() + if err != nil { + return "", err + } + + return filepath.Join(dir, ".terraform.d"), nil +} + +func homeDir() (string, error) { // First prefer the HOME environmental variable if home := os.Getenv("HOME"); home != "" { log.Printf("Detected home directory from env var: %s", home) diff --git a/config_windows.go b/config_windows.go index 770c01978..ead48d4b1 100644 --- a/config_windows.go +++ b/config_windows.go @@ -16,7 +16,7 @@ var ( const CSIDL_APPDATA = 26 func configFile() (string, error) { - dir, err := configDir() + dir, err := homeDir() if err != nil { return "", err } @@ -25,6 +25,15 @@ func configFile() (string, error) { } func configDir() (string, error) { + dir, err := homeDir() + if err != nil { + return "", err + } + + return filepath.Join(dir, "terraform.d"), nil +} + +func homeDir() (string, error) { b := make([]uint16, syscall.MAX_PATH) // See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx