forked from jessevdk/go-flags
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ini.go
202 lines (159 loc) · 3.45 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
package flags
import (
"bufio"
"fmt"
"io"
"os"
"reflect"
"strings"
)
type IniSection map[string]string
type Ini map[string]IniSection
func readFullLine(reader *bufio.Reader) (string, error) {
var line []byte
for {
l, more, err := reader.ReadLine()
if err != nil {
return "", err
}
if line == nil && !more {
return string(l), nil
}
line = append(line, l...)
if !more {
break
}
}
return string(line), nil
}
type IniError struct {
Message string
File string
LineNumber uint
}
func (x *IniError) Error() string {
return fmt.Sprintf("%s:%d: %s",
x.File,
x.LineNumber,
x.Message)
}
type IniOptions uint
const (
IniNone IniOptions = 0
IniIncludeDefaults = 1 << iota
IniIncludeComments
IniDefault = IniIncludeComments
)
func writeIni(parser *Parser, writer io.Writer, options IniOptions) {
parser.EachGroup(func(i int, group *Group) {
if i != 0 {
io.WriteString(writer, "\n")
}
fmt.Fprintf(writer, "[%s]\n", group.Name)
for _, option := range group.Options {
if option.isFunc() {
continue
}
if len(option.Field.Tag.Get("no-ini")) != 0 {
continue
}
val := option.Value
if (options&IniIncludeDefaults) == IniNone &&
reflect.DeepEqual(val, option.defaultValue) {
continue
}
if (options & IniIncludeComments) != IniNone {
fmt.Fprintf(writer, "; %s\n", option.Description)
}
switch val.Type().Kind() {
case reflect.Slice:
for idx := 0; idx < val.Len(); idx++ {
fmt.Fprintf(writer,
"%s = %s\n",
option.iniName(),
convertToString(val.Index(idx),
option.Field.Tag))
}
case reflect.Map:
for _, key := range val.MapKeys() {
fmt.Fprintf(writer,
"%s = %s:%s\n",
option.iniName(),
convertToString(key,
option.Field.Tag),
convertToString(val.MapIndex(key),
option.Field.Tag))
}
default:
fmt.Fprintf(writer,
"%s = %s\n",
option.iniName(),
convertToString(val,
option.Field.Tag))
}
}
})
}
func readIniFromFile(filename string) (Ini, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return readIni(file, filename)
}
func readIni(contents io.Reader, filename string) (Ini, error) {
ret := make(Ini)
reader := bufio.NewReader(contents)
var section IniSection
var lineno uint
for {
line, err := readFullLine(reader)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
lineno++
line = strings.TrimSpace(line)
// Skip empty lines and lines starting with ; (comments)
if len(line) == 0 || line[0] == ';' {
continue
}
if section == nil {
if line[0] != '[' || line[len(line)-1] != ']' {
return nil, &IniError{
Message: "malformed section header",
File: filename,
LineNumber: lineno,
}
}
name := strings.TrimSpace(line[1 : len(line)-1])
if len(name) == 0 {
return nil, &IniError{
Message: "empty section name",
File: filename,
LineNumber: lineno,
}
}
section = ret[name]
if section == nil {
section = make(IniSection)
ret[name] = section
}
continue
}
// Parse option here
keyval := strings.SplitN(line, "=", 2)
if len(keyval) != 2 {
return nil, &IniError{
Message: "malformed key=value",
File: filename,
LineNumber: lineno,
}
}
section[strings.TrimSpace(keyval[0])] = strings.TrimSpace(keyval[1])
}
return ret, nil
}