-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (62 loc) · 1.53 KB
/
main.go
File metadata and controls
73 lines (62 loc) · 1.53 KB
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
package main
import (
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
"golang.org/x/net/context"
)
type options struct {
Environment string `default:"local"`
AmqpURL string `default:"amqp://localhost"`
}
type dockerHubPushMessage struct {
CallbackURL string `json:"callback_url"`
PushData struct {
Tag string `json:"tag"`
} `json:"push_data"`
Repository struct {
Name string `json:"repo_name"`
} `json:"repository"`
}
func main() {
var opt options
err := envconfig.Process("listener", &opt)
if err != nil {
log.Fatal(err)
}
if opt.Environment == "production" {
log.SetFormatter(&log.JSONFormatter{})
}
ctx, done := context.WithCancel(context.Background())
subscriber, err := NewSubscriber(ctx, done, opt.AmqpURL,
"holmescode.deployments", "topic", "holmescode.deploymentsQueue", "", "")
if err != nil {
log.Fatal(err)
}
subscriber.Consume()
for {
select {
case msg := <-subscriber.Messages:
push := &dockerHubPushMessage{}
err := json.Unmarshal(msg.Body, push)
if err != nil {
log.WithFields(log.Fields{
"raw_message": string(msg.Body[:]),
"error": err,
}).Error("Could not parse message")
break
}
log.WithFields(log.Fields{
"callback_url": push.CallbackURL,
"tag": push.PushData.Tag,
"repo_name": push.Repository.Name,
}).Info("Received push notification")
go func() {
NewUpdater(push.Repository.Name, push.PushData.Tag)
}()
case <-ctx.Done():
subscriber.Close()
return
}
}
}