-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
79 lines (65 loc) · 1.51 KB
/
env.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
package main
import (
"flag"
"os"
"strings"
)
// This file contains environnement variables parsing related methods,
// for configuration purpose.
// parseEnv parses configuration environnement variables.
func parseEnv() {
//FIXME this is very ugly
if !isFlagSet("channels") {
parseChannelsFromEnv()
}
if !isFlagSet("server") {
*serv = parseStringFromEnv("BYTEBOT_SERVER", "localhost:6667")
}
if !isFlagSet("redis") {
*redisAddr = parseStringFromEnv("BYTEBOT_REDIS", "localhost:6379")
}
if !isFlagSet("nick") {
*nick = parseStringFromEnv("BYTEBOT_NICK", "bytebot")
}
if !isFlagSet("id") {
*id = parseStringFromEnv("BYTEBOT_ID", "irc")
}
if !isFlagSet("inbound") {
*inbound = parseStringFromEnv("BYTEBOT_INBOUND", "irc-inbound")
}
if !isFlagSet("outbound") {
*outbound = parseStringFromEnv("BYTEBOT_OUTBOUND", *id)
}
if !isFlagSet("tls") {
_, set := os.LookupEnv("BYTEBOT_TLS")
if set {
*tls = true
}
}
}
// Parses channels and sets them
func parseChannelsFromEnv() {
val, set := os.LookupEnv("BYTEBOT_CHANNELS")
if set {
channels = strings.Split(val, ",")
}
}
// Parses a string from an env variable and returns it.
func parseStringFromEnv(varName, defaultVal string) string {
val, set := os.LookupEnv(varName)
if set {
return val
}
return defaultVal
}
// This is used to check if a flag was set
// Must be called after flag.Parse()
func isFlagSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}