From 15091757e3143454b2db2747ea229d739fbc492a Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 21 Apr 2017 17:20:49 +0200 Subject: [PATCH] tg: Return broker instance when creating a client --- mods/main.go | 8 +++++--- mspa/main.go | 2 +- stats/main.go | 2 +- tg/client.go | 6 +++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mods/main.go b/mods/main.go index 77ba7ff..12c087c 100644 --- a/mods/main.go +++ b/mods/main.go @@ -86,6 +86,7 @@ func isCommand(update tg.APIMessage, cmdname string) bool { return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@")) } +var broker *tg.Broker var botname *string var impact *string var gillmt *string @@ -123,12 +124,13 @@ func main() { rand.Seed(time.Now().Unix()) - initmods() - - err := tg.CreateBrokerClient(*brokerAddr, dispatch) + var err error + broker, err = tg.CreateBrokerClient(*brokerAddr, dispatch) if err != nil { panic(err) } + + initmods() } func assert(err error) { diff --git a/mspa/main.go b/mspa/main.go index cdf2ed8..85ef8fa 100644 --- a/mspa/main.go +++ b/mspa/main.go @@ -140,6 +140,6 @@ func main() { go startPolling(*delay) - err := tg.CreateBrokerClient(*brokerAddr, process) + _, err := tg.CreateBrokerClient(*brokerAddr, process) assert(err) } diff --git a/stats/main.go b/stats/main.go index 11ed96f..a71d1b3 100644 --- a/stats/main.go +++ b/stats/main.go @@ -42,6 +42,6 @@ func main() { go startWebServer(*webBind) - err = tg.CreateBrokerClient(*brokerAddr, process) + _, err = tg.CreateBrokerClient(*brokerAddr, process) assert(err) } diff --git a/tg/client.go b/tg/client.go index c82689a..2cd2bbe 100644 --- a/tg/client.go +++ b/tg/client.go @@ -15,10 +15,10 @@ type BrokerCallback func(broker *Broker, update BrokerUpdate) // CreateBrokerClient creates a connection to a broker and sends all webhook updates to a given function. // This is the intended way to create clients, please refer to examples for how to make a simple client. -func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error { +func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) (*Broker, error) { broker, err := ConnectToBroker(brokerAddr) if err != nil { - return err + return nil, err } defer broker.Close() @@ -54,5 +54,5 @@ func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error { // Empty buffer buf = []byte{} } - return io.EOF + return broker, io.EOF }