1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-12 09:04:17 +02:00
watchtower/pkg/notifications/slack.go

69 lines
1.7 KiB
Go
Raw Normal View History

2017-11-27 13:04:08 +02:00
package notifications
import (
"strings"
shoutrrrSlack "github.com/containrrr/shoutrrr/pkg/services/slack"
2019-07-21 22:22:30 +02:00
t "github.com/containrrr/watchtower/pkg/types"
2017-11-27 13:04:08 +02:00
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
2019-06-22 22:04:36 +02:00
"github.com/spf13/cobra"
2017-11-27 13:04:08 +02:00
)
const (
slackType = "slack"
)
type slackTypeNotifier struct {
slackrus.SlackrusHook
}
// NewSlackNotifier is a factory function used to generate new instance of the slack notifier type
func NewSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.ConvertableNotifier {
return newSlackNotifier(c, acceptedLogLevels)
}
func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.ConvertableNotifier {
flags := c.PersistentFlags()
2019-06-22 22:04:36 +02:00
hookURL, _ := flags.GetString("notification-slack-hook-url")
userName, _ := flags.GetString("notification-slack-identifier")
channel, _ := flags.GetString("notification-slack-channel")
emoji, _ := flags.GetString("notification-slack-icon-emoji")
iconURL, _ := flags.GetString("notification-slack-icon-url")
2017-11-27 13:04:08 +02:00
n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{
2019-06-23 00:32:50 +02:00
HookURL: hookURL,
2019-06-22 22:04:36 +02:00
Username: userName,
Channel: channel,
IconEmoji: emoji,
2019-06-23 00:32:50 +02:00
IconURL: iconURL,
AcceptedLevels: acceptedLogLevels,
2017-11-27 13:04:08 +02:00
},
}
return n
}
func (s *slackTypeNotifier) GetURL() string {
rawTokens := strings.Replace(s.HookURL, "https://hooks.slack.com/services/", "", 1)
tokens := strings.Split(rawTokens, "/")
conf := &shoutrrrSlack.Config{
BotName: s.Username,
Token: shoutrrrSlack.Token{
A: tokens[0],
B: tokens[1],
C: tokens[2],
},
}
return conf.GetURL().String()
}
func (s *slackTypeNotifier) StartNotification() {
}
2017-11-27 13:04:08 +02:00
func (s *slackTypeNotifier) SendNotification() {}
func (s *slackTypeNotifier) Close() {}