/
ini.go
42 lines (37 loc) · 887 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
package ini
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/go-ini/ini"
)
type Parser struct {
FileName string
}
func (i *Parser) Unmarshal(p []byte, v interface{}) error {
filePath, _ := filepath.Abs(i.FileName)
result := map[string]map[string]string{}
cfg, err := ini.Load(filePath)
if err != nil {
return fmt.Errorf("Fail to read ini file: %v", err)
}
sections := cfg.Sections()
for _, s := range sections {
sectionName := s.Name()
if sectionName == "DEFAULT" {
continue
}
result[sectionName] = map[string]string{}
result[sectionName] = s.KeysHash()
}
j, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("Error trying to parse ini to json: %s", err)
}
err = yaml.Unmarshal(j, v)
if err != nil {
return fmt.Errorf("Unable to parse YAML from ini-json %s: %s", i.FileName, err)
}
return nil
}