diff --git a/client/flags.go b/client/flags.go new file mode 100644 index 000000000000..e89c49cf8928 --- /dev/null +++ b/client/flags.go @@ -0,0 +1,33 @@ +package client + +import "github.com/spf13/cobra" + +const ( + FlagChainID = "chain-id" + FlagNode = "node" + FlagHeight = "height" + FlagTrustNode = "trust-node" + FlagName = "name" +) + +// GetCommands adds common flags to query commands +func GetCommands(cmds ...*cobra.Command) []*cobra.Command { + for _, c := range cmds { + // TODO: make this default false when we support proofs + c.Flags().Bool(FlagTrustNode, true, "Don't verify proofs for responses") + c.Flags().String(FlagChainID, "", "Chain ID of tendermint node") + c.Flags().String(FlagNode, "", ": to tendermint rpc interface for this chain") + c.Flags().Int64(FlagHeight, 0, "block height to query, omit to get most recent provable block") + } + return cmds +} + +// PostCommands adds common flags for commands to post tx +func PostCommands(cmds ...*cobra.Command) []*cobra.Command { + for _, c := range cmds { + c.Flags().String(FlagName, "", "Name of private key with which to sign") + c.Flags().String(FlagChainID, "", "Chain ID of tendermint node") + c.Flags().String(FlagNode, "", ": to tendermint rpc interface for this chain") + } + return cmds +} diff --git a/examples/basecoin/cmd/basecli/client.go b/examples/basecoin/cmd/basecli/client.go index 736ce4b4e280..26ee87375d4e 100644 --- a/examples/basecoin/cmd/basecli/client.go +++ b/examples/basecoin/cmd/basecli/client.go @@ -1,28 +1,23 @@ package main -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" -const ( - // these are needed for every init - flagChainID = "chain-id" - flagNode = "node" + "github.com/cosmos/cosmos-sdk/client" +) +const ( // one of the following should be provided to verify the connection flagGenesis = "genesis" flagCommit = "commit" flagValHash = "validator-set" - flagHeight = "height" flagSelect = "select" flagTags = "tag" flagAny = "any" - flagBind = "bind" - flagCORS = "cors" - flagTrustNode = "trust-node" - - // this is for signing - flagName = "name" + flagBind = "bind" + flagCORS = "cors" ) var ( @@ -49,36 +44,14 @@ func AddClientCommands(cmd *cobra.Command) { ) } -// GetCommands adds common flags to query commands -func GetCommands(cmds ...*cobra.Command) []*cobra.Command { - for _, c := range cmds { - // TODO: make this default false when we support proofs - c.Flags().Bool(flagTrustNode, true, "Don't verify proofs for responses") - c.Flags().String(flagChainID, "", "Chain ID of tendermint node") - c.Flags().String(flagNode, "", ": to tendermint rpc interface for this chain") - c.Flags().Int64(flagHeight, 0, "block height to query, omit to get most recent provable block") - } - return cmds -} - -// PostCommands adds common flags for commands to post tx -func PostCommands(cmds ...*cobra.Command) []*cobra.Command { - for _, c := range cmds { - c.Flags().String(flagName, "", "Name of private key with which to sign") - c.Flags().String(flagChainID, "", "Chain ID of tendermint node") - c.Flags().String(flagNode, "", ": to tendermint rpc interface for this chain") - } - return cmds -} - func initClientCommand() *cobra.Command { cmd := &cobra.Command{ Use: "init", Short: "Initialize light client", RunE: todoNotImplemented, } - cmd.Flags().StringP(flagChainID, "c", "", "ID of chain we connect to") - cmd.Flags().StringP(flagNode, "n", "tcp://localhost:46657", "Node to connect to") + cmd.Flags().StringP(client.FlagChainID, "c", "", "ID of chain we connect to") + cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") cmd.Flags().String(flagGenesis, "", "Genesis file to verify header validity") cmd.Flags().String(flagCommit, "", "File with trusted and signed header") cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)") diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 5ba5450157b7..f12dc8c65f7b 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -8,8 +8,10 @@ import ( "github.com/tendermint/tmlibs/cli" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/version" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands" ) // gaiacliCmd is the entry point for this binary @@ -35,9 +37,9 @@ func main() { // query/post commands (custom to binary) basecliCmd.AddCommand( - GetCommands(getAccountCmd())...) + client.GetCommands(bankcmd.GetAccountCmd())...) basecliCmd.AddCommand( - PostCommands(postSendCommand())...) + client.PostCommands(bankcmd.SendTxCommand())...) // add proxy, version and key info basecliCmd.AddCommand( diff --git a/examples/basecoin/cmd/basecli/account.go b/x/bank/commands/account.go similarity index 85% rename from examples/basecoin/cmd/basecli/account.go rename to x/bank/commands/account.go index 118e37b24f65..c805d44c21bb 100644 --- a/examples/basecoin/cmd/basecli/account.go +++ b/x/bank/commands/account.go @@ -1,4 +1,4 @@ -package main +package commands import ( "encoding/hex" @@ -17,7 +17,9 @@ import ( "github.com/cosmos/cosmos-sdk/examples/basecoin/types" ) -func getAccountCmd() *cobra.Command { +// GetAccountCmd returns a query account that will display the +// state of the account at a given address +func GetAccountCmd() *cobra.Command { cmd := &cobra.Command{ Use: "account
", Short: "Query account balance", @@ -42,16 +44,15 @@ func getAccount(cmd *cobra.Command, args []string) error { // TODO: make the store name a variable in getAccountCmd? path := "/main/key" - uri := viper.GetString(flagNode) + uri := viper.GetString(client.FlagNode) if uri == "" { return errors.New("Must define which node to query with --node") } node := client.GetNode(uri) opts := rpcclient.ABCIQueryOptions{ - Height: viper.GetInt64(flagHeight), - // Trusted: viper.GetBool(flagTrustNode), - Trusted: true, + Height: viper.GetInt64(client.FlagHeight), + Trusted: viper.GetBool(client.FlagTrustNode), } result, err := node.ABCIQueryWithOptions(path, key, opts) if err != nil { diff --git a/examples/basecoin/cmd/basecli/sendtx.go b/x/bank/commands/sendtx.go similarity index 93% rename from examples/basecoin/cmd/basecli/sendtx.go rename to x/bank/commands/sendtx.go index 8eff14b6a186..011d2877d8b9 100644 --- a/examples/basecoin/cmd/basecli/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -1,4 +1,4 @@ -package main +package commands import ( "encoding/hex" @@ -23,7 +23,8 @@ const ( flagSequence = "seq" ) -func postSendCommand() *cobra.Command { +// SendTxCommand will create a send tx and sign it with the given key +func SendTxCommand() *cobra.Command { cmd := &cobra.Command{ Use: "send", Short: "Create and sign a send tx", @@ -42,7 +43,7 @@ func sendTx(cmd *cobra.Command, args []string) error { return err } - uri := viper.GetString(flagNode) + uri := viper.GetString(client.FlagNode) if uri == "" { return errors.New("Must define which node to query with --node") } @@ -75,7 +76,7 @@ func buildTx() ([]byte, error) { return nil, err } - name := viper.GetString(flagName) + name := viper.GetString(client.FlagName) info, err := keybase.Get(name) if err != nil { return nil, errors.WithMessage(err, "No key for name")