ntfy/util/util.go

131 lines
2.8 KiB
Go
Raw Normal View History

2021-10-30 06:58:14 +13:00
package util
import (
2021-12-17 14:33:01 +13:00
"errors"
2021-11-09 03:24:34 +13:00
"fmt"
2021-10-30 06:58:14 +13:00
"math/rand"
"os"
2021-12-17 14:33:01 +13:00
"strings"
2021-12-08 10:03:01 +13:00
"sync"
2021-10-30 06:58:14 +13:00
"time"
)
const (
randomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
var (
2021-12-08 10:03:01 +13:00
random = rand.New(rand.NewSource(time.Now().UnixNano()))
randomMutex = sync.Mutex{}
2021-12-17 14:33:01 +13:00
errInvalidPriority = errors.New("unknown priority")
2021-10-30 06:58:14 +13:00
)
2021-12-08 05:45:15 +13:00
// FileExists checks if a file exists, and returns true if it does
2021-10-30 06:58:14 +13:00
func FileExists(filename string) bool {
stat, _ := os.Stat(filename)
return stat != nil
}
2021-12-09 16:13:59 +13:00
// InStringList returns true if needle is contained in haystack
func InStringList(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
2021-12-22 09:22:27 +13:00
// InStringListAll returns true if all needles are contained in haystack
func InStringListAll(haystack []string, needles []string) bool {
matches := 0
for _, s := range haystack {
for _, needle := range needles {
if s == needle {
matches++
}
}
}
return matches == len(needles)
}
// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
func SplitNoEmpty(s string, sep string) []string {
res := make([]string, 0)
for _, r := range strings.Split(s, sep) {
if r != "" {
res = append(res, r)
}
}
return res
}
2021-10-30 06:58:14 +13:00
// RandomString returns a random string with a given length
func RandomString(length int) string {
2021-12-08 10:03:01 +13:00
randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
defer randomMutex.Unlock()
2021-10-30 06:58:14 +13:00
b := make([]byte, length)
for i := range b {
b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
}
return string(b)
}
2021-11-09 03:24:34 +13:00
// DurationToHuman converts a duration to a human readable format
func DurationToHuman(d time.Duration) (str string) {
if d == 0 {
return "0"
}
d = d.Round(time.Second)
days := d / time.Hour / 24
if days > 0 {
str += fmt.Sprintf("%dd", days)
}
d -= days * time.Hour * 24
hours := d / time.Hour
if hours > 0 {
str += fmt.Sprintf("%dh", hours)
}
d -= hours * time.Hour
minutes := d / time.Minute
if minutes > 0 {
str += fmt.Sprintf("%dm", minutes)
}
d -= minutes * time.Minute
seconds := d / time.Second
if seconds > 0 {
str += fmt.Sprintf("%ds", seconds)
}
return
}
2021-12-17 14:33:01 +13:00
// ParsePriority parses a priority string into its equivalent integer value
2021-12-17 14:33:01 +13:00
func ParsePriority(priority string) (int, error) {
switch strings.TrimSpace(strings.ToLower(priority)) {
2021-12-17 14:33:01 +13:00
case "":
return 0, nil
case "1", "min":
return 1, nil
case "2", "low":
return 2, nil
case "3", "default":
return 3, nil
case "4", "high":
return 4, nil
case "5", "max", "urgent":
return 5, nil
default:
return 0, errInvalidPriority
}
}
2021-12-19 08:43:27 +13:00
// ExpandHome replaces "~" with the user's home directory
func ExpandHome(path string) string {
return os.ExpandEnv(strings.ReplaceAll(path, "~", "$HOME"))
}