This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
104 lines (85 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright (C) 2018 Expedia Group.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/url"
"os"
"os/signal"
api "github.com/HotelsDotCom/flyte-client/client"
"github.com/HotelsDotCom/flyte-client/flyte"
"github.com/HotelsDotCom/flyte-hipchat/bkp"
"github.com/HotelsDotCom/flyte-hipchat/client"
"github.com/HotelsDotCom/flyte-hipchat/command"
"github.com/HotelsDotCom/flyte-hipchat/config"
"github.com/HotelsDotCom/flyte-hipchat/event"
"github.com/HotelsDotCom/flyte-hipchat/hipchat"
"github.com/HotelsDotCom/go-logger"
"syscall"
"time"
)
func main() {
messages := make(chan hipchat.Message)
hc := initHipchat(messages)
p := flyte.NewPack(getPackDef(hc), api.NewClient(config.ApiHost(), 10*time.Second))
p.Start()
event.HandleReceivedMessages(p, messages)
logger.Infof("joined rooms=%v", hc.JoinedRoomIds())
// block until we get an exit causing signal
signalCh := make(chan os.Signal)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
select {
case <-signalCh:
logger.Info("received interrupt, shutting down...")
hc.Shutdown()
logger.Info("shut down")
}
}
func initHipchat(messages chan hipchat.Message) hipchat.Hipchat {
hcClient := client.NewHipChatClient(config.HipchatAuthTokens())
bkpDir := config.BkpDir()
if bkpDir == "" {
bkpDir = bkp.CreateDefaultBkpDir()
}
bkpFile := bkp.CreateBkpFile(bkpDir, "rooms.json")
hc, err := hipchat.NewHipchat(bkpFile, hcClient, messages)
if err != nil {
logger.Fatalf("cannot initialize pack: %v", err)
}
if defaultRoom := config.DefaultRoom(); defaultRoom != "" {
hc.JoinRoom(defaultRoom)
}
if len(hc.JoinedRoomIds()) == 0 {
logger.Fatal("pack did NOT join any rooms, provide DEFAULT_JOIN_ROOM env. var.")
}
return hc
}
func getPackDef(hc hipchat.Hipchat) flyte.PackDef {
helpUrl, err := url.Parse("http://github.com/HotelsDotCom/flyte-hipchat/browse/README.md")
if err != nil {
logger.Fatal("invalid pack help url")
}
return flyte.PackDef{
Name: "HipChat",
HelpURL: helpUrl,
Commands: []flyte.Command{
command.SendMessageCommand(hc),
command.SendNotificationCommand(hc),
command.BroadcastCommand(hc),
command.JoinCommand(hc),
command.LeaveCommand(hc),
},
EventDefs: []flyte.EventDef{
{Name: "ReceivedMessage"},
},
}
}