Added Mail Subject Tag to email.go (#389)

* Update email.go

Added SubjectTag as variable

* Update email.go

* Update email.go

* Update email.go

* Update flags.go

* Update flags.go
pull/408/head
Simon Aronsson 5 years ago committed by GitHub
commit 2a6e296838
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,6 +32,7 @@ To receive notifications by email, the following command-line options, or their
- `--notification-email-server-user` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER`): The username to authenticate with the SMTP server with. - `--notification-email-server-user` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER`): The username to authenticate with the SMTP server with.
- `--notification-email-server-password` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD`): The password to authenticate with the SMTP server with. - `--notification-email-server-password` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD`): The password to authenticate with the SMTP server with.
- `--notification-email-delay` (env. `WATCHTOWER_NOTIFICATION_EMAIL_DELAY`): Delay before sending notifications expressed in seconds. - `--notification-email-delay` (env. `WATCHTOWER_NOTIFICATION_EMAIL_DELAY`): Delay before sending notifications expressed in seconds.
- `--notification-email-subjecttag` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG`): Prefix to include in the subject tag. Useful when running multiple watchtowers.
Example: Example:

@ -174,6 +174,12 @@ Should only be used for testing.
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD"), viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD"),
"SMTP server password for sending notifications") "SMTP server password for sending notifications")
flags.StringP(
"notification-email-subjecttag",
"",
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG"),
"Subject prefix tag for notifications via mail")
flags.StringP( flags.StringP(
"notification-slack-hook-url", "notification-slack-hook-url",
"", "",
@ -238,6 +244,7 @@ func SetDefaults() {
viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{}) viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{})
viper.SetDefault("WATCHTOWER_NOTIFICATIONS_LEVEL", "info") viper.SetDefault("WATCHTOWER_NOTIFICATIONS_LEVEL", "info")
viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT", 25) viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT", 25)
viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG", "")
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower") viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
} }

@ -24,7 +24,7 @@ const (
// We work around that by holding on to log entries until the update cycle is done. // We work around that by holding on to log entries until the update cycle is done.
type emailTypeNotifier struct { type emailTypeNotifier struct {
From, To string From, To string
Server, User, Password string Server, User, Password, SubjectTag string
Port int Port int
tlsSkipVerify bool tlsSkipVerify bool
entries []*log.Entry entries []*log.Entry
@ -43,6 +43,7 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie
port, _ := flags.GetInt("notification-email-server-port") port, _ := flags.GetInt("notification-email-server-port")
tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify") tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify")
delay, _ := flags.GetInt("notification-email-delay") delay, _ := flags.GetInt("notification-email-delay")
subjecttag, _ := flags.GetString("notification-email-subjecttag")
n := &emailTypeNotifier{ n := &emailTypeNotifier{
From: from, From: from,
@ -54,6 +55,7 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie
tlsSkipVerify: tlsSkipVerify, tlsSkipVerify: tlsSkipVerify,
logLevels: acceptedLogLevels, logLevels: acceptedLogLevels,
delay: time.Duration(delay) * time.Second, delay: time.Duration(delay) * time.Second,
SubjectTag: subjecttag,
} }
log.AddHook(n) log.AddHook(n)
@ -62,7 +64,11 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie
} }
func (e *emailTypeNotifier) buildMessage(entries []*log.Entry) []byte { func (e *emailTypeNotifier) buildMessage(entries []*log.Entry) []byte {
emailSubject := "Watchtower updates" if SubjectTag == "" {
emailSubject := "Watchtower updates"
} else {
emailSubject := SubjectTag + " Watchtower updates"
}
if hostname, err := os.Hostname(); err == nil { if hostname, err := os.Hostname(); err == nil {
emailSubject += " on " + hostname emailSubject += " on " + hostname
} }

Loading…
Cancel
Save