watchtower/pkg/notifications/msteams.go
Amir Zarrinkafsh dc12a1ac7f
feat: allow hostname override for notifiers (#994)
* feat: allow hostname override for email notifier

As it currently stands all notifiers utilise `os.Hostname` to populate their titles/subjects.

When utilising Docker with a bridged network if you set the hostname for a container to an external DNS hostname Docker's internal DNS resolver will override said hostname for all containers within the bridged network.

This change allows a user to specify what hostname should be represented in the email notifications without having to change the `os.Hostname`.

* feat: allow custom hostname for all notifiers

* docs: adjust notification hostname flag
2021-06-24 00:29:20 +02:00

60 lines
1.4 KiB
Go

package notifications
import (
shoutrrrTeams "github.com/containrrr/shoutrrr/pkg/services/teams"
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
)
const (
msTeamsType = "msteams"
)
type msTeamsTypeNotifier struct {
webHookURL string
levels []log.Level
data bool
}
// NewMsTeamsNotifier is a factory method creating a new teams notifier instance
func NewMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.ConvertibleNotifier {
return newMsTeamsNotifier(cmd, acceptedLogLevels)
}
func newMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.ConvertibleNotifier {
flags := cmd.PersistentFlags()
webHookURL, _ := flags.GetString("notification-msteams-hook")
if len(webHookURL) <= 0 {
log.Fatal("Required argument --notification-msteams-hook(cli) or WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL(env) is empty.")
}
withData, _ := flags.GetBool("notification-msteams-data")
n := &msTeamsTypeNotifier{
levels: acceptedLogLevels,
webHookURL: webHookURL,
data: withData,
}
return n
}
func (n *msTeamsTypeNotifier) GetURL(c *cobra.Command) (string, error) {
webhookURL, err := url.Parse(n.webHookURL)
if err != nil {
return "", err
}
config, err := shoutrrrTeams.ConfigFromWebhookURL(*webhookURL)
if err != nil {
return "", err
}
config.Color = ColorHex
config.Title = GetTitle(c)
return config.GetURL().String(), nil
}