homedir.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package homedir
  2. import (
  3. "bytes"
  4. "errors"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. )
  13. // DisableCache will disable caching of the home directory. Caching is enabled
  14. // by default.
  15. var DisableCache bool
  16. var homedirCache string
  17. var cacheLock sync.RWMutex
  18. // Dir returns the home directory for the executing user.
  19. //
  20. // This uses an OS-specific method for discovering the home directory.
  21. // An error is returned if a home directory cannot be detected.
  22. func Dir() (string, error) {
  23. if !DisableCache {
  24. cacheLock.RLock()
  25. cached := homedirCache
  26. cacheLock.RUnlock()
  27. if cached != "" {
  28. return cached, nil
  29. }
  30. }
  31. cacheLock.Lock()
  32. defer cacheLock.Unlock()
  33. var result string
  34. var err error
  35. if runtime.GOOS == "windows" {
  36. result, err = dirWindows()
  37. } else {
  38. // Unix-like system, so just assume Unix
  39. result, err = dirUnix()
  40. }
  41. if err != nil {
  42. return "", err
  43. }
  44. homedirCache = result
  45. return result, nil
  46. }
  47. // Expand expands the path to include the home directory if the path
  48. // is prefixed with `~`. If it isn't prefixed with `~`, the path is
  49. // returned as-is.
  50. func Expand(path string) (string, error) {
  51. if len(path) == 0 {
  52. return path, nil
  53. }
  54. if path[0] != '~' {
  55. return path, nil
  56. }
  57. if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
  58. return "", errors.New("cannot expand user-specific home dir")
  59. }
  60. dir, err := Dir()
  61. if err != nil {
  62. return "", err
  63. }
  64. return filepath.Join(dir, path[1:]), nil
  65. }
  66. // Reset clears the cache, forcing the next call to Dir to re-detect
  67. // the home directory. This generally never has to be called, but can be
  68. // useful in tests if you're modifying the home directory via the HOME
  69. // env var or something.
  70. func Reset() {
  71. cacheLock.Lock()
  72. defer cacheLock.Unlock()
  73. homedirCache = ""
  74. }
  75. func dirUnix() (string, error) {
  76. homeEnv := "HOME"
  77. if runtime.GOOS == "plan9" {
  78. // On plan9, env vars are lowercase.
  79. homeEnv = "home"
  80. }
  81. // First prefer the HOME environmental variable
  82. if home := os.Getenv(homeEnv); home != "" {
  83. return home, nil
  84. }
  85. var stdout bytes.Buffer
  86. // If that fails, try OS specific commands
  87. if runtime.GOOS == "darwin" {
  88. cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
  89. cmd.Stdout = &stdout
  90. if err := cmd.Run(); err == nil {
  91. result := strings.TrimSpace(stdout.String())
  92. if result != "" {
  93. return result, nil
  94. }
  95. }
  96. } else {
  97. cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
  98. cmd.Stdout = &stdout
  99. if err := cmd.Run(); err != nil {
  100. // If the error is ErrNotFound, we ignore it. Otherwise, return it.
  101. if err != exec.ErrNotFound {
  102. return "", err
  103. }
  104. } else {
  105. if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
  106. // username:password:uid:gid:gecos:home:shell
  107. passwdParts := strings.SplitN(passwd, ":", 7)
  108. if len(passwdParts) > 5 {
  109. return passwdParts[5], nil
  110. }
  111. }
  112. }
  113. }
  114. // If all else fails, try the shell
  115. stdout.Reset()
  116. cmd := exec.Command("sh", "-c", "cd && pwd")
  117. cmd.Stdout = &stdout
  118. if err := cmd.Run(); err != nil {
  119. return "", err
  120. }
  121. result := strings.TrimSpace(stdout.String())
  122. if result == "" {
  123. return "", errors.New("blank output when reading home directory")
  124. }
  125. return result, nil
  126. }
  127. func dirWindows() (string, error) {
  128. // First prefer the HOME environmental variable
  129. if home := os.Getenv("HOME"); home != "" {
  130. return home, nil
  131. }
  132. // Prefer standard environment variable USERPROFILE
  133. if home := os.Getenv("USERPROFILE"); home != "" {
  134. return home, nil
  135. }
  136. drive := os.Getenv("HOMEDRIVE")
  137. path := os.Getenv("HOMEPATH")
  138. home := drive + path
  139. if drive == "" || path == "" {
  140. return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE are blank")
  141. }
  142. return home, nil
  143. }