mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-31 11:17:25 -06:00
55e6f64977
This originated in the cliconfig code to write out credentials files. The Windows implementation of this in particular was quite onerous to get right because it needs a very specific sequence of operations to avoid running into exclusive file locks, and so by factoring this out with only cosmetic modification we can avoid repeating all of that engineering effort for other atomic file writing use-cases.
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
// +build !windows
|
|
|
|
package cliconfig
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
)
|
|
|
|
func configFile() (string, error) {
|
|
dir, err := homeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(dir, ".terraformrc"), nil
|
|
}
|
|
|
|
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 != "" {
|
|
// 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)
|
|
|
|
return home, nil
|
|
}
|
|
|
|
// If that fails, try build-in module
|
|
user, err := user.Current()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if user.HomeDir == "" {
|
|
return "", errors.New("blank output")
|
|
}
|
|
|
|
return user.HomeDir, nil
|
|
}
|