util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11. const (
  12. randomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  13. )
  14. var (
  15. random = rand.New(rand.NewSource(time.Now().UnixNano()))
  16. randomMutex = sync.Mutex{}
  17. errInvalidPriority = errors.New("invalid priority")
  18. )
  19. // FileExists checks if a file exists, and returns true if it does
  20. func FileExists(filename string) bool {
  21. stat, _ := os.Stat(filename)
  22. return stat != nil
  23. }
  24. // InStringList returns true if needle is contained in haystack
  25. func InStringList(haystack []string, needle string) bool {
  26. for _, s := range haystack {
  27. if s == needle {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. // InStringListAll returns true if all needles are contained in haystack
  34. func InStringListAll(haystack []string, needles []string) bool {
  35. matches := 0
  36. for _, s := range haystack {
  37. for _, needle := range needles {
  38. if s == needle {
  39. matches++
  40. }
  41. }
  42. }
  43. return matches == len(needles)
  44. }
  45. // InIntList returns true if needle is contained in haystack
  46. func InIntList(haystack []int, needle int) bool {
  47. for _, s := range haystack {
  48. if s == needle {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. // SplitNoEmpty splits a string using strings.Split, but filters out empty strings
  55. func SplitNoEmpty(s string, sep string) []string {
  56. res := make([]string, 0)
  57. for _, r := range strings.Split(s, sep) {
  58. if r != "" {
  59. res = append(res, r)
  60. }
  61. }
  62. return res
  63. }
  64. // RandomString returns a random string with a given length
  65. func RandomString(length int) string {
  66. randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
  67. defer randomMutex.Unlock()
  68. b := make([]byte, length)
  69. for i := range b {
  70. b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
  71. }
  72. return string(b)
  73. }
  74. // DurationToHuman converts a duration to a human readable format
  75. func DurationToHuman(d time.Duration) (str string) {
  76. if d == 0 {
  77. return "0"
  78. }
  79. d = d.Round(time.Second)
  80. days := d / time.Hour / 24
  81. if days > 0 {
  82. str += fmt.Sprintf("%dd", days)
  83. }
  84. d -= days * time.Hour * 24
  85. hours := d / time.Hour
  86. if hours > 0 {
  87. str += fmt.Sprintf("%dh", hours)
  88. }
  89. d -= hours * time.Hour
  90. minutes := d / time.Minute
  91. if minutes > 0 {
  92. str += fmt.Sprintf("%dm", minutes)
  93. }
  94. d -= minutes * time.Minute
  95. seconds := d / time.Second
  96. if seconds > 0 {
  97. str += fmt.Sprintf("%ds", seconds)
  98. }
  99. return
  100. }
  101. // ParsePriority parses a priority string into its equivalent integer value
  102. func ParsePriority(priority string) (int, error) {
  103. switch strings.TrimSpace(strings.ToLower(priority)) {
  104. case "":
  105. return 0, nil
  106. case "1", "min":
  107. return 1, nil
  108. case "2", "low":
  109. return 2, nil
  110. case "3", "default":
  111. return 3, nil
  112. case "4", "high":
  113. return 4, nil
  114. case "5", "max", "urgent":
  115. return 5, nil
  116. default:
  117. return 0, errInvalidPriority
  118. }
  119. }
  120. // PriorityString converts a priority number to a string
  121. func PriorityString(priority int) (string, error) {
  122. switch priority {
  123. case 0:
  124. return "default", nil
  125. case 1:
  126. return "min", nil
  127. case 2:
  128. return "low", nil
  129. case 3:
  130. return "default", nil
  131. case 4:
  132. return "high", nil
  133. case 5:
  134. return "max", nil
  135. default:
  136. return "", errInvalidPriority
  137. }
  138. }
  139. // ExpandHome replaces "~" with the user's home directory
  140. func ExpandHome(path string) string {
  141. return os.ExpandEnv(strings.ReplaceAll(path, "~", "$HOME"))
  142. }
  143. // ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://
  144. func ShortTopicURL(s string) string {
  145. return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
  146. }