2015-12-30 23:37:24 +01:00
|
|
|
// +build !windows
|
2014-08-14 18:45:58 +02:00
|
|
|
|
2019-07-31 01:54:48 +02:00
|
|
|
package cliconfig
|
2014-08-14 18:45:58 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2018-03-21 14:55:56 +01:00
|
|
|
"os/user"
|
2014-08-14 18:45:58 +02:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func configFile() (string, error) {
|
2014-09-27 21:36:13 +02:00
|
|
|
dir, err := homeDir()
|
2014-08-14 18:45:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, ".terraformrc"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configDir() (string, error) {
|
2014-09-27 21:36:13 +02:00
|
|
|
dir, err := homeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, ".terraform.d"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func homeDir() (string, error) {
|
2014-08-14 18:45:58 +02:00
|
|
|
// First prefer the HOME environmental variable
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
2017-06-12 21:04:40 +02:00
|
|
|
// FIXME: homeDir gets called from globalPluginDirs during init, before
|
|
|
|
// the logging is setup. We should move meta initializtion outside of
|
|
|
|
// init, but in the meantime we just need to silence this output.
|
|
|
|
//log.Printf("[DEBUG] Detected home directory from env var: %s", home)
|
|
|
|
|
2014-08-14 18:45:58 +02:00
|
|
|
return home, nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 14:55:56 +01:00
|
|
|
// If that fails, try build-in module
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
2014-08-14 18:45:58 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-03-21 14:55:56 +01:00
|
|
|
if user.HomeDir == "" {
|
2014-08-14 18:45:58 +02:00
|
|
|
return "", errors.New("blank output")
|
|
|
|
}
|
|
|
|
|
2018-03-21 14:55:56 +01:00
|
|
|
return user.HomeDir, nil
|
2014-08-14 18:45:58 +02:00
|
|
|
}
|