mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-19 09:32:35 +01:00
Clean up config loading. (#561)
This commit is contained in:
parent
d2a1dc792f
commit
f2703979a4
@ -2,7 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -183,42 +183,38 @@ type Config struct {
|
|||||||
func NewConfig(cfgfile string) *Config {
|
func NewConfig(cfgfile string) *Config {
|
||||||
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false})
|
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: false})
|
||||||
flog := log.WithFields(log.Fields{"prefix": "config"})
|
flog := log.WithFields(log.Fields{"prefix": "config"})
|
||||||
var cfg ConfigValues
|
|
||||||
viper.SetConfigType("toml")
|
|
||||||
viper.SetConfigFile(cfgfile)
|
viper.SetConfigFile(cfgfile)
|
||||||
viper.SetEnvPrefix("matterbridge")
|
input, err := getFileContents(cfgfile)
|
||||||
viper.AddConfigPath(".")
|
|
||||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
|
||||||
viper.AutomaticEnv()
|
|
||||||
f, err := os.Open(cfgfile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
err = viper.ReadConfig(f)
|
mycfg := NewConfigFromString(input)
|
||||||
if err != nil {
|
if mycfg.ConfigValues.General.MediaDownloadSize == 0 {
|
||||||
log.Fatal(err)
|
mycfg.ConfigValues.General.MediaDownloadSize = 1000000
|
||||||
}
|
|
||||||
err = viper.Unmarshal(&cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("blah", err)
|
|
||||||
}
|
|
||||||
mycfg := new(Config)
|
|
||||||
mycfg.v = viper.GetViper()
|
|
||||||
if cfg.General.MediaDownloadSize == 0 {
|
|
||||||
cfg.General.MediaDownloadSize = 1000000
|
|
||||||
}
|
}
|
||||||
viper.WatchConfig()
|
viper.WatchConfig()
|
||||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||||
flog.Println("Config file changed:", e.Name)
|
flog.Println("Config file changed:", e.Name)
|
||||||
})
|
})
|
||||||
|
|
||||||
mycfg.ConfigValues = &cfg
|
|
||||||
return mycfg
|
return mycfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFileContents(filename string) ([]byte, error) {
|
||||||
|
input, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return []byte(nil), err
|
||||||
|
}
|
||||||
|
return input, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewConfigFromString(input []byte) *Config {
|
func NewConfigFromString(input []byte) *Config {
|
||||||
var cfg ConfigValues
|
var cfg ConfigValues
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
|
viper.SetEnvPrefix("matterbridge")
|
||||||
|
viper.AddConfigPath(".")
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
||||||
|
viper.AutomaticEnv()
|
||||||
err := viper.ReadConfig(bytes.NewBuffer(input))
|
err := viper.ReadConfig(bytes.NewBuffer(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user