/
goini.go
88 lines (74 loc) · 1.95 KB
/
goini.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
package utils
import (
"fmt"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/go-ini/ini"
)
// Fatal prints the error's details if it is a libcontainer specific error type
// then exits the program with an exit status of 1.
func Fatal(err error) {
// make sure the error is written to the logger
logrus.Error(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func IsExists(file string) (ret bool, err error) {
// equivalent to Python's `if not os.path.exists(filename)`
if _, err := os.Stat(file); os.IsNotExist(err) {
return false, err
} else {
return true, nil
}
}
var logLevel string
var reqDoms []string
var cachePath string
var cacheTTL int
func LoadConfig(path string) error {
if _, err := IsExists(path); err != nil {
return fmt.Errorf("Unable to locate local configuration file. path: %s", path)
}
iniCfg, err := ini.Load(path)
if err != nil || iniCfg == nil {
return err
}
if sect, err := iniCfg.GetSection("MAIN"); sect == nil || err != nil {
logLevel = "WARN"
} else {
if ok := sect.HasKey("log-level"); ok {
logLevel = sect.Key("log-level").In("WARN", []string{"DEBUG", "INFO", "ERROR", "CRITICAL"})
}
if ok := sect.HasKey("req-doms"); ok {
var doms []string
reqdoms := strings.Split(sect.Key("req-doms").String(), ",")
for _, dom := range reqdoms {
dom = strings.TrimSpace(dom)
doms = append(doms, dom)
}
if len(doms) <= 0 {
return fmt.Errorf("Malformed req-doms in config file: %s", path)
}
reqDoms = doms
}
}
if sect, err := iniCfg.GetSection("CACHE"); sect != nil && err == nil {
if ok := sect.HasKey("cache-path"); ok {
cachePath = sect.Key("cache-path").Validate(func(in string) string {
if len(in) == 0 {
return cachePath
}
return in
})
}
}
if sect, err := iniCfg.GetSection("DNS"); sect != nil && err == nil {
if ok := sect.HasKey("cache-ttl"); ok {
if v, err := sect.Key("cache-ttl").Int(); err == nil {
cacheTTL = v
}
}
}
return nil
}