-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
183 lines (160 loc) · 3.08 KB
/
conf.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
package config
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
)
type Conf struct {
confGroups []*confGroup
defaultGroups []*Group
errors *confErrors
}
func newConf() *Conf {
return &Conf{
confGroups: make([]*confGroup, 0),
defaultGroups: make([]*Group, 0),
errors: newConfError(),
}
}
func (c *Conf) LoadConfiguration(filePath string) error {
ext := filepath.Ext(filePath)
if ext != ".conf" {
return fmt.Errorf("does not support file %s with %s", filePath, ext)
}
return c.loadConfiguration(filePath)
}
func (c *Conf) loadConfiguration(filePath string) error {
filebytes, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("read file data failed: %v", err)
}
rendered := bytes.NewReader(filebytes)
var group *confGroup
scanner := bufio.NewScanner(rendered)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
text := scanner.Text()
confline := newConfLine(text)
lineT, err := confline.parse()
if err != nil {
c.errors.appendError(err)
continue
}
switch lineT {
case lineEmpty:
continue
case lineConfGroup:
name := confline.confGroupName()
if name != "" {
group = c.getConfGroup(name)
}
case lineKeyValue:
key, value := confline.keyValue()
if key != "" && group != nil {
group.set(key, value)
}
}
}
if scanner.Err() != nil {
return err
}
if !c.errors.isNil() {
return c.errors
}
return nil
}
func (c *Conf) getConfGroup(name string) *confGroup {
for _, g := range c.confGroups {
if g.name == name {
return g
}
}
g := newConfGroup(name)
c.confGroups = append(c.confGroups, g)
return g
}
func (c *Conf) RegisterGroup(g *Group) {
for _, gro := range c.defaultGroups {
if gro.name == g.name {
gro.copy(g)
return
}
}
ng := NewGroup(g.name)
ng.copy(g)
c.defaultGroups = append(c.defaultGroups, ng)
}
// if get failed, GetString will return the empty string.
func (c *Conf) GetString(group, key string) string {
// get from groups.
for _, g := range c.confGroups {
if g.name == group {
v, ok := g.getString(key)
if !ok {
break
}
return v
}
}
// get from default group.
for _, g := range c.defaultGroups {
if g.name == group {
v, ok := g.getString(key)
if !ok {
break
}
return v
}
}
return ""
}
// if get failed, GetBool will return false.
func (c *Conf) GetBool(group, key string) bool {
// get from groups.
for _, g := range c.confGroups {
if g.name == group {
v, ok := g.getBool(key)
if !ok {
break
}
return v
}
}
// get from default groups.
for _, g := range c.defaultGroups {
if g.name == group {
v, ok := g.getBool(key)
if !ok {
break
}
return v
}
}
return false
}
// if get failed, GetInt will return -1.
func (c *Conf) GetInt(group, key string) int {
// get from groups.
for _, g := range c.confGroups {
if g.name == group {
v, ok := g.getInt(key)
if !ok {
break
}
return v
}
}
// get from default groups.
for _, g := range c.defaultGroups {
if g.name == group {
v, ok := g.getInt(key)
if !ok {
break
}
return v
}
}
return 0
}