-
Notifications
You must be signed in to change notification settings - Fork 20
/
tmconfig.go
129 lines (116 loc) · 3.14 KB
/
tmconfig.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
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @file: tmconfig.go
* @package: tmconfig
* @author: heiyeluren
* @desc: config parse
* @date: 2013/6/24
* @history:
* 2013/6/24 created file
*/
package tmconfig
import (
"errors"
"io/ioutil"
"path"
"strconv"
"strings"
)
type Config struct {
data map[string]string
}
func NewConfig() *Config {
return &Config{data: make(map[string]string)}
}
const emptyRunes = " \r\t\v"
func (this *Config) Load(configFile string) error {
stream, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.New("cannot load config file")
}
content := string(stream)
lines := strings.Split(content, "\n")
for _, line := range lines {
line = strings.Trim(line, emptyRunes)
if line == "" || line[0] == '#' {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
for i, part := range parts {
parts[i] = strings.Trim(part, emptyRunes)
}
this.data[parts[0]] = parts[1]
} else {
// 判断并处理include条目,load相应的config文件
includes := strings.SplitN(parts[0], " ", 2)
if len(includes) == 2 && strings.EqualFold(includes[0], "include") {
// 拼解新包含config文件的path
confDir := path.Dir(configFile)
newConfName := strings.Trim(includes[1], emptyRunes)
newConfPath := path.Join(confDir, newConfName)
// 载入include的config文件,调用Load自身
err := this.Load(newConfPath)
if err != nil {
return errors.New("load include config file failed")
}
continue
} else {
return errors.New("invalid config file syntax")
}
}
}
return nil
}
func (this *Config) GetAll() map[string]string {
return this.data
}
func (this *Config) Get(key string) string {
if value, ok := this.data[key]; ok {
return value
}
return ""
}
func (this *Config) GetInt(key string) int {
value := this.Get(key)
if value == "" {
return 0
}
result, err := strconv.Atoi(value)
if err != nil {
return 0
}
return result
}
func (this *Config) GetInt64(key string) int64 {
value := this.Get(key)
if value == "" {
return 0
}
result, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0
}
return result
}
func (this *Config) GetSlice(key string, separator string) []string {
slice := []string{}
value := this.Get(key)
if value != "" {
for _, part := range strings.Split(value, separator) {
slice = append(slice, strings.Trim(part, emptyRunes))
}
}
return slice
}
func (this *Config) GetSliceInt(key string, separator string) []int {
slice := this.GetSlice(key, separator)
results := []int{}
for _, part := range slice {
result, err := strconv.Atoi(part)
if err != nil {
continue
}
results = append(results, result)
}
return results
}