1
0
mirror of https://github.com/containrrr/watchtower.git synced 2025-01-20 18:28:27 +02:00

63 lines
1.5 KiB
Go
Raw Normal View History

package notifications
import (
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
type typeNotifier interface {
StartNotification()
SendNotification()
}
2017-10-29 23:45:01 -07:00
// Notifier can send log output as notification to admins, with optional batching.
type Notifier struct {
types []typeNotifier
}
2017-10-29 23:45:01 -07:00
// NewNotifier creates and returns a new Notifier, using global configuration.
func NewNotifier(c *cli.Context) *Notifier {
n := &Notifier{}
logLevel, err := log.ParseLevel(c.GlobalString("notifications-level"))
if err != nil {
log.Fatalf("Notifications invalid log level: %s", err.Error())
}
acceptedLogLevels := slackrus.LevelThreshold(logLevel)
// Parse types and create notifiers.
types := c.GlobalStringSlice("notifications")
for _, t := range types {
var tn typeNotifier
switch t {
case emailType:
tn = newEmailNotifier(c, acceptedLogLevels)
2017-11-27 12:04:08 +01:00
case slackType:
tn = newSlackNotifier(c, acceptedLogLevels)
case msTeamsType:
tn = newMsTeamsNotifier(c, acceptedLogLevels)
default:
log.Fatalf("Unknown notification type %q", t)
}
n.types = append(n.types, tn)
}
return n
}
2017-10-29 23:45:01 -07:00
// StartNotification starts a log batch. Notifications will be accumulated after this point and only sent when SendNotification() is called.
func (n *Notifier) StartNotification() {
for _, t := range n.types {
t.StartNotification()
}
}
2017-10-29 23:45:01 -07:00
// SendNotification sends any notifications accumulated since StartNotification() was called.
func (n *Notifier) SendNotification() {
for _, t := range n.types {
t.SendNotification()
}
}