dissertation-2-code/config/loader.go

43 lines
649 B
Go

package config
import (
"gopkg.in/ini.v1"
)
func LoadConfig(path string) (c Configuration, err error) {
var file *ini.File
file, err = ini.LoadSources(ini.LoadOptions{
AllowShadows: true,
AllowNonUniqueSections: true,
}, path)
if err != nil {
return
}
if s := file.Section("Host"); s != nil {
err = s.MapTo(&c.Host)
if err != nil {
return
}
}
var sections []*ini.Section
sections, err = file.SectionsByName("Peer")
for _, s := range sections {
if s == nil {
continue
}
p := Peer{}
err = s.MapTo(&p)
if err != nil {
return
}
c.Peers = append(c.Peers, p)
}
err = c.Validate()
return
}