-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
117 lines (105 loc) · 2.63 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"errors"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
)
func schedule(delay time.Duration, what func()) chan struct{} {
ticker := time.NewTicker(delay * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
what()
case <-quit:
ticker.Stop()
return
}
}
}()
return quit
}
func getLoggerFromConfig(logLevel string) *logrus.Logger {
logLevel = strings.ToLower(logLevel)
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{ForceQuote: true, TimestampFormat: time.RFC3339Nano})
switch logLevel {
case "debug":
log.Level = logrus.DebugLevel
case "info":
log.Level = logrus.InfoLevel
case "warning":
log.Level = logrus.WarnLevel
case "fatal":
log.Level = logrus.FatalLevel
default:
log.Level = logrus.InfoLevel
}
return log
}
func getEnvStrOrDefault(key, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
func getEnvOrDefaultBool(key string, defaultVal bool) bool {
if value, exists := os.LookupEnv(key); exists {
b, err := strconv.ParseBool(value)
if err != nil {
log.WithField("envVar", key).WithError(err).Error("Cannot parse env variable as boolean")
return defaultVal
}
return b
}
return defaultVal
}
func validateIpOrHostname(input string) (string, error) {
// Check if input is a valid IP address
if net.ParseIP(input) != nil {
return input, nil
}
// Check if input is a valid hostname
hostnameRegex := `^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$`
matched, err := regexp.MatchString(hostnameRegex, input)
if err != nil {
return "", err
}
if matched {
return input, nil
}
return "", errors.New("input is neither a valid IP address nor a valid hostname")
}
func getEnvIp(key string) (string, error) {
v := os.Getenv(key)
return validateIpOrHostname(v)
}
func getEnvInt(key string) (int, error) {
v := os.Getenv(key)
i, err := strconv.Atoi(v)
return i, err
}
func activityToStatus(activity *discordgo.Activity) string {
switch activity.Type {
case discordgo.ActivityTypeGame:
return "Playing " + activity.Name
case discordgo.ActivityTypeStreaming:
return "Streaming " + activity.Name
case discordgo.ActivityTypeListening:
return "Listening to " + activity.Name
case discordgo.ActivityTypeWatching:
return "Watching " + activity.Name
case discordgo.ActivityTypeCustom:
return activity.Emoji.Name + " " + activity.Name
case discordgo.ActivityTypeCompeting:
return "Competing in " + activity.Name
}
return "Unknown"
}