162 lines
3.6 KiB
Go
162 lines
3.6 KiB
Go
package ezconf
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Configuration struct {
|
|
fileLocation string
|
|
file *ini.File
|
|
}
|
|
|
|
func NewConfiguration(fileLocation string) *Configuration {
|
|
conf := new(Configuration)
|
|
conf.fileLocation = fileLocation
|
|
conf.load()
|
|
return conf
|
|
}
|
|
|
|
func (config *Configuration) getSection(sectionName string) *ini.Section {
|
|
section, err := config.file.GetSection(sectionName)
|
|
if err != nil {
|
|
fmt.Printf("Error getting section %v in config: %v", sectionName, err)
|
|
os.Exit(1)
|
|
}
|
|
return section
|
|
}
|
|
|
|
func getChildSection(section *ini.Section, childSectionName string) (childSection *ini.Section, err error) {
|
|
childSections := section.ChildSections()
|
|
for _, childSection := range childSections {
|
|
if childSection.Name() == childSectionName {
|
|
return childSection, nil
|
|
}
|
|
}
|
|
return nil, errors.New("subsection " + childSectionName + " not found")
|
|
}
|
|
|
|
func (config *Configuration) GetKey(path string) *ini.Key {
|
|
splitPaths := strings.Split(path, ".")
|
|
if len(splitPaths) == 1 {
|
|
splitPaths = append([]string{"."}, splitPaths...)
|
|
}
|
|
section := config.getSection(splitPaths[0])
|
|
var key *ini.Key
|
|
var err error
|
|
if len(splitPaths) > 2 {
|
|
tempSection := section
|
|
for i := 0; i < len(splitPaths)-1; i++ {
|
|
tempSection, err = getChildSection(tempSection, splitPaths[i])
|
|
if err != nil {
|
|
fmt.Printf("Error getting child section %v: %v", splitPaths[i], err)
|
|
os.Exit(1)
|
|
}
|
|
if len(splitPaths) == i+1 {
|
|
key, err = tempSection.GetKey(splitPaths[i+1])
|
|
if err != nil {
|
|
fmt.Printf("Error getting key %v: %v", splitPaths[i+1], err)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
key, err = section.GetKey(splitPaths[1])
|
|
if err != nil {
|
|
fmt.Printf("Error getting key %v: %v", splitPaths[1], err)
|
|
}
|
|
}
|
|
return key
|
|
}
|
|
|
|
func (config *Configuration) GetString(path string) string {
|
|
return config.GetKey(path).String()
|
|
}
|
|
|
|
func (config *Configuration) GetStrings(path string) []string {
|
|
return config.GetKey(path).Strings(",")
|
|
}
|
|
|
|
func (config *Configuration) GetInt(path string) int {
|
|
var i int
|
|
var err error
|
|
i, err = config.GetKey(path).Int()
|
|
if err != nil {
|
|
fmt.Printf("Error parsing integer: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
return i
|
|
}
|
|
|
|
func (config *Configuration) GetInts(path string) []int {
|
|
return config.GetKey(path).Ints(",")
|
|
}
|
|
|
|
func (config *Configuration) GetUInt(path string) uint {
|
|
var i uint
|
|
var err error
|
|
i, err = config.GetKey(path).Uint()
|
|
if err != nil {
|
|
fmt.Printf("Error parsing integer: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
return i
|
|
}
|
|
|
|
func (config *Configuration) GetUInts(path string) []uint {
|
|
return config.GetKey(path).Uints(",")
|
|
}
|
|
|
|
func (config *Configuration) GetFloat(path string) float64 {
|
|
var f float64
|
|
var err error
|
|
f, err = config.GetKey(path).Float64()
|
|
if err != nil {
|
|
fmt.Printf("Error parsing float: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (config *Configuration) GetFloats(path string) []float64 {
|
|
return config.GetKey(path).Float64s(",")
|
|
}
|
|
|
|
func (config *Configuration) GetBoolean(path string) bool {
|
|
var b bool
|
|
var err error
|
|
b, err = config.GetKey(path).Bool()
|
|
if err != nil {
|
|
fmt.Printf("Error parsingn bool: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (config *Configuration) GetBooleans(path string) []bool {
|
|
return config.GetKey(path).Bools(",")
|
|
}
|
|
|
|
func (config *Configuration) load() {
|
|
var err error = nil
|
|
config.file, err = ini.Load(config.fileLocation)
|
|
if err != nil {
|
|
fmt.Printf("Failed to open configuration file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func (config *Configuration) Reload() {
|
|
config.load()
|
|
}
|
|
|
|
func (config *Configuration) Save() {
|
|
err := config.file.SaveTo(config.fileLocation)
|
|
if err != nil {
|
|
fmt.Printf("Failed to save configuration file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|