opentofu/config_unix.go
James Nugent 1e1d78329e core: use !windows instead of a list of unixes
This allows building on a wider variety of unix-a-likes without needing
to list them all explicitly - Windows is the special case here!
2015-12-30 17:37:24 -05:00

55 lines
952 B
Go

// +build !windows
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
}