-
Notifications
You must be signed in to change notification settings - Fork 4
/
cfgread.go
47 lines (43 loc) · 865 Bytes
/
cfgread.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
cfg map[string]string
inblacklist map[string]bool
inwhitelist map[string]bool
)
// InitCfg read cfgfile variable
func InitCfg(s string) {
cfg = make(map[string]string)
inblacklist = make(map[string]bool)
inwhitelist = make(map[string]bool)
f, err := os.Open(s)
if err != nil {
panic(fmt.Sprintf("Unable to read configuration file %s", s))
}
defer f.Close()
rd := bufio.NewReader(f)
for {
cfgline, err := rd.ReadString('\n')
if err != nil {
break
}
cfgline = strings.Trim(cfgline, " \n\r")
cfgval := strings.SplitN(cfgline, "=", 2)
if len(cfgval) < 2 {
continue
}
switch {
case cfgval[0] == "blacklist":
inblacklist[cfgval[1]] = true
case cfgval[0] == "whitelist":
inwhitelist[cfgval[1]] = true
default:
cfg[cfgval[0]] = cfgval[1]
}
}
}