diff --git a/cmd/cmd_debug.go b/cmd/cmd_debug.go index 866e281..3946588 100644 --- a/cmd/cmd_debug.go +++ b/cmd/cmd_debug.go @@ -12,8 +12,14 @@ import ( "github.com/noerw/osem_notify/utils" ) +var ( + clearCache bool +) + func init() { debugCmd.AddCommand(debugNotificationsCmd) + debugCacheCmd.PersistentFlags().BoolVarP(&clearCache, "clear", "", false, "reset the notifications cache") + debugCmd.AddCommand(debugCacheCmd) rootCmd.AddCommand(debugCmd) } @@ -32,13 +38,28 @@ var debugCmd = &cobra.Command{ }, } +var debugCacheCmd = &cobra.Command{ + Use: "cache", + Short: "Print or clear the notifications cache", + Long: "osem_notify debug cache prints the contents of the notifications cache", + RunE: func(cmd *cobra.Command, args []string) error { + if clearCache { + return core.ClearCache() + } + core.PrintCache() + return nil + }, +} + + var debugNotificationsCmd = &cobra.Command{ Use: "notifications", Short: "Verify that notifications are working", - Long: "osem_notify debug tests the functionality of the given feature", + Long: `osem_notify debug notifications sends a test notification according +to healthchecks.default.notifications.options as defined in the config file`, RunE: func(cmd *cobra.Command, args []string) error { defaultNotifyConf := &core.NotifyConfig{} - err := viper.UnmarshalKey("defaultHealthchecks", defaultNotifyConf) + err := viper.UnmarshalKey("healthchecks.default", defaultNotifyConf) if err != nil { return err } diff --git a/cmd/cmd_root.go b/cmd/cmd_root.go index 7113cf2..c3549b5 100644 --- a/cmd/cmd_root.go +++ b/cmd/cmd_root.go @@ -39,7 +39,7 @@ var configHelpCmd = &cobra.Command{ target: "all" threshold: "" - # override default health checks per box + # set health checks per box 593bcd656ccf3b0011791f5a: notifications: options: @@ -129,14 +129,14 @@ func init() { rootCmd.PersistentFlags().StringVarP(&api, "api", "a", "https://api.opensensemap.org", "openSenseMap API to query against") rootCmd.PersistentFlags().StringVarP(&logFormat, "logformat", "l", "plain", "log format, can be plain or json") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable verbose logging") - rootCmd.PersistentFlags().StringVarP(&shouldNotify, "notify", "n", "", `if set, will send out notifications for the specified type of check result, -Otherwise results are printed to stdout only. + rootCmd.PersistentFlags().StringVarP(&shouldNotify, "notify", "n", "", `If set, will send out notifications for the specified type of check result, +otherwise results are printed to stdout only. Allowed values are "all", "error", "ok". You might want to run 'osem_notify debug notifications' first to verify everything works. -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. +Notifications for failing checks are sent only once, and then cached until the issue got +resolved, unless --no-cache is set. +To clear the cache, run 'osem_notify debug cache --clear'. `) rootCmd.PersistentFlags().BoolVarP(&noCache, "no-cache", "", false, "send all notifications, ignoring results from previous runs. also don't update the cache.") diff --git a/core/cache.go b/core/cache.go index 92eafc7..877d6f3 100644 --- a/core/cache.go +++ b/core/cache.go @@ -60,3 +60,18 @@ func updateCache(box *Box, results []CheckResult) error { } return cache.WriteConfig() } + +func ClearCache() error { + fileName := utils.GetConfigFile("osem_notify_cache") + _, err := os.Stat(fileName) + if err != nil { + return nil + } + return os.Remove(fileName) +} + +func PrintCache() { + for key, val := range cache.AllSettings() { + log.Infof("%20s: %v", key, val) + } +}