aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/mitchellh/go-homedir/homedir.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mitchellh/go-homedir/homedir.go')
-rw-r--r--vendor/github.com/mitchellh/go-homedir/homedir.go56
1 files changed, 38 insertions, 18 deletions
diff --git a/vendor/github.com/mitchellh/go-homedir/homedir.go b/vendor/github.com/mitchellh/go-homedir/homedir.go
index 47e1f9e..fb87bef 100644
--- a/vendor/github.com/mitchellh/go-homedir/homedir.go
+++ b/vendor/github.com/mitchellh/go-homedir/homedir.go
@@ -77,33 +77,51 @@ func Expand(path string) (string, error) {
77} 77}
78 78
79func dirUnix() (string, error) { 79func dirUnix() (string, error) {
80 homeEnv := "HOME"
81 if runtime.GOOS == "plan9" {
82 // On plan9, env vars are lowercase.
83 homeEnv = "home"
84 }
85
80 // First prefer the HOME environmental variable 86 // First prefer the HOME environmental variable
81 if home := os.Getenv("HOME"); home != "" { 87 if home := os.Getenv(homeEnv); home != "" {
82 return home, nil 88 return home, nil
83 } 89 }
84 90
85 // If that fails, try getent
86 var stdout bytes.Buffer 91 var stdout bytes.Buffer
87 cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid())) 92
88 cmd.Stdout = &stdout 93 // If that fails, try OS specific commands
89 if err := cmd.Run(); err != nil { 94 if runtime.GOOS == "darwin" {
90 // If the error is ErrNotFound, we ignore it. Otherwise, return it. 95 cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
91 if err != exec.ErrNotFound { 96 cmd.Stdout = &stdout
92 return "", err 97 if err := cmd.Run(); err == nil {
98 result := strings.TrimSpace(stdout.String())
99 if result != "" {
100 return result, nil
101 }
93 } 102 }
94 } else { 103 } else {
95 if passwd := strings.TrimSpace(stdout.String()); passwd != "" { 104 cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
96 // username:password:uid:gid:gecos:home:shell 105 cmd.Stdout = &stdout
97 passwdParts := strings.SplitN(passwd, ":", 7) 106 if err := cmd.Run(); err != nil {
98 if len(passwdParts) > 5 { 107 // If the error is ErrNotFound, we ignore it. Otherwise, return it.
99 return passwdParts[5], nil 108 if err != exec.ErrNotFound {
109 return "", err
110 }
111 } else {
112 if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
113 // username:password:uid:gid:gecos:home:shell
114 passwdParts := strings.SplitN(passwd, ":", 7)
115 if len(passwdParts) > 5 {
116 return passwdParts[5], nil
117 }
100 } 118 }
101 } 119 }
102 } 120 }
103 121
104 // If all else fails, try the shell 122 // If all else fails, try the shell
105 stdout.Reset() 123 stdout.Reset()
106 cmd = exec.Command("sh", "-c", "cd && pwd") 124 cmd := exec.Command("sh", "-c", "cd && pwd")
107 cmd.Stdout = &stdout 125 cmd.Stdout = &stdout
108 if err := cmd.Run(); err != nil { 126 if err := cmd.Run(); err != nil {
109 return "", err 127 return "", err
@@ -123,14 +141,16 @@ func dirWindows() (string, error) {
123 return home, nil 141 return home, nil
124 } 142 }
125 143
144 // Prefer standard environment variable USERPROFILE
145 if home := os.Getenv("USERPROFILE"); home != "" {
146 return home, nil
147 }
148
126 drive := os.Getenv("HOMEDRIVE") 149 drive := os.Getenv("HOMEDRIVE")
127 path := os.Getenv("HOMEPATH") 150 path := os.Getenv("HOMEPATH")
128 home := drive + path 151 home := drive + path
129 if drive == "" || path == "" { 152 if drive == "" || path == "" {
130 home = os.Getenv("USERPROFILE") 153 return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE are blank")
131 }
132 if home == "" {
133 return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
134 } 154 }
135 155
136 return home, nil 156 return home, nil