From bc542eb506296d414f6154730c9de6da0cb5c301 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Fri, 29 Jun 2018 21:38:00 +0200 Subject: [PATCH] add --no-cache flag #2 --- cmd/cmd_root.go | 2 ++ cmd/shared.go | 3 ++- core/notifiers.go | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/cmd_root.go b/cmd/cmd_root.go index 9f82a01..7113cf2 100644 --- a/cmd/cmd_root.go +++ b/cmd/cmd_root.go @@ -117,6 +117,7 @@ var cfgFile string func init() { var ( debug bool + noCache bool shouldNotify string logFormat string api string @@ -137,6 +138,7 @@ Notifications for failing checks are sent only once, and then cached until the issue got resolved. To clear the cache, delete the file ~/.osem_notify_cache.yaml. `) + rootCmd.PersistentFlags().BoolVarP(&noCache, "no-cache", "", false, "send all notifications, ignoring results from previous runs. also don't update the cache.") viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config diff --git a/cmd/shared.go b/cmd/shared.go index b017330..ea56cc5 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -64,7 +64,8 @@ func checkAndNotify(boxIds []string) error { return fmt.Errorf("invalid value %s for \"notify\"", notify) } - return results.SendNotifications(types) + useCache := !viper.GetBool("no-cache") + return results.SendNotifications(types, useCache) } return nil } diff --git a/core/notifiers.go b/core/notifiers.go index 127bc27..2b41503 100644 --- a/core/notifiers.go +++ b/core/notifiers.go @@ -39,9 +39,10 @@ func (box Box) GetNotifier() (AbstractNotifier, error) { return notifier.New(box.NotifyConf.Notifications.Options) } -func (results BoxCheckResults) SendNotifications(notifyTypes []string) error { - // TODO: expose flag to not use cache - results = results.filterChangedFromCache() +func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache bool) error { + if useCache { + results = results.filterChangedFromCache() + } toCheck := results.Size(notifyTypes) if toCheck == 0 { @@ -90,11 +91,13 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string) error { } // update cache (with /all/ changed results to reset status) - notifyLog.Debug("updating cache") - cacheError := updateCache(box, resultsBox) - if cacheError != nil { - notifyLog.Error("could not cache notification results: ", cacheError) - errs = append(errs, cacheError.Error()) + if useCache { + notifyLog.Debug("updating cache") + cacheError := updateCache(box, resultsBox) + if cacheError != nil { + notifyLog.Error("could not cache notification results: ", cacheError) + errs = append(errs, cacheError.Error()) + } } if len(resultsDue) != 0 {