openpdu/src/config/config.go

55 lines
1.3 KiB
Go

package config
import (
"log"
"github.com/spf13/viper"
)
func init() {
viper.SetConfigName("openpdu") // name of config file (without extension)
viper.SetConfigType("yaml")
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Printf("Can't read config file: %s \n", err.Error())
}
// temporary disabled because it screwsup the config on save
// viper.OnConfigChange(func(e fsnotify.Event) {
// //The Viper configuration has changed to perform the responding operation
// fmt.Println("Config file changed:", e.Name)
// events.FireEvent("config_changed")
// })
// viper.WatchConfig()
}
func SetDefault(k string, v interface{}) {
viper.SetDefault(k, v)
}
func GetString(k string) string {
return viper.GetString(k)
}
func GetInt(k string) int {
return viper.GetInt(k)
}
func GetBool(k string) bool {
return viper.GetBool(k)
}
func Set(k string, v interface{}) {
viper.Set(k, v)
}
func WriteConfig() {
viper.WriteConfig()
}
func ConfigFileUsed() string {
return viper.ConfigFileUsed()
}