Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 22c6bd1ff2d68fec2b795048d598c19ae5c0d686 Author: Jakub <i+github@always.fail> Date: Wed Mar 18 23:20:29 2020 +0000 Support JSON and YAML config formats (#1045) Signed-off-by: Jakub SokoĊ‚owski <jakub@status.im> diff --git a/bridge/config/config.go b/bridge/config/config.go index efbb8e1..ea62d7c 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -36 +37 @@ package config  import (   "bytes"   "io/ioutil" + "path/filepath"   "strings"   "sync"   "time" @@ -2407 +2418 @@ 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   } @@ -25114 +25326 @@ 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()