-
Notifications
You must be signed in to change notification settings - Fork 23
/
os_1.11.go
37 lines (33 loc) · 861 Bytes
/
os_1.11.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
// +build !go1.12
package main
import (
"errors"
"os"
"runtime"
)
// UserHomeDir was introduced in Go 1.12. When we drop support for Go 1.11, we can
// lose this file.
// UserHomeDir returns the current user's home directory.
//
// On Unix, including macOS, it returns the $HOME environment variable.
// On Windows, it returns %USERPROFILE%.
// On Plan 9, it returns the $home environment variable.
func UserHomeDir() (string, error) {
env, enverr := "HOME", "$HOME"
switch runtime.GOOS {
case "windows":
env, enverr = "USERPROFILE", "%userprofile%"
case "plan9":
env, enverr = "home", "$home"
case "nacl", "android":
return "/", nil
case "darwin":
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
return "/", nil
}
}
if v := os.Getenv(env); v != "" {
return v, nil
}
return "", errors.New(enverr + " is not defined")
}