mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-21 14:12:57 -06:00
8173cd25bb
Now that we support log line filtering (as of 0090c063
) it's good to be
a bit more fussy about what log levels are assigned to different things.
Here we make a few things that are implementation details log as DEBUG,
and prevent spurious errors from EvalValidateCount where it was returning
an empty EvalValidateError rather than nil when everything was okay.
55 lines
979 B
Go
55 lines
979 B
Go
// +build darwin freebsd linux netbsd openbsd
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"log"
|
|
"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 != "" {
|
|
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
|
|
}
|