From f2088a687edd53e9e797f3b8941210ca3650cc45 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Sat, 1 Dec 2018 06:53:00 +0800 Subject: [PATCH] Extract bridgeMap into own package to improve testability (#601) --- gateway/bridgemap/bridgemap.go | 35 ++++++++++++++++++++++++++++++++++ gateway/gateway.go | 32 +------------------------------ gateway/gateway_test.go | 3 ++- gateway/router.go | 4 +++- matterbridge.go | 3 ++- 5 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 gateway/bridgemap/bridgemap.go diff --git a/gateway/bridgemap/bridgemap.go b/gateway/bridgemap/bridgemap.go new file mode 100644 index 00000000..20577dc1 --- /dev/null +++ b/gateway/bridgemap/bridgemap.go @@ -0,0 +1,35 @@ +package bridgemap + +import ( + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/api" + "github.com/42wim/matterbridge/bridge/discord" + "github.com/42wim/matterbridge/bridge/gitter" + "github.com/42wim/matterbridge/bridge/irc" + "github.com/42wim/matterbridge/bridge/matrix" + "github.com/42wim/matterbridge/bridge/mattermost" + "github.com/42wim/matterbridge/bridge/rocketchat" + "github.com/42wim/matterbridge/bridge/slack" + "github.com/42wim/matterbridge/bridge/sshchat" + "github.com/42wim/matterbridge/bridge/steam" + "github.com/42wim/matterbridge/bridge/telegram" + "github.com/42wim/matterbridge/bridge/xmpp" + "github.com/42wim/matterbridge/bridge/zulip" +) + +var FullMap = map[string]bridge.Factory{ + "api": api.New, + "discord": bdiscord.New, + "gitter": bgitter.New, + "irc": birc.New, + "mattermost": bmattermost.New, + "matrix": bmatrix.New, + "rocketchat": brocketchat.New, + "slack-legacy": bslack.NewLegacy, + "slack": bslack.New, + "sshchat": bsshchat.New, + "steam": bsteam.New, + "telegram": btelegram.New, + "xmpp": bxmpp.New, + "zulip": bzulip.New, +} diff --git a/gateway/gateway.go b/gateway/gateway.go index 2b8bdfa9..674179fb 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -13,20 +13,7 @@ import ( "time" "github.com/42wim/matterbridge/bridge" - "github.com/42wim/matterbridge/bridge/api" "github.com/42wim/matterbridge/bridge/config" - bdiscord "github.com/42wim/matterbridge/bridge/discord" - bgitter "github.com/42wim/matterbridge/bridge/gitter" - birc "github.com/42wim/matterbridge/bridge/irc" - bmatrix "github.com/42wim/matterbridge/bridge/matrix" - bmattermost "github.com/42wim/matterbridge/bridge/mattermost" - brocketchat "github.com/42wim/matterbridge/bridge/rocketchat" - bslack "github.com/42wim/matterbridge/bridge/slack" - bsshchat "github.com/42wim/matterbridge/bridge/sshchat" - bsteam "github.com/42wim/matterbridge/bridge/steam" - btelegram "github.com/42wim/matterbridge/bridge/telegram" - bxmpp "github.com/42wim/matterbridge/bridge/xmpp" - bzulip "github.com/42wim/matterbridge/bridge/zulip" "github.com/hashicorp/golang-lru" "github.com/peterhellberg/emojilib" log "github.com/sirupsen/logrus" @@ -53,23 +40,6 @@ type BrMsgID struct { var flog *log.Entry -var bridgeMap = map[string]bridge.Factory{ - "api": api.New, - "discord": bdiscord.New, - "gitter": bgitter.New, - "irc": birc.New, - "mattermost": bmattermost.New, - "matrix": bmatrix.New, - "rocketchat": brocketchat.New, - "slack-legacy": bslack.NewLegacy, - "slack": bslack.New, - "sshchat": bsshchat.New, - "steam": bsteam.New, - "telegram": btelegram.New, - "xmpp": bxmpp.New, - "zulip": bzulip.New, -} - const ( apiProtocol = "api" ) @@ -114,7 +84,7 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error { br.Log = log.WithFields(log.Fields{"prefix": "bridge"}) brconfig := &bridge.Config{Remote: gw.Message, Log: log.WithFields(log.Fields{"prefix": br.Protocol}), Bridge: br} // add the actual bridger for this protocol to this bridge using the bridgeMap - br.Bridger = bridgeMap[br.Protocol](brconfig) + br.Bridger = gw.Router.BridgeMap[br.Protocol](brconfig) } gw.mapChannelsToBridge(br) gw.Bridges[cfg.Account] = br diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index e7b77bf4..798b30c8 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/gateway/bridgemap" "github.com/stretchr/testify/assert" "testing" @@ -160,7 +161,7 @@ const ( func maketestRouter(input []byte) *Router { cfg := config.NewConfigFromString(input) - r, err := NewRouter(cfg) + r, err := NewRouter(cfg, bridgemap.FullMap) if err != nil { fmt.Println(err) } diff --git a/gateway/router.go b/gateway/router.go index 13e8ddd8..e4478d84 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -12,14 +12,16 @@ import ( type Router struct { config.Config + BridgeMap map[string]bridge.Factory Gateways map[string]*Gateway Message chan config.Message MattermostPlugin chan config.Message } -func NewRouter(cfg config.Config) (*Router, error) { +func NewRouter(cfg config.Config, bridgeMap map[string]bridge.Factory) (*Router, error) { r := &Router{ Config: cfg, + BridgeMap: bridgeMap, Message: make(chan config.Message), MattermostPlugin: make(chan config.Message), Gateways: make(map[string]*Gateway), diff --git a/matterbridge.go b/matterbridge.go index df0918bb..81af86d0 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -8,6 +8,7 @@ import ( "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/gateway" + "github.com/42wim/matterbridge/gateway/bridgemap" "github.com/google/gops/agent" prefixed "github.com/matterbridge/logrus-prefixed-formatter" log "github.com/sirupsen/logrus" @@ -45,7 +46,7 @@ func main() { } cfg := config.NewConfig(*flagConfig) cfg.BridgeValues().General.Debug = *flagDebug - r, err := gateway.NewRouter(cfg) + r, err := gateway.NewRouter(cfg, bridgemap.FullMap) if err != nil { flog.Fatalf("Starting gateway failed: %s", err) }