From 287f26a37c0672b4a4e194d4d83e8558c5d02d66 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sun, 10 Feb 2019 01:58:23 +0100 Subject: [PATCH] refactor ComposeNotification() --- core/notifier_email.go | 33 --------------------------------- core/notifier_xmpp.go | 34 ---------------------------------- core/notifiers.go | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 69 deletions(-) diff --git a/core/notifier_email.go b/core/notifier_email.go index c7fd626..970ccfb 100644 --- a/core/notifier_email.go +++ b/core/notifier_email.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/smtp" - "strings" "time" "github.com/spf13/viper" @@ -55,38 +54,6 @@ func (n EmailNotifier) New(config TransportConfig) (AbstractNotifier, error) { }, nil } -func (n EmailNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification { - errTexts := []string{} - resolvedTexts := []string{} - for _, check := range checks { - if check.Status == CheckErr { - errTexts = append(errTexts, check.String()) - } else { - resolvedTexts = append(resolvedTexts, check.String()) - } - } - - var ( - resolved string - resolvedList string - errList string - ) - if len(resolvedTexts) != 0 { - resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n")) - } - if len(errTexts) != 0 { - errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n")) - } else { - resolved = "resolved " - } - - return Notification{ - Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name), - Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)", - time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id), - } -} - func (n EmailNotifier) Submit(notification Notification) error { // :TransportConfSourceHack auth := smtp.PlainAuth( diff --git a/core/notifier_xmpp.go b/core/notifier_xmpp.go index f431d34..c649573 100644 --- a/core/notifier_xmpp.go +++ b/core/notifier_xmpp.go @@ -3,8 +3,6 @@ package core import ( "errors" "fmt" - "strings" - "time" xmpp "github.com/mattn/go-xmpp" "github.com/spf13/viper" @@ -54,38 +52,6 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) { }, nil } -func (n XmppNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification { - errTexts := []string{} - resolvedTexts := []string{} - for _, check := range checks { - if check.Status == CheckErr { - errTexts = append(errTexts, check.String()) - } else { - resolvedTexts = append(resolvedTexts, check.String()) - } - } - - var ( - resolved string - resolvedList string - errList string - ) - if len(resolvedTexts) != 0 { - resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n")) - } - if len(errTexts) != 0 { - errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n")) - } else { - resolved = "resolved " - } - - return Notification{ - Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name), - Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)", - time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id), - } -} - func (n XmppNotifier) Submit(notification Notification) error { // :TransportConfSourceHack xmppOpts := xmpp.Options{ diff --git a/core/notifiers.go b/core/notifiers.go index 5848b4c..a6ffa8d 100644 --- a/core/notifiers.go +++ b/core/notifiers.go @@ -15,7 +15,6 @@ var Notifiers = map[string]AbstractNotifier{ type AbstractNotifier interface { New(config TransportConfig) (AbstractNotifier, error) - ComposeNotification(box *Box, checks []CheckResult) Notification Submit(notification Notification) error } @@ -81,7 +80,7 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache continue } - notification := notifier.ComposeNotification(box, resultsDue) + notification := ComposeNotification(box, resultsDue) var submitErr error submitErr = notifier.Submit(notification) @@ -116,3 +115,36 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache } return nil } + +func ComposeNotification(box *Box, checks []CheckResult) Notification { + errTexts := []string{} + resolvedTexts := []string{} + for _, check := range checks { + if check.Status == CheckErr { + errTexts = append(errTexts, check.String()) + } else { + resolvedTexts = append(resolvedTexts, check.String()) + } + } + + var ( + resolved string + resolvedList string + errList string + ) + if len(resolvedTexts) != 0 { + resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n")) + } + if len(errTexts) != 0 { + errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n")) + } else { + resolved = "resolved " + } + + return Notification{ + Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name), + Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)", + time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id), + } +} +