-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.go
127 lines (106 loc) · 2.55 KB
/
notifier.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"fmt"
"html"
"log"
"os"
"strings"
"time"
"github.com/alecthomas/template"
sendgrid "github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
var notifiers map[string]func(string) (Notifier, error)
func init() {
notifiers = make(map[string]func(string) (Notifier, error))
notifiers["stdout"] = NewStdOutNotifier
notifiers["sendgrid"] = NewSendGridNotifier
}
func PrepareNotifiers() ([]Notifier, error) {
n := []Notifier{}
for _, e := range os.Environ() {
kv := strings.SplitN(e, "=", 2)
key := strings.ToLower(kv[0])
value := kv[1]
if strings.HasPrefix(key, "webstalker_notifier") {
for name, fn := range notifiers {
if strings.Contains(key, name) {
notifier, err := fn(value)
if err != nil {
return n, err
}
log.Printf("Notifier config for '%s' found\n", name)
n = append(n, notifier)
}
}
}
}
if len(n) < 1 {
log.Println("No notifier configured, using StdOut Notifier")
n = append(n, StdOutNotifier{})
}
return n, nil
}
type Notifier interface {
Notify(recipient string, message string, diff string) error
}
func renderTemplate(m, d string) (string, error) {
var data = struct {
Diff string
}{
Diff: d,
}
tmpl, err := template.New("tmpl").Parse(m)
if err != nil {
return "", err
}
var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
return "", err
}
return b.String(), nil
}
type StdOutNotifier struct{}
func NewStdOutNotifier(c string) (Notifier, error) {
return StdOutNotifier{}, nil
}
func (son StdOutNotifier) Notify(r, m, d string) error {
msg, err := renderTemplate(m, d)
if err != nil {
return err
}
ts := time.Now()
fmt.Printf("\tTimestamp: %s\n\tTo: %s\n\t%s\n", ts.String(), r, msg)
return nil
}
type SendGridNotifier struct {
APIKey string
Sender string
}
func NewSendGridNotifier(c string) (Notifier, error) {
n := SendGridNotifier{}
tokens := strings.Fields(c)
if len(tokens) != 2 {
return n, fmt.Errorf("Malformend config for SendGrid notifier: %s", c)
}
n.Sender = tokens[0]
n.APIKey = tokens[1]
return n, nil
}
func (sgn SendGridNotifier) Notify(r, m, d string) error {
msg, err := renderTemplate(m, d)
if err != nil {
return err
}
from := mail.NewEmail(sgn.Sender, sgn.Sender)
subject := "Updates from webstalker"
to := mail.NewEmail(r, r)
plainTextContent := msg
htmlContent := html.EscapeString(msg)
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
client := sendgrid.NewSendClient(sgn.APIKey)
_, err = client.Send(message)
return err
}