-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.go
52 lines (41 loc) · 897 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
package main
import (
"gopkg.in/ini.v1"
)
type IniValue struct {
Meta string
Value string
}
type IniSection struct {
Entries map[string]IniValue
Meta string
}
type IniDocument struct {
Sections map[string]IniSection
Filename string
}
// ini_obj["section"]["keypair"]
func readIniFile(path string) (*IniDocument, error) {
document := new(IniDocument)
document.Filename = path
document.Sections = make(map[string]IniSection)
cfg, err := ini.Load(path)
if err != nil {
return nil, err
}
for _, section := range cfg.Sections() {
name := section.Name()
document.Sections[name] = IniSection{
Meta: section.Comment,
Entries: make(map[string]IniValue),
}
for _, key := range section.Keys() {
keyName := key.Name()
document.Sections[name].Entries[keyName] = IniValue{
Value: key.Value(),
Meta: key.Comment,
}
}
}
return document, nil
}