forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.go
57 lines (44 loc) · 892 Bytes
/
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
package ini
import (
"bytes"
"fmt"
"io"
)
type File struct {
Comment string
sections []*Section
}
type Section struct {
Name string
Keys []*Key
}
type Key struct {
Name string
Value string
}
func Empty() *File {
return &File{
Comment: "",
sections: make([]*Section, 0, 10),
}
}
func (f *File) AppendSection(section *Section) {
f.sections = append(f.sections, section)
}
func (f *File) WriteTo(w io.Writer) (int64, error) {
buf := bytes.NewBuffer(nil)
if f.Comment != "" {
buf.WriteString(f.Comment + "\n")
}
for _, section := range f.sections {
buf.WriteString("[" + section.Name + "]\n")
for _, key := range section.Keys {
buf.WriteString(fmt.Sprintf("%s=%s\n", key.Name, key.Value))
}
buf.WriteString("\n")
}
return buf.WriteTo(w)
}
func (s *Section) AddKey(name, value string) {
s.Keys = append(s.Keys, &Key{Name: name, Value: value})
}