mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Simple notification framework (#297)
* bonfire notifications working * make interface to make more extensible * some docs * typo * rename typo
This commit is contained in:
33
pkg/notifications/bonfire.go
Normal file
33
pkg/notifications/bonfire.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
initers = append(initers, func(cfg map[string]string) Notifier {
|
||||
if url, ok := cfg["bonfire_url"]; ok {
|
||||
return bonfireNotifier(url)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// bonfire notifier for stack exchange internal chat. String is just url with room and token in it
|
||||
type bonfireNotifier string
|
||||
|
||||
func (b bonfireNotifier) Notify(domain, provider, msg string, err error, preview bool) {
|
||||
var payload string
|
||||
if preview {
|
||||
payload = fmt.Sprintf(`**Preview: %s[%s] -** %s`, domain, provider, msg)
|
||||
} else if err != nil {
|
||||
payload = fmt.Sprintf(`**ERROR running correction on %s[%s] -** (%s) Error: %s`, domain, provider, msg, err)
|
||||
} else {
|
||||
payload = fmt.Sprintf(`Successfully ran correction for %s[%s] - %s`, domain, provider, msg)
|
||||
}
|
||||
http.Post(string(b), "text/markdown", strings.NewReader(payload))
|
||||
}
|
||||
|
||||
func (b bonfireNotifier) Done() {}
|
41
pkg/notifications/notifications.go
Normal file
41
pkg/notifications/notifications.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package notifications
|
||||
|
||||
// Notifier is a type that can send a notification
|
||||
type Notifier interface {
|
||||
// Notify will be called after a correction is performed.
|
||||
// It will be given the correction's message, the result of executing it,
|
||||
// and a flag for whether this is a preview or if it actually ran.
|
||||
// If preview is true, err will always be nil.
|
||||
Notify(domain, provider string, message string, err error, preview bool)
|
||||
// Done will be called exactly once after all notifications are done. This will allow "batched" notifiers to flush and send
|
||||
Done()
|
||||
}
|
||||
|
||||
// new notification types should add themselves to this array
|
||||
var initers = []func(map[string]string) Notifier{}
|
||||
|
||||
// Init will take the given config map (from creds.json notifications key) and create a single Notifier with
|
||||
// all notifications it has full config for.
|
||||
func Init(config map[string]string) Notifier {
|
||||
notifiers := multiNotifier{}
|
||||
for _, i := range initers {
|
||||
n := i(config)
|
||||
if n != nil {
|
||||
notifiers = append(notifiers, n)
|
||||
}
|
||||
}
|
||||
return notifiers
|
||||
}
|
||||
|
||||
type multiNotifier []Notifier
|
||||
|
||||
func (m multiNotifier) Notify(domain, provider string, message string, err error, preview bool) {
|
||||
for _, n := range m {
|
||||
n.Notify(domain, provider, message, err, preview)
|
||||
}
|
||||
}
|
||||
func (m multiNotifier) Done() {
|
||||
for _, n := range m {
|
||||
n.Done()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user