forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_unix.go
54 lines (44 loc) · 969 Bytes
/
config_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// +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, ".packerconfig"), nil
}
func configDir() (string, error) {
dir, err := homeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".packer.d"), nil
}
func homeDir() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
log.Printf("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
}