Torben Nehmer
9952343cda
- moved everything to cmd/root.go - Sanitize all paths to full absolute ones - Set a config key with the current config basedir - moved default config logging to a central location - resolve user config dir relative to config dir - change cwd to config dir
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type config struct {
|
|
DNS configDNS
|
|
Users configUsers
|
|
}
|
|
|
|
type configDNS struct {
|
|
Server string
|
|
DefaultTTL uint32
|
|
}
|
|
|
|
type configUsers struct {
|
|
ConfigDir string
|
|
}
|
|
|
|
var C config
|
|
|
|
func init() {
|
|
SetConfigDefaults()
|
|
}
|
|
|
|
func SetConfigDefaults() {
|
|
viper.SetDefault("Service.DNS.Server", "127.0.0.1:53")
|
|
viper.SetDefault("Service.DNS.DefaultTTL", 60)
|
|
viper.SetDefault("Service.Users.ConfigDir", "users/")
|
|
}
|
|
|
|
func LoadConfig() {
|
|
// Does not work with partially overrides, only GetXXX does resolve the subkeys correctly
|
|
// https://github.com/spf13/viper/issues/798
|
|
// https://github.com/spf13/viper/issues/309
|
|
// viper.UnmarshalKey("Service", &C)
|
|
C.DNS.Server = viper.GetString("Service.DNS.Server")
|
|
C.DNS.DefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL")
|
|
path := viper.GetString("Service.Users.ConfigDir")
|
|
if filepath.IsAbs(path) {
|
|
C.Users.ConfigDir = filepath.Clean(path)
|
|
} else {
|
|
C.Users.ConfigDir = filepath.Join(viper.GetString("ConfigDir"), path)
|
|
}
|
|
}
|
|
|
|
func (obj *config) PrettyPrint() string {
|
|
s, err := json.MarshalIndent(obj, "", " ")
|
|
if err != nil {
|
|
log.Fatalf("Failed to pretty print Service Config via JSON: %v", err)
|
|
}
|
|
return string(s)
|
|
}
|