Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. // +build linux
  2.  
  3. package libconf
  4.  
  5. import "path/filepath"
  6. import "os"
  7. import "bytes"
  8.  
  9. func userJoin(rest string) string {
  10. userp := os.Getenv("HOME")
  11. return filepath.Join(userp, rest)
  12. }
  13.  
  14. func UserConfigDir() string {
  15. return userJoin(".config/")
  16. }
  17.  
  18. func UserLocalDir() string {
  19. return userJoin(".local/share/")
  20. }
  21.  
  22. func UserCacheDir() string {
  23. return userJoin(".cache/")
  24. }
  25.  
  26. func FromUserDirJoin(rest string) string {
  27. return userJoin(rest)
  28. }
  29.  
  30. const (
  31. userConfDirs string = "$HOME/.config/:/etc/xdg/"
  32. userCacheDirs string = "$HOME/.cache/"
  33. userDataDirs string = "$HOME/local/share/:/usr/local/share/:/usr/share/"
  34. )
  35.  
  36. func UserConfDirs() string {
  37. if paths := os.Getenv("XDG_CONFIG_DIRS"); paths != "" {
  38. return paths
  39. }
  40. return os.ExpandEnv(userConfDirs)
  41. }
  42.  
  43. func UserCacheDirs() string {
  44. if paths := os.Getenv("XDG_CACHE_DIRS"); paths != "" {
  45. return paths
  46. }
  47. return os.ExpandEnv(userCacheDirs)
  48. }
  49.  
  50. func UserDataDirs() string {
  51. if paths := os.Getenv("XDG_DATA_DIRS"); paths != "" {
  52. return paths
  53. }
  54. return os.ExpandEnv(userDataDirs)
  55. }
  56.  
  57. func isStatOk(path string) bool {
  58. stat, err := os.Stat(path)
  59. return stat != nil && err == nil
  60. }
  61.  
  62. func byteSearch(paths string, file string) string {
  63. buf := bytes.Split([]byte(paths), []byte(":"))
  64. for _, n := range buf {
  65. conf := filepath.Join(string(n), file)
  66. if isStatOk(conf) {
  67. return conf
  68. }
  69. }
  70. return ""
  71. }
  72.  
  73. func ConfFileSearch(path string) string {
  74. result := byteSearch(UserConfDirs(), path)
  75. return result
  76. }
  77.  
  78. func CacheFileSearch(path string) string {
  79. result := byteSearch(UserCacheDirs(), path)
  80. return result
  81. }
  82.  
  83. func DataFileSearch(path string) string {
  84. result := byteSearch(UserDataDirs(), path)
  85. return result
  86. }
  87.  
  88. const (
  89. table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  90. )
  91.  
  92. func RandomFileName(name string) string {
  93. keys := []rune(table)
  94. rand.Shuffle(len(keys), func(i, j int) { keys[i], keys[j] = keys[j], keys[i] })
  95. var buf string
  96. var x int
  97. for _, r := range name {
  98. if r == 'X' {
  99. buf += string(keys[x])
  100. x++
  101. } else {
  102. buf += string(r)
  103. }
  104. }
  105. return buf
  106. }
  107.  
  108. func DoesExist(path string) bool {
  109. return isStatOk(path)
  110. }
  111.  
  112. func OpenFile(path string, opts int, mode os.FileMode) *os.File {
  113. file, err := os.OpenFile(path, opts, mode)
  114. if err != nil {
  115. return nil
  116. }
  117. return file
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement