You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
osem_notify/cmd/watch.go

48 lines
1.0 KiB
Go

package cmd
import (
"time"
"github.com/spf13/cobra"
)
var watchInterval int
var ticker <-chan time.Time
func init() {
watchCmd.AddCommand(watchBoxesCmd)
watchCmd.PersistentFlags().IntVarP(&watchInterval, "interval", "i", 15, "interval to run checks in minutes")
rootCmd.AddCommand(watchCmd)
}
var watchCmd = &cobra.Command{
Use: "watch",
Aliases: []string{"serve"},
Short: "Watch boxes for events at an interval",
Long: "Watch boxes for events at an interval",
}
var watchBoxesCmd = &cobra.Command{
Use: "boxes <boxId> [...<boxIds>]",
Short: "watch a list of box IDs for events",
Long: "specify box IDs to watch them for events",
Args: BoxIdValidator,
PreRun: func(cmd *cobra.Command, args []string) {
ticker = time.NewTicker(time.Duration(watchInterval) * time.Minute).C
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := checkAndNotify(args)
if err != nil {
return err
}
for {
<-ticker
err = checkAndNotify(args)
if err != nil {
return err
}
}
},
}