From d4fb6d4ebe003104722021542a7c3800bda7c69d Mon Sep 17 00:00:00 2001 From: Jack Zampolin Date: Wed, 7 Nov 2018 15:03:00 -0800 Subject: [PATCH] Reorganize CLI command structure. Fixes #2575 --- client/rpc/root.go | 10 +-- client/rpc/status.go | 3 +- client/tx/root.go | 9 --- cmd/gaia/cmd/gaiacli/main.go | 93 +++------------------------ cmd/gaia/cmd/gaiacli/query.go | 81 +++++++++++++++++++++++ cmd/gaia/cmd/gaiacli/tx.go | 83 ++++++++++++++++++++++++ examples/basecoin/cmd/basecli/main.go | 12 ++-- examples/democoin/cmd/democli/main.go | 13 ++-- 8 files changed, 193 insertions(+), 111 deletions(-) create mode 100644 cmd/gaia/cmd/gaiacli/query.go create mode 100644 cmd/gaia/cmd/gaiacli/tx.go diff --git a/client/rpc/root.go b/client/rpc/root.go index 74c5a69a2ed2..6ef33e0e3abf 100644 --- a/client/rpc/root.go +++ b/client/rpc/root.go @@ -22,15 +22,7 @@ func todoNotImplemented(_ *cobra.Command, _ []string) error { return errors.New("todo: Command not yet implemented") } -// AddCommands adds a number of rpc-related subcommands -func AddCommands(cmd *cobra.Command) { - cmd.AddCommand( - initClientCommand(), - statusCommand(), - ) -} - -func initClientCommand() *cobra.Command { +func InitClientCommand() *cobra.Command { cmd := &cobra.Command{ Use: "init", Short: "Initialize light client", diff --git a/client/rpc/status.go b/client/rpc/status.go index 68716f3a0b75..0f81fc9c6f00 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -14,7 +14,8 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" ) -func statusCommand() *cobra.Command { +// StatusCommand returns the status of the network +func StatusCommand() *cobra.Command { cmd := &cobra.Command{ Use: "status", Short: "Query remote node for status", diff --git a/client/tx/root.go b/client/tx/root.go index 0f237b59677e..5cd7eca0b8aa 100644 --- a/client/tx/root.go +++ b/client/tx/root.go @@ -2,20 +2,11 @@ package tx import ( "github.com/gorilla/mux" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" ) -// AddCommands adds a number of tx-query related subcommands -func AddCommands(cmd *cobra.Command, cdc *codec.Codec) { - cmd.AddCommand( - SearchTxCmd(cdc), - QueryTxCmd(cdc), - ) -} - // register REST routes func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) { r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(cdc, cliCtx)).Methods("GET") diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 212a67021404..e4ba7ec65970 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -13,18 +13,10 @@ import ( "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/client/lcd" "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - distrcmd "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" - govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli" - slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" - stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" - _ "github.com/cosmos/cosmos-sdk/client/lcd/statik" ) @@ -40,7 +32,7 @@ const ( var ( rootCmd = &cobra.Command{ Use: "gaiacli", - Short: "Gaia light-client", + Short: "Command line interface for interacting with gaiad", } ) @@ -57,90 +49,23 @@ func main() { // TODO: setup keybase, viper object, etc. to be passed into // the below functions and eliminate global vars, like we do // with the cdc - rootCmd.AddCommand(client.ConfigCmd()) - - // add standard rpc commands - rpc.AddCommands(rootCmd) - - //Add query commands - queryCmd := &cobra.Command{ - Use: "query", - Aliases: []string{"q"}, - Short: "Querying subcommands", - } - queryCmd.AddCommand( - rpc.BlockCommand(), - rpc.ValidatorCommand(), - ) - tx.AddCommands(queryCmd, cdc) - queryCmd.AddCommand(client.LineBreak) - queryCmd.AddCommand(client.GetCommands( - authcmd.GetAccountCmd(storeAcc, cdc, authcmd.GetAccountDecoder(cdc)), - stakecmd.GetCmdQueryDelegation(storeStake, cdc), - stakecmd.GetCmdQueryDelegations(storeStake, cdc), - stakecmd.GetCmdQueryUnbondingDelegation(storeStake, cdc), - stakecmd.GetCmdQueryUnbondingDelegations(storeStake, cdc), - stakecmd.GetCmdQueryRedelegation(storeStake, cdc), - stakecmd.GetCmdQueryRedelegations(storeStake, cdc), - stakecmd.GetCmdQueryValidator(storeStake, cdc), - stakecmd.GetCmdQueryValidators(storeStake, cdc), - stakecmd.GetCmdQueryValidatorUnbondingDelegations(queryRouteStake, cdc), - stakecmd.GetCmdQueryValidatorRedelegations(queryRouteStake, cdc), - stakecmd.GetCmdQueryParams(storeStake, cdc), - stakecmd.GetCmdQueryPool(storeStake, cdc), - govcmd.GetCmdQueryProposal(storeGov, cdc), - govcmd.GetCmdQueryProposals(storeGov, cdc), - govcmd.GetCmdQueryVote(storeGov, cdc), - govcmd.GetCmdQueryVotes(storeGov, cdc), - govcmd.GetCmdQueryDeposit(storeGov, cdc), - govcmd.GetCmdQueryDeposits(storeGov, cdc), - slashingcmd.GetCmdQuerySigningInfo(storeSlashing, cdc), - )...) - - //Add query commands - txCmd := &cobra.Command{ - Use: "tx", - Short: "Transactions subcommands", - } - //Add auth and bank commands - txCmd.AddCommand( - client.PostCommands( - bankcmd.GetBroadcastCommand(cdc), - authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc)), - )...) - txCmd.AddCommand(client.LineBreak) - - txCmd.AddCommand( - client.PostCommands( - stakecmd.GetCmdCreateValidator(cdc), - stakecmd.GetCmdEditValidator(cdc), - stakecmd.GetCmdDelegate(cdc), - stakecmd.GetCmdRedelegate(storeStake, cdc), - stakecmd.GetCmdUnbond(storeStake, cdc), - distrcmd.GetCmdWithdrawRewards(cdc), - distrcmd.GetCmdSetWithdrawAddr(cdc), - govcmd.GetCmdDeposit(cdc), - bankcmd.SendTxCmd(cdc), - govcmd.GetCmdSubmitProposal(cdc), - slashingcmd.GetCmdUnjail(cdc), - govcmd.GetCmdVote(cdc), - )...) + // Consturct Root Command rootCmd.AddCommand( - queryCmd, - txCmd, + rpc.InitClientCommand(), + rpc.StatusCommand(), + client.ConfigCmd(), + queryCmd(cdc), + txCmd(cdc), + client.LineBreak, lcd.ServeCommand(cdc), client.LineBreak, - ) - - // add proxy, version and key info - rootCmd.AddCommand( keys.Commands(), client.LineBreak, version.VersionCmd, ) - // prepare and add flags + // Add flags and prefix all env exposed with GA executor := cli.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome) err := initConfig(rootCmd) if err != nil { diff --git a/cmd/gaia/cmd/gaiacli/query.go b/cmd/gaia/cmd/gaiacli/query.go new file mode 100644 index 000000000000..be3138a35a7c --- /dev/null +++ b/cmd/gaia/cmd/gaiacli/query.go @@ -0,0 +1,81 @@ +package main + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/rpc" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/spf13/cobra" + + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" + stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" + amino "github.com/tendermint/go-amino" +) + +func queryCmd(cdc *amino.Codec) *cobra.Command { + //Add query commands + queryCmd := &cobra.Command{ + Use: "query", + Aliases: []string{"q"}, + Short: "Querying subcommands", + } + + // Group staking queries under a subcommand + stakeQueryCmd := &cobra.Command{ + Use: "stake", + Short: "Querying commands for staking module", + } + + stakeQueryCmd.AddCommand(client.GetCommands( + stakecmd.GetCmdQueryDelegation(storeStake, cdc), + stakecmd.GetCmdQueryDelegations(storeStake, cdc), + stakecmd.GetCmdQueryUnbondingDelegation(storeStake, cdc), + stakecmd.GetCmdQueryUnbondingDelegations(storeStake, cdc), + stakecmd.GetCmdQueryRedelegation(storeStake, cdc), + stakecmd.GetCmdQueryRedelegations(storeStake, cdc), + stakecmd.GetCmdQueryValidator(storeStake, cdc), + stakecmd.GetCmdQueryValidators(storeStake, cdc), + stakecmd.GetCmdQueryValidatorUnbondingDelegations(queryRouteStake, cdc), + stakecmd.GetCmdQueryValidatorRedelegations(queryRouteStake, cdc), + stakecmd.GetCmdQueryParams(storeStake, cdc), + stakecmd.GetCmdQueryPool(storeStake, cdc))...) + + // Group gov queries under a subcommand + govQueryCmd := &cobra.Command{ + Use: "gov", + Short: "Querying commands for gov module", + } + + govQueryCmd.AddCommand(client.GetCommands( + govcmd.GetCmdQueryProposal(storeGov, cdc), + govcmd.GetCmdQueryProposals(storeGov, cdc), + govcmd.GetCmdQueryVote(storeGov, cdc), + govcmd.GetCmdQueryVotes(storeGov, cdc), + govcmd.GetCmdQueryDeposit(storeGov, cdc), + govcmd.GetCmdQueryDeposits(storeGov, cdc))...) + + // Group slashing queries under a subcommand + slashingQueryCmd := &cobra.Command{ + Use: "slashing", + Short: "Querying commands for slashing module", + } + + slashingQueryCmd.AddCommand(client.GetCommands( + slashingcmd.GetCmdQuerySigningInfo(storeSlashing, cdc))...) + + // Query commcmmand sturcture + queryCmd.AddCommand( + rpc.BlockCommand(), + rpc.ValidatorCommand(), + tx.SearchTxCmd(cdc), + tx.QueryTxCmd(cdc), + client.LineBreak, + authcmd.GetAccountCmd(storeAcc, cdc, authcmd.GetAccountDecoder(cdc)), + stakeQueryCmd, + govQueryCmd, + slashingQueryCmd, + ) + + return queryCmd +} diff --git a/cmd/gaia/cmd/gaiacli/tx.go b/cmd/gaia/cmd/gaiacli/tx.go new file mode 100644 index 000000000000..892e6ae7c45d --- /dev/null +++ b/cmd/gaia/cmd/gaiacli/tx.go @@ -0,0 +1,83 @@ +package main + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/spf13/cobra" + + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + distrcmd "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" + govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" + stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" + amino "github.com/tendermint/go-amino" +) + +func txCmd(cdc *amino.Codec) *cobra.Command { + //Add transaction generation commands + txCmd := &cobra.Command{ + Use: "tx", + Short: "Transactions subcommands", + } + + stakeTxCmd := &cobra.Command{ + Use: "stake", + Short: "Stake Transactions subcommands", + } + + stakeTxCmd.AddCommand(client.PostCommands( + stakecmd.GetCmdCreateValidator(cdc), + stakecmd.GetCmdEditValidator(cdc), + stakecmd.GetCmdDelegate(cdc), + stakecmd.GetCmdRedelegate(storeStake, cdc), + stakecmd.GetCmdUnbond(storeStake, cdc), + )...) + + distTxCmd := &cobra.Command{ + Use: "dist", + Short: "Distribution Transactions subcommands", + } + + distTxCmd.AddCommand(client.PostCommands( + distrcmd.GetCmdWithdrawRewards(cdc), + distrcmd.GetCmdSetWithdrawAddr(cdc), + )...) + + govTxCmd := &cobra.Command{ + Use: "gov", + Short: "Governance Transactions subcommands", + } + + govTxCmd.AddCommand(client.PostCommands( + govcmd.GetCmdDeposit(cdc), + govcmd.GetCmdVote(cdc), + govcmd.GetCmdSubmitProposal(cdc), + )...) + + slashingTxCmd := &cobra.Command{ + Use: "slashing", + Short: "Slashing Transactions subcommands", + } + + slashingTxCmd.AddCommand(client.PostCommands( + slashingcmd.GetCmdUnjail(cdc), + )...) + + txCmd.AddCommand( + //Add auth and bank commands + client.PostCommands( + bankcmd.SendTxCmd(cdc), + bankcmd.GetBroadcastCommand(cdc), + authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc)), + )...) + + txCmd.AddCommand( + client.LineBreak, + stakeTxCmd, + distTxCmd, + govTxCmd, + slashingTxCmd, + ) + + return txCmd +} diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 1732e82eb916..4ebd1bef8476 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -39,10 +39,14 @@ func main() { // with the cdc. // add standard rpc, and tx commands - rpc.AddCommands(rootCmd) - rootCmd.AddCommand(client.LineBreak) - tx.AddCommands(rootCmd, cdc) - rootCmd.AddCommand(client.LineBreak) + rootCmd.AddCommand( + rpc.InitClientCommand(), + rpc.StatusCommand(), + client.LineBreak, + tx.SearchTxCmd(cdc), + tx.QueryTxCmd(cdc), + client.LineBreak, + ) // add query/post commands (custom to binary) rootCmd.AddCommand( diff --git a/examples/democoin/cmd/democli/main.go b/examples/democoin/cmd/democli/main.go index 6bb2da868083..4121fa30b5e3 100644 --- a/examples/democoin/cmd/democli/main.go +++ b/examples/democoin/cmd/democli/main.go @@ -52,10 +52,15 @@ func main() { // with the cdc // add standard rpc, and tx commands - rpc.AddCommands(rootCmd) - rootCmd.AddCommand(client.LineBreak) - tx.AddCommands(rootCmd, cdc) - rootCmd.AddCommand(client.LineBreak) + + rootCmd.AddCommand( + rpc.InitClientCommand(), + rpc.StatusCommand(), + client.LineBreak, + tx.SearchTxCmd(cdc), + tx.QueryTxCmd(cdc), + client.LineBreak, + ) // add query/post commands (custom to binary) // start with commands common to basecoin