Thumbnail

rani/matterbridge.git

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

commit aab1533e779eb72a1ea7b7e88743a3c62c7a609f Author: Wim <wim@42.be> Date: Mon Sep 09 23:48:00 2019 +0000 Do configuration validation on start-up. Fixes #888 (#889) Fail if: * we don't have any gateways configured * we have gateways configured but with non-existing bridge configuration * we have gateways configured without any configuration diff --git a/bridge/config/config.go b/bridge/config/config.go index cf6872a..5da535d 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -2066 +2067 @@ type BridgeValues struct {  }    type Config interface { + Viper() *viper.Viper   BridgeValues() *BridgeValues   GetBool(key string) (bool, bool)   GetInt(key string) (int, bool) @@ -2746 +27510 @@ func (c *config) BridgeValues() *BridgeValues {   return c.cv  }   +func (c *config) Viper() *viper.Viper { + return c.v +} +  func (c *config) GetBool(key string) (bool, bool) {   c.RLock()   defer c.RUnlock() diff --git a/gateway/gateway.go b/gateway/gateway.go index b875cca..af8aecd 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -856 +857 @@ func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {  func (gw *Gateway) AddBridge(cfg *config.Bridge) error {   br := gw.Router.getBridge(cfg.Account)   if br == nil { + gw.checkConfig(cfg)   br = bridge.New(cfg)   br.Config = gw.Router.Config   br.General = &gw.BridgeValues().General @@ -1046 +10519 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {   return nil  }   +func (gw *Gateway) checkConfig(cfg *config.Bridge) { + match := false + for _, key := range gw.Router.Config.Viper().AllKeys() { + if strings.HasPrefix(key, cfg.Account) { + match = true + break + } + } + if !match { + gw.logger.Fatalf("Account %s defined in gateway %s but no configuration found, exiting.", cfg.Account, gw.Name) + } +} +  // AddConfig associates a new configuration with the gateway object.  func (gw *Gateway) AddConfig(cfg *config.Gateway) error {   gw.Name = cfg.Name diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index b9bb5b9..2d1a106 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -1510 +1515 @@ import (    var testconfig = []byte(`  [irc.freenode] +server=""  [mattermost.test] +server=""  [gitter.42wim] +server=""  [discord.test] +server=""  [slack.test] +server=""    [[gateway]] name = "bridge1" @@ -4410 +4915 @@ var testconfig = []byte(`    var testconfig2 = []byte(`  [irc.freenode] +server=""  [mattermost.test] +server=""  [gitter.42wim] +server=""  [discord.test] +server=""  [slack.test] +server=""    [[gateway]] name = "bridge1" @@ -878 +9711 @@ var testconfig2 = []byte(`    var testconfig3 = []byte(`  [irc.zzz] +server=""  [telegram.zzz] +server=""  [slack.zzz] +server=""  [[gateway]]  name="bridge"  enable=true @@ -1767 +1896 @@ func TestNewRouter(t *testing.T) {   assert.Equal(t, 1, len(r.Gateways))   assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))   assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) -   r = maketestRouter(testconfig2)   assert.Equal(t, 2, len(r.Gateways))   assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) diff --git a/gateway/router.go b/gateway/router.go index 3d53167..56573f7 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -598 +5914 @@ func NewRouter(rootLogger *logrus.Logger, cfg config.Config, bridgeMap map[strin  // between them.  func (r *Router) Start() error {   m := make(map[string]*bridge.Bridge) + if len(r.Gateways) == 0 { + return fmt.Errorf("no [[gateway]] configured. See https://github.com/42wim/matterbridge/wiki/How-to-create-your-config for more info") + }   for _, gw := range r.Gateways {   r.logger.Infof("Parsing gateway %s", gw.Name) + if len(gw.Bridges) == 0 { + return fmt.Errorf("no bridges configured for gateway %s. See https://github.com/42wim/matterbridge/wiki/How-to-create-your-config for more info", gw.Name) + }   for _, br := range gw.Bridges {   m[br.Account] = br   }