diff options
author | Jakub <i+github@always.fail> | 2020-03-18 23:20:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-18 23:20:29 +0100 |
commit | 6b017b226a9944940158331b7ff51abc6945b119 (patch) | |
tree | 36d506f5180db2d85f0aef49edd1fd9549288c5d | |
parent | 9e3bd7398cd153637f084cf4b5e6f01e567b491c (diff) | |
download | matterbridge-msglm-6b017b226a9944940158331b7ff51abc6945b119.tar.gz matterbridge-msglm-6b017b226a9944940158331b7ff51abc6945b119.tar.bz2 matterbridge-msglm-6b017b226a9944940158331b7ff51abc6945b119.zip |
Support JSON and YAML config formats (#1045)
Signed-off-by: Jakub SokoĊowski <jakub@status.im>
-rw-r--r-- | bridge/config/config.go | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/bridge/config/config.go b/bridge/config/config.go index efbb8e1f..ea62d7cc 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -3,6 +3,7 @@ package config import ( "bytes" "io/ioutil" + "path/filepath" "strings" "sync" "time" @@ -240,7 +241,8 @@ func NewConfig(rootLogger *logrus.Logger, cfgfile string) Config { logger.Fatalf("Failed to read configuration file: %#v", err) } - mycfg := newConfigFromString(logger, input) + cfgtype := detectConfigType(cfgfile) + mycfg := newConfigFromString(logger, input, cfgtype) if mycfg.cv.General.MediaDownloadSize == 0 { mycfg.cv.General.MediaDownloadSize = 1000000 } @@ -251,14 +253,26 @@ func NewConfig(rootLogger *logrus.Logger, cfgfile string) Config { return mycfg } +// detectConfigType detects JSON and YAML formats, defaults to TOML. +func detectConfigType(cfgfile string) string { + fileExt := filepath.Ext(cfgfile) + switch fileExt { + case ".json": + return "json" + case ".yaml", ".yml": + return "yaml" + } + return "toml" +} + // NewConfigFromString instantiates a new configuration based on the specified string. func NewConfigFromString(rootLogger *logrus.Logger, input []byte) Config { logger := rootLogger.WithFields(logrus.Fields{"prefix": "config"}) - return newConfigFromString(logger, input) + return newConfigFromString(logger, input, "toml") } -func newConfigFromString(logger *logrus.Entry, input []byte) *config { - viper.SetConfigType("toml") +func newConfigFromString(logger *logrus.Entry, input []byte, cfgtype string) *config { + viper.SetConfigType(cfgtype) viper.SetEnvPrefix("matterbridge") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) viper.AutomaticEnv() |