5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 08:10:26 +00:00

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
This commit is contained in:
Wim 2019-09-09 23:48:00 +02:00 committed by GitHub
parent f92927eae5
commit 28783a4146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 1 deletions

View File

@ -206,6 +206,7 @@ type BridgeValues struct {
} }
type Config interface { type Config interface {
Viper() *viper.Viper
BridgeValues() *BridgeValues BridgeValues() *BridgeValues
GetBool(key string) (bool, bool) GetBool(key string) (bool, bool)
GetInt(key string) (int, bool) GetInt(key string) (int, bool)
@ -274,6 +275,10 @@ func (c *config) BridgeValues() *BridgeValues {
return c.cv return c.cv
} }
func (c *config) Viper() *viper.Viper {
return c.v
}
func (c *config) GetBool(key string) (bool, bool) { func (c *config) GetBool(key string) (bool, bool) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()

View File

@ -85,6 +85,7 @@ func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {
func (gw *Gateway) AddBridge(cfg *config.Bridge) error { func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
br := gw.Router.getBridge(cfg.Account) br := gw.Router.getBridge(cfg.Account)
if br == nil { if br == nil {
gw.checkConfig(cfg)
br = bridge.New(cfg) br = bridge.New(cfg)
br.Config = gw.Router.Config br.Config = gw.Router.Config
br.General = &gw.BridgeValues().General br.General = &gw.BridgeValues().General
@ -104,6 +105,19 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
return nil 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. // AddConfig associates a new configuration with the gateway object.
func (gw *Gateway) AddConfig(cfg *config.Gateway) error { func (gw *Gateway) AddConfig(cfg *config.Gateway) error {
gw.Name = cfg.Name gw.Name = cfg.Name

View File

@ -15,10 +15,15 @@ import (
var testconfig = []byte(` var testconfig = []byte(`
[irc.freenode] [irc.freenode]
server=""
[mattermost.test] [mattermost.test]
server=""
[gitter.42wim] [gitter.42wim]
server=""
[discord.test] [discord.test]
server=""
[slack.test] [slack.test]
server=""
[[gateway]] [[gateway]]
name = "bridge1" name = "bridge1"
@ -44,10 +49,15 @@ var testconfig = []byte(`
var testconfig2 = []byte(` var testconfig2 = []byte(`
[irc.freenode] [irc.freenode]
server=""
[mattermost.test] [mattermost.test]
server=""
[gitter.42wim] [gitter.42wim]
server=""
[discord.test] [discord.test]
server=""
[slack.test] [slack.test]
server=""
[[gateway]] [[gateway]]
name = "bridge1" name = "bridge1"
@ -87,8 +97,11 @@ var testconfig2 = []byte(`
var testconfig3 = []byte(` var testconfig3 = []byte(`
[irc.zzz] [irc.zzz]
server=""
[telegram.zzz] [telegram.zzz]
server=""
[slack.zzz] [slack.zzz]
server=""
[[gateway]] [[gateway]]
name="bridge" name="bridge"
enable=true enable=true
@ -176,7 +189,6 @@ func TestNewRouter(t *testing.T) {
assert.Equal(t, 1, len(r.Gateways)) assert.Equal(t, 1, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
r = maketestRouter(testconfig2) r = maketestRouter(testconfig2)
assert.Equal(t, 2, len(r.Gateways)) assert.Equal(t, 2, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))

View File

@ -59,8 +59,14 @@ func NewRouter(rootLogger *logrus.Logger, cfg config.Config, bridgeMap map[strin
// between them. // between them.
func (r *Router) Start() error { func (r *Router) Start() error {
m := make(map[string]*bridge.Bridge) 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 { for _, gw := range r.Gateways {
r.logger.Infof("Parsing gateway %s", gw.Name) 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 { for _, br := range gw.Bridges {
m[br.Account] = br m[br.Account] = br
} }