-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfgread.go
61 lines (55 loc) · 1.1 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
var (
cfg map[string]string
inblacklist map[string]bool
inwhitelist map[string]bool
usesyslog bool
pidfile string
)
// 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(filepath.Clean(s))
if err != nil {
panic(fmt.Sprintf("Unable to read configuration file %s", s))
}
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] == "pidfile":
pidfile = cfgval[1]
case cfgval[0] == "usesyslog":
if cfgval[1] == "yes" {
usesyslog = true
}
case cfgval[0] == "blacklist":
inblacklist[cfgval[1]] = true
case cfgval[0] == "whitelist":
inwhitelist[cfgval[1]] = true
default:
cfg[cfgval[0]] = cfgval[1]
}
}
if err := f.Close(); err != nil {
log.Print(err)
}
}