From 54a5869efde87bf397be6d176d4634ce134cd1a0 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 26 Apr 2018 17:32:20 -0400 Subject: [PATCH] added used of cobra arg constraints --- client/keys/delete.go | 5 +---- client/keys/show.go | 26 ++++++++--------------- client/keys/update.go | 5 +---- client/rpc/block.go | 1 + client/rpc/validators.go | 3 ++- examples/democoin/x/cool/client/cli/tx.go | 11 ++-------- examples/democoin/x/pow/client/cli/tx.go | 8 +------ 7 files changed, 17 insertions(+), 42 deletions(-) diff --git a/client/keys/delete.go b/client/keys/delete.go index cd2016bd20..e9f3c1abe7 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/gorilla/mux" - "github.com/pkg/errors" keys "github.com/tendermint/go-crypto/keys" "github.com/spf13/cobra" @@ -18,14 +17,12 @@ func deleteKeyCommand() *cobra.Command { Use: "delete ", Short: "Delete the given key", RunE: runDeleteCmd, + Args: cobra.ExactArgs(1), } return cmd } func runDeleteCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a name for the key") - } name := args[0] buf := client.BufferStdin() diff --git a/client/keys/show.go b/client/keys/show.go index 76966cd192..d0555764df 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -5,7 +5,6 @@ import ( "net/http" "github.com/gorilla/mux" - "github.com/pkg/errors" keys "github.com/tendermint/go-crypto/keys" "github.com/spf13/cobra" @@ -15,7 +14,15 @@ var showKeysCmd = &cobra.Command{ Use: "show ", Short: "Show key info for the given name", Long: `Return public details of one local key.`, - RunE: runShowCmd, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + name := args[0] + info, err := getKey(name) + if err == nil { + printInfo(info) + } + return err + }, } func getKey(name string) (keys.Info, error) { @@ -27,21 +34,6 @@ func getKey(name string) (keys.Info, error) { return kb.Get(name) } -// CMD - -func runShowCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a name for the key") - } - name := args[0] - - info, err := getKey(name) - if err == nil { - printInfo(info) - } - return err -} - /////////////////////////// // REST diff --git a/client/keys/update.go b/client/keys/update.go index c0edc5fe6e..f3db82c3f4 100644 --- a/client/keys/update.go +++ b/client/keys/update.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/gorilla/mux" - "github.com/pkg/errors" keys "github.com/tendermint/go-crypto/keys" "github.com/spf13/cobra" @@ -18,14 +17,12 @@ func updateKeyCommand() *cobra.Command { Use: "update ", Short: "Change the password used to protect private key", RunE: runUpdateCmd, + Args: cobra.ExactArgs(1), } return cmd } func runUpdateCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a name for the key") - } name := args[0] buf := client.BufferStdin() diff --git a/client/rpc/block.go b/client/rpc/block.go index 12c65fa700..3547270197 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -20,6 +20,7 @@ func blockCommand() *cobra.Command { cmd := &cobra.Command{ Use: "block [height]", Short: "Get verified data for a the block at given height", + Args: cobra.MaximumNArgs(1), RunE: printBlock, } cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") diff --git a/client/rpc/validators.go b/client/rpc/validators.go index f9b1f6f8f8..892d131ac3 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -16,8 +16,9 @@ import ( func validatorCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "validatorset ", + Use: "validatorset [height]", Short: "Get the full validator set at given height", + Args: cobra.MaximumNArgs(1), RunE: printValidators, } cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") diff --git a/examples/democoin/x/cool/client/cli/tx.go b/examples/democoin/x/cool/client/cli/tx.go index 9abcfd6308..f502f09e14 100644 --- a/examples/democoin/x/cool/client/cli/tx.go +++ b/examples/democoin/x/cool/client/cli/tx.go @@ -3,7 +3,6 @@ package cli import ( "fmt" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -20,11 +19,8 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "cool [answer]", Short: "What's cooler than being cool?", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide an answer") - } - ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // get the from address from the name flag @@ -56,11 +52,8 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "setcool [answer]", Short: "You're so cool, tell us what is cool!", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide an answer") - } - ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // get the from address from the name flag diff --git a/examples/democoin/x/pow/client/cli/tx.go b/examples/democoin/x/pow/client/cli/tx.go index 62ee4db696..92f97d4e0d 100644 --- a/examples/democoin/x/pow/client/cli/tx.go +++ b/examples/democoin/x/pow/client/cli/tx.go @@ -4,7 +4,6 @@ import ( "fmt" "strconv" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" @@ -19,13 +18,8 @@ func MineCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "mine [difficulty] [count] [nonce] [solution]", Short: "Mine some coins with proof-of-work!", + Args: cobra.ExactArgs(4), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 4 { - return errors.New("You must provide a difficulty, a count, a solution, and a nonce (in that order)") - } - - // get from address and parse arguments - ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) from, err := ctx.GetFromAddress()