Merge pull request #921 from cosmos/rigel/885-exact-args

cobra arg constraints
This commit is contained in:
Christopher Goes 2018-04-27 13:01:14 +02:00 committed by GitHub
commit b5f9a6873b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 17 additions and 42 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors"
keys "github.com/tendermint/go-crypto/keys" keys "github.com/tendermint/go-crypto/keys"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -18,14 +17,12 @@ func deleteKeyCommand() *cobra.Command {
Use: "delete <name>", Use: "delete <name>",
Short: "Delete the given key", Short: "Delete the given key",
RunE: runDeleteCmd, RunE: runDeleteCmd,
Args: cobra.ExactArgs(1),
} }
return cmd return cmd
} }
func runDeleteCmd(cmd *cobra.Command, args []string) error { 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] name := args[0]
buf := client.BufferStdin() buf := client.BufferStdin()

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors"
keys "github.com/tendermint/go-crypto/keys" keys "github.com/tendermint/go-crypto/keys"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -15,7 +14,15 @@ var showKeysCmd = &cobra.Command{
Use: "show <name>", Use: "show <name>",
Short: "Show key info for the given name", Short: "Show key info for the given name",
Long: `Return public details of one local key.`, 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) { func getKey(name string) (keys.Info, error) {
@ -27,21 +34,6 @@ func getKey(name string) (keys.Info, error) {
return kb.Get(name) 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 // REST

View File

@ -7,7 +7,6 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors"
keys "github.com/tendermint/go-crypto/keys" keys "github.com/tendermint/go-crypto/keys"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -18,14 +17,12 @@ func updateKeyCommand() *cobra.Command {
Use: "update <name>", Use: "update <name>",
Short: "Change the password used to protect private key", Short: "Change the password used to protect private key",
RunE: runUpdateCmd, RunE: runUpdateCmd,
Args: cobra.ExactArgs(1),
} }
return cmd return cmd
} }
func runUpdateCmd(cmd *cobra.Command, args []string) error { 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] name := args[0]
buf := client.BufferStdin() buf := client.BufferStdin()

View File

@ -20,6 +20,7 @@ func blockCommand() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "block [height]", Use: "block [height]",
Short: "Get verified data for a the block at given height", Short: "Get verified data for a the block at given height",
Args: cobra.MaximumNArgs(1),
RunE: printBlock, RunE: printBlock,
} }
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")

View File

@ -16,8 +16,9 @@ import (
func validatorCommand() *cobra.Command { func validatorCommand() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "validatorset <height>", Use: "validatorset [height]",
Short: "Get the full validator set at given height", Short: "Get the full validator set at given height",
Args: cobra.MaximumNArgs(1),
RunE: printValidators, RunE: printValidators,
} }
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")

View File

@ -3,7 +3,6 @@ package cli
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -20,11 +19,8 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "cool [answer]", Use: "cool [answer]",
Short: "What's cooler than being cool?", Short: "What's cooler than being cool?",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { 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)) ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag // get the from address from the name flag
@ -56,11 +52,8 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "setcool [answer]", Use: "setcool [answer]",
Short: "You're so cool, tell us what is cool!", Short: "You're so cool, tell us what is cool!",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { 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)) ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag // get the from address from the name flag

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
@ -19,13 +18,8 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "mine [difficulty] [count] [nonce] [solution]", Use: "mine [difficulty] [count] [nonce] [solution]",
Short: "Mine some coins with proof-of-work!", Short: "Mine some coins with proof-of-work!",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error { 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)) ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
from, err := ctx.GetFromAddress() from, err := ctx.GetFromAddress()