15 // DisableCache will disable caching of the home directory. Caching is enabled
19 var homedirCache string
20 var cacheLock sync.RWMutex
22 // Dir returns the home directory for the executing user.
24 // This uses an OS-specific method for discovering the home directory.
25 // An error is returned if a home directory cannot be detected.
26 func Dir() (string, error) {
29 cached := homedirCache
37 defer cacheLock.Unlock()
41 if runtime.GOOS == "windows" {
42 result, err = dirWindows()
44 // Unix-like system, so just assume Unix
45 result, err = dirUnix()
55 // Expand expands the path to include the home directory if the path
56 // is prefixed with `~`. If it isn't prefixed with `~`, the path is
58 func Expand(path string) (string, error) {
67 if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
68 return "", errors.New("cannot expand user-specific home dir")
76 return filepath.Join(dir, path[1:]), nil
79 // Reset clears the cache, forcing the next call to Dir to re-detect
80 // the home directory. This generally never has to be called, but can be
81 // useful in tests if you're modifying the home directory via the HOME
82 // env var or something.
85 defer cacheLock.Unlock()
89 func dirUnix() (string, error) {
91 if runtime.GOOS == "plan9" {
92 // On plan9, env vars are lowercase.
96 // First prefer the HOME environmental variable
97 if home := os.Getenv(homeEnv); home != "" {
101 var stdout bytes.Buffer
103 // If that fails, try OS specific commands
104 if runtime.GOOS == "darwin" {
105 cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
107 if err := cmd.Run(); err == nil {
108 result := strings.TrimSpace(stdout.String())
114 cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
116 if err := cmd.Run(); err != nil {
117 // If the error is ErrNotFound, we ignore it. Otherwise, return it.
118 if err != exec.ErrNotFound {
122 if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
123 // username:password:uid:gid:gecos:home:shell
124 passwdParts := strings.SplitN(passwd, ":", 7)
125 if len(passwdParts) > 5 {
126 return passwdParts[5], nil
132 // If all else fails, try the shell
134 cmd := exec.Command("sh", "-c", "cd && pwd")
136 if err := cmd.Run(); err != nil {
140 result := strings.TrimSpace(stdout.String())
142 return "", errors.New("blank output when reading home directory")
148 func dirWindows() (string, error) {
149 // First prefer the HOME environmental variable
150 if home := os.Getenv("HOME"); home != "" {
154 // Prefer standard environment variable USERPROFILE
155 if home := os.Getenv("USERPROFILE"); home != "" {
159 drive := os.Getenv("HOMEDRIVE")
160 path := os.Getenv("HOMEPATH")
162 if drive == "" || path == "" {
163 return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE are blank")