homedir.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. func dirUnix() (string, error) {
  67. // First prefer the HOME environmental variable
  68. if home := os.Getenv("HOME"); home != "" {
  69. return home, nil
  70. }
  71. // If that fails, try getent
  72. var stdout bytes.Buffer
  73. cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
  74. cmd.Stdout = &stdout
  75. if err := cmd.Run(); err != nil {
  76. // If the error is ErrNotFound, we ignore it. Otherwise, return it.
  77. if err != exec.ErrNotFound {
  78. return "", err
  79. }
  80. } else {
  81. if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
  82. // username:password:uid:gid:gecos:home:shell
  83. passwdParts := strings.SplitN(passwd, ":", 7)
  84. if len(passwdParts) > 5 {
  85. return passwdParts[5], nil
  86. }
  87. }
  88. }
  89. // If all else fails, try the shell
  90. stdout.Reset()
  91. cmd = exec.Command("sh", "-c", "cd && pwd")
  92. cmd.Stdout = &stdout
  93. if err := cmd.Run(); err != nil {
  94. return "", err
  95. }
  96. result := strings.TrimSpace(stdout.String())
  97. if result == "" {
  98. return "", errors.New("blank output when reading home directory")
  99. }
  100. return result, nil
  101. }
  102. func dirWindows() (string, error) {
  103. // First prefer the HOME environmental variable
  104. if home := os.Getenv("HOME"); home != "" {
  105. return home, nil
  106. }
  107. drive := os.Getenv("HOMEDRIVE")
  108. path := os.Getenv("HOMEPATH")
  109. home := drive + path
  110. if drive == "" || path == "" {
  111. home = os.Getenv("USERPROFILE")
  112. }
  113. if home == "" {
  114. return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
  115. }
  116. return home, nil
  117. }