directory.go 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "os"
  4. "path/filepath"
  5. "runtime"
  6. )
  7. //HomeDir return user home directory
  8. func HomeDir() string {
  9. if runtime.GOOS == "windows" {
  10. return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
  11. }
  12. if h := os.Getenv("HOME"); h != "" {
  13. return h
  14. }
  15. return "/"
  16. }
  17. //HiddenFilePrefix get hidden file prefix
  18. func HiddenFilePrefix() string {
  19. switch runtime.GOOS {
  20. case "windows":
  21. return "~"
  22. default:
  23. return "."
  24. }
  25. }
  26. //CacheDir return user cache directory
  27. func CacheDir() string {
  28. switch runtime.GOOS {
  29. case "darwin":
  30. return filepath.Join(HomeDir(), "Library", "Caches")
  31. case "windows":
  32. for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
  33. if v := os.Getenv(ev); v != "" {
  34. return v
  35. }
  36. }
  37. // Worst case:
  38. return HomeDir()
  39. }
  40. if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
  41. return xdg
  42. }
  43. return filepath.Join(HomeDir(), ".cache")
  44. }