improve command handling

- add alias box for boxes
- expose api host as flag
- allow to set watch through config file
develop
noerw 6 years ago
parent d99e0da559
commit dfbbe9d58f

@ -16,10 +16,11 @@ var checkCmd = &cobra.Command{
}
var checkBoxCmd = &cobra.Command{
Use: "boxes <boxId> [...<boxIds>]",
Short: "one-off check on one or more box IDs",
Long: "specify box IDs to check them for events",
Args: BoxIdValidator,
Use: "boxes <boxId> [...<boxIds>]",
Aliases: []string{"box"},
Short: "one-off check on one or more box IDs",
Long: "specify box IDs to check them for events",
Args: BoxIdValidator,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return checkAndNotify(args)

@ -41,14 +41,19 @@ func init() {
shouldNotify bool
debug bool
logFormat string
api string
)
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "path to config file (default $HOME/.osem_notify.yml)")
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().BoolVarP(&shouldNotify, "notify", "n", false, "if set, will send out notifications.\nOtherwise results are printed to stdout only")
rootCmd.PersistentFlags().BoolVarP(&shouldNotify, "notify", "n", false, `if set, will send out notifications.
Otherwise results are printed to stdout only.
You might want to run 'osem_notify debug notifications' first to verify everything works.
`)
viper.BindPFlags(rootCmd.PersistentFlags()) // let flags override config
}

@ -4,14 +4,20 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var watchInterval int
var ticker <-chan time.Time
func init() {
watchCmd.AddCommand(watchBoxesCmd)
var (
watchInterval int
)
watchCmd.PersistentFlags().IntVarP(&watchInterval, "interval", "i", 15, "interval to run checks in minutes")
viper.BindPFlags(watchCmd.PersistentFlags())
watchCmd.AddCommand(watchBoxesCmd)
rootCmd.AddCommand(watchCmd)
}
@ -23,12 +29,14 @@ var watchCmd = &cobra.Command{
}
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,
Use: "boxes <boxId> [...<boxIds>]",
Aliases: []string{"box"},
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
interval := viper.GetDuration("interval") * time.Minute
ticker = time.NewTicker(interval).C
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

@ -161,8 +161,10 @@ func checkBox(boxId string, defaultConf *NotifyConfig) (*Box, []CheckResult, err
boxLogger := log.WithFields(log.Fields{"boxId": boxId})
boxLogger.Info("checking box for events")
osem := NewOsemClient(viper.GetString("api"))
// get box data
box, err := Osem.GetBox(boxId)
box, err := osem.GetBox(boxId)
if err != nil {
boxLogger.Error(err)
return nil, nil, err

@ -16,9 +16,9 @@ type OsemClient struct {
sling *sling.Sling
}
func NewOsemClient(client *http.Client) *OsemClient {
func NewOsemClient(endpoint string) *OsemClient {
return &OsemClient{
sling: sling.New().Client(client).Base("https://api.opensensemap.org/"),
sling: sling.New().Client(&http.Client{}).Base(endpoint),
}
}
@ -32,4 +32,3 @@ func (client *OsemClient) GetBox(boxId string) (*Box, error) {
return box, nil
}
var Osem = NewOsemClient(&http.Client{}) // default client

Loading…
Cancel
Save