mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
c0b1761096
The extra output shouldn't be seen by the user, and is causing TFE to fail.
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
// +build !windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
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 the shell
|
|
var stdout bytes.Buffer
|
|
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
|
|
cmd.Stdout = &stdout
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
result := strings.TrimSpace(stdout.String())
|
|
if result == "" {
|
|
return "", errors.New("blank output")
|
|
}
|
|
|
|
return result, nil
|
|
}
|