feat(notifications): add title field to template data (#1125)

This commit is contained in:
nils måsén 2022-01-05 12:08:47 +01:00 committed by GitHub
parent 1d59fb83dd
commit aa02d8d31b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 94 additions and 35 deletions

View file

@ -30,14 +30,14 @@ func NewNotifier(c *cobra.Command) ty.Notifier {
tplString, _ := f.GetString("notification-template")
urls, _ := f.GetStringArray("notification-url")
urls, delay := AppendLegacyUrls(urls, c)
hostname := GetHostname(c)
urls, delay := AppendLegacyUrls(urls, c, GetTitle(hostname))
title := GetTitle(c)
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, title, delay, urls...)
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, hostname, delay, urls...)
}
// AppendLegacyUrls creates shoutrrr equivalent URLs from legacy notification flags
func AppendLegacyUrls(urls []string, cmd *cobra.Command) ([]string, time.Duration) {
func AppendLegacyUrls(urls []string, cmd *cobra.Command, title string) ([]string, time.Duration) {
// Parse types and create notifiers.
types, err := cmd.Flags().GetStringSlice("notifications")
@ -69,7 +69,7 @@ func AppendLegacyUrls(urls []string, cmd *cobra.Command) ([]string, time.Duratio
continue
}
shoutrrrURL, err := legacyNotifier.GetURL(cmd)
shoutrrrURL, err := legacyNotifier.GetURL(cmd, title)
if err != nil {
log.Fatal("failed to create notification config: ", err)
}
@ -85,20 +85,27 @@ func AppendLegacyUrls(urls []string, cmd *cobra.Command) ([]string, time.Duratio
}
// GetTitle returns a common notification title with hostname appended
func GetTitle(c *cobra.Command) (title string) {
title = "Watchtower updates"
func GetTitle(hostname string) string {
title := "Watchtower updates"
if hostname != "" {
title += " on " + hostname
}
return title
}
// GetHostname returns the hostname as set by args or resolved from OS
func GetHostname(c *cobra.Command) string {
f := c.PersistentFlags()
hostname, _ := f.GetString("notifications-hostname")
if hostname != "" {
title += " on " + hostname
return hostname
} else if hostname, err := os.Hostname(); err == nil {
title += " on " + hostname
return hostname
}
return
return ""
}
// ColorHex is the default notification color used for services that support it (formatted as a CSS hex string)