-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.go
203 lines (176 loc) · 3.93 KB
/
ini.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package conf
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
//DefaultSection define default section name
const DefaultSection = "default"
//IniConfig is ini file config
type IniConfig struct {
dict map[string]string
}
var (
_ Config = new(IniConfig)
_ Parser = new(IniConfig)
)
//NewIniConfig return a empty Config
func NewIniConfig() *IniConfig {
return &IniConfig{dict: map[string]string{}}
}
//ParseFile open and read the ini file
func (ini *IniConfig) ParseFile(file string) (Config, error) {
bys, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return ini.Parse(bytes.NewBuffer(bys))
}
//Parse ini data from reader
func (ini *IniConfig) Parse(reader io.Reader) (Config, error) {
err := ini.parse(DefaultSection, 0, reader)
if err != nil {
return nil, err
}
//print all configure value to debug
if ini.DefaultBool("print_all_conf", false) {
for key, val := range ini.dict {
fmt.Println(key, "=", val)
}
}
return ini, nil
}
func (ini *IniConfig) parse(section string, deep int, reader io.Reader) error {
if deep > 20 {
return ErrLoadLoop
}
scaner := bufio.NewScanner(reader)
for scaner.Scan() {
line := strings.Trim(scaner.Text(), " ")
if len(line) == 0 {
continue
}
if line[0] == '#' || line[0] == ';' {
continue
}
if line[0] == '@' {
bys, err := runOrder(ini, line)
if err != nil && err != ErrValueEmpty {
return err
} else if err == nil {
err = ini.parse(section, deep+1, bytes.NewBuffer(bys))
if err != nil {
return err
}
}
continue
}
if line[0] == '[' {
if line[len(line)-1] != ']' {
return fmt.Errorf("unclose section '%v'", line)
}
section = strings.Trim(line, "[]")
continue
}
kv := strings.SplitN(line, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("no assign value '%v'", line)
}
key := kv[0]
if section != DefaultSection {
key = section + "." + key
}
ini.dict[key] = assignVariable(ini, kv[1])
}
return nil
}
func (ini *IniConfig) Exists(key string) bool {
_, ok := ini.dict[key]
return ok || os.Getenv(key) != ""
}
//String get value, key is default section key or such as 'section.key' format key
func (ini *IniConfig) String(key string) string {
str, ok := ini.dict[key]
if ok {
return str
}
return os.Getenv(key)
}
func (ini *IniConfig) Strings(key string) []string {
str := ini.String(key)
if str == "" {
return []string{}
}
return strings.Split(str, ",")
}
func (ini *IniConfig) Int(key string) (int, error) {
return strconv.Atoi(ini.String(key))
}
func (ini *IniConfig) Int64(key string) (int64, error) {
return strconv.ParseInt(ini.String(key), 10, 64)
}
func (ini *IniConfig) Bool(key string) (bool, error) {
return strconv.ParseBool(ini.String(key))
}
func (ini *IniConfig) Float(key string) (float64, error) {
return strconv.ParseFloat(ini.String(key), 64)
}
func (ini *IniConfig) DefaultString(key string, defaultVal string) string {
str := ini.String(key)
if str == "" {
return defaultVal
}
return str
}
func (ini *IniConfig) DefaultStrings(key string, defaultVal []string) []string {
strs := ini.Strings(key)
if len(strs) == 0 {
return defaultVal
}
return strs
}
func (ini *IniConfig) DefaultInt(key string, defaultVal int) int {
if !ini.Exists(key) {
return defaultVal
}
val, err := ini.Int(key)
if err != nil {
return defaultVal
}
return val
}
func (ini *IniConfig) DefaultInt64(key string, defaultVal int64) int64 {
if !ini.Exists(key) {
return defaultVal
}
val, err := ini.Int64(key)
if err != nil {
return defaultVal
}
return val
}
func (ini *IniConfig) DefaultBool(key string, defaultVal bool) bool {
if !ini.Exists(key) {
return defaultVal
}
val, err := ini.Bool(key)
if err != nil {
return defaultVal
}
return val
}
func (ini *IniConfig) DefaultFloat(key string, defaultVal float64) float64 {
if !ini.Exists(key) {
return defaultVal
}
val, err := ini.Float(key)
if err != nil {
return defaultVal
}
return val
}