From 15da08dbc000c5580795d0351b56df629f2dcce8 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 19 Jan 2020 08:40:51 -0800 Subject: [PATCH] [ci skip] Allow config file to set default values for flags --- .gitignore | 5 +++- tools/asynqmon/README.md | 52 +++++++++++++++++++++++++++++++++++ tools/asynqmon/cmd/del.go | 6 ++-- tools/asynqmon/cmd/delall.go | 6 ++-- tools/asynqmon/cmd/enq.go | 6 ++-- tools/asynqmon/cmd/enqall.go | 6 ++-- tools/asynqmon/cmd/history.go | 6 ++-- tools/asynqmon/cmd/kill.go | 6 ++-- tools/asynqmon/cmd/killall.go | 6 ++-- tools/asynqmon/cmd/ls.go | 6 ++-- tools/asynqmon/cmd/rmq.go | 6 ++-- tools/asynqmon/cmd/root.go | 18 ++++++------ tools/asynqmon/cmd/stats.go | 6 ++-- 13 files changed, 106 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 8e92ec0..9846a9d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,7 @@ /examples # Ignore command binary -/tools/asynqmon/asynqmon \ No newline at end of file +/tools/asynqmon/asynqmon + +# Ignore asynqmon config file +.asynqmon.* \ No newline at end of file diff --git a/tools/asynqmon/README.md b/tools/asynqmon/README.md index e1e359e..49f5964 100644 --- a/tools/asynqmon/README.md +++ b/tools/asynqmon/README.md @@ -2,6 +2,20 @@ Asynqmon is a CLI tool to monitor the queues managed by `asynq` package. +## Table of Contents + +- [Installation](#installation) +- [Quick Start](#quick-start) + - [Stats](#stats) + - [History](#history) + - [List](#list) + - [Enqueue](#enqueue) + - [Delete](#delete) + - [Kill](#kill) +- [Config File](#config-file) + +## Installation + In order to use the tool, compile it using the following command: go get github.com/hibiken/asynq/tools/asynqmon @@ -29,3 +43,41 @@ Example: This will run `asynqmon stats` command every 3 seconds. ![Gif](/docs/assets/asynqmon_stats.gif) + +### History + +TODO: Add discription + +### List + +TODO: Add discription + +### Enqueue + +TODO: Add discription + +### Delete + +TODO: Add discription + +### Kill + +TODO: Add discription + +## Config File + +You can use a config file to set default values for flags. +This is useful, for example when you have to connect to a remote redis server. + +By default, `asynqmon` will try to read config file located in +`$HOME/.asynqmon.(yml|json)`. You can specify the file location via `--config` flag. + +Config file example: + +```yml +uri: 127.0.0.1:6379 +db: 2 +password: mypassword +``` + +This will set the default values for `--uri`, `--db`, and `--password` flags. diff --git a/tools/asynqmon/cmd/del.go b/tools/asynqmon/cmd/del.go index a85a9b0..28d2169 100644 --- a/tools/asynqmon/cmd/del.go +++ b/tools/asynqmon/cmd/del.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // delCmd represents the del command @@ -49,8 +50,9 @@ func del(cmd *cobra.Command, args []string) { os.Exit(1) } r := rdb.NewRDB(redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), })) switch qtype { case "s": diff --git a/tools/asynqmon/cmd/delall.go b/tools/asynqmon/cmd/delall.go index 860e8cd..ce1b8fc 100644 --- a/tools/asynqmon/cmd/delall.go +++ b/tools/asynqmon/cmd/delall.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var delallValidArgs = []string{"scheduled", "retry", "dead"} @@ -45,8 +46,9 @@ func init() { func delall(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) var err error diff --git a/tools/asynqmon/cmd/enq.go b/tools/asynqmon/cmd/enq.go index 2bcd137..31831b1 100644 --- a/tools/asynqmon/cmd/enq.go +++ b/tools/asynqmon/cmd/enq.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // enqCmd represents the enq command @@ -52,8 +53,9 @@ func enq(cmd *cobra.Command, args []string) { os.Exit(1) } r := rdb.NewRDB(redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), })) switch qtype { case "s": diff --git a/tools/asynqmon/cmd/enqall.go b/tools/asynqmon/cmd/enqall.go index 90586f0..e5e1ab5 100644 --- a/tools/asynqmon/cmd/enqall.go +++ b/tools/asynqmon/cmd/enqall.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var enqallValidArgs = []string{"scheduled", "retry", "dead"} @@ -48,8 +49,9 @@ func init() { func enqall(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) var n int64 diff --git a/tools/asynqmon/cmd/history.go b/tools/asynqmon/cmd/history.go index d716020..5547a42 100644 --- a/tools/asynqmon/cmd/history.go +++ b/tools/asynqmon/cmd/history.go @@ -14,6 +14,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // historyCmd represents the history command @@ -55,8 +56,9 @@ Usage: asynqmon history [num of days] } c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) diff --git a/tools/asynqmon/cmd/kill.go b/tools/asynqmon/cmd/kill.go index 7289048..80986bb 100644 --- a/tools/asynqmon/cmd/kill.go +++ b/tools/asynqmon/cmd/kill.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // killCmd represents the kill command @@ -49,8 +50,9 @@ func kill(cmd *cobra.Command, args []string) { os.Exit(1) } r := rdb.NewRDB(redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), })) switch qtype { case "s": diff --git a/tools/asynqmon/cmd/killall.go b/tools/asynqmon/cmd/killall.go index 7865b59..00c2d3d 100644 --- a/tools/asynqmon/cmd/killall.go +++ b/tools/asynqmon/cmd/killall.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var killallValidArgs = []string{"scheduled", "retry"} @@ -45,8 +46,9 @@ func init() { func killall(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) var n int64 diff --git a/tools/asynqmon/cmd/ls.go b/tools/asynqmon/cmd/ls.go index 1440824..7070dee 100644 --- a/tools/asynqmon/cmd/ls.go +++ b/tools/asynqmon/cmd/ls.go @@ -17,6 +17,7 @@ import ( "github.com/hibiken/asynq/internal/rdb" "github.com/rs/xid" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var lsValidArgs = []string{"enqueued", "inprogress", "scheduled", "retry", "dead"} @@ -58,8 +59,9 @@ func init() { func ls(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) parts := strings.Split(args[0], ":") diff --git a/tools/asynqmon/cmd/rmq.go b/tools/asynqmon/cmd/rmq.go index b8696be..14bdbb3 100644 --- a/tools/asynqmon/cmd/rmq.go +++ b/tools/asynqmon/cmd/rmq.go @@ -11,6 +11,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // rmqCmd represents the rmq command @@ -35,8 +36,9 @@ func init() { func rmq(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c) err := r.RemoveQueue(args[0], rmqForce) diff --git a/tools/asynqmon/cmd/root.go b/tools/asynqmon/cmd/root.go index 9846c7d..1f19c3c 100644 --- a/tools/asynqmon/cmd/root.go +++ b/tools/asynqmon/cmd/root.go @@ -6,9 +6,10 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" + "github.com/spf13/cobra" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) @@ -18,6 +19,7 @@ var cfgFile string // Flags var uri string var db int +var password string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -31,9 +33,6 @@ Monitoring commands such as "stats" and "ls" can be used in conjunction with the "watch" command to continuously run the command at a certain interval. Example: watch -n 5 asynqmon stats`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -48,13 +47,16 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.asynqmon.yaml)") - rootCmd.PersistentFlags().StringVarP(&uri, "uri", "u", "127.0.0.1:6379", "Redis server URI") - rootCmd.PersistentFlags().IntVarP(&db, "db", "n", 0, "Redis database number (default is 0)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file to set flag defaut values (default is $HOME/.asynqmon.yaml)") + rootCmd.PersistentFlags().StringVarP(&uri, "uri", "u", "127.0.0.1:6379", "redis server URI") + rootCmd.PersistentFlags().IntVarP(&db, "db", "n", 0, "redis database number (default is 0)") + rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password to use when connecting to redis server") + viper.BindPFlag("uri", rootCmd.PersistentFlags().Lookup("uri")) + viper.BindPFlag("db", rootCmd.PersistentFlags().Lookup("db")) + viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password")) } // initConfig reads in config file and ENV variables if set. -// TODO(hibiken): Remove this if not necessary. func initConfig() { if cfgFile != "" { // Use config file from the flag. diff --git a/tools/asynqmon/cmd/stats.go b/tools/asynqmon/cmd/stats.go index 3183a9c..6ceb0ea 100644 --- a/tools/asynqmon/cmd/stats.go +++ b/tools/asynqmon/cmd/stats.go @@ -15,6 +15,7 @@ import ( "github.com/go-redis/redis/v7" "github.com/hibiken/asynq/internal/rdb" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // statsCmd represents the stats command @@ -48,8 +49,9 @@ func init() { func stats(cmd *cobra.Command, args []string) { c := redis.NewClient(&redis.Options{ - Addr: uri, - DB: db, + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), }) r := rdb.NewRDB(c)