Skip to content

Commit

Permalink
Reorganize CLI command structure. Fixes #2575
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzampolin committed Nov 7, 2018
1 parent 8f690b5 commit d4fb6d4
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 111 deletions.
10 changes: 1 addition & 9 deletions client/rpc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 0 additions & 9 deletions client/tx/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
93 changes: 9 additions & 84 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -40,7 +32,7 @@ const (
var (
rootCmd = &cobra.Command{
Use: "gaiacli",
Short: "Gaia light-client",
Short: "Command line interface for interacting with gaiad",
}
)

Expand All @@ -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 {
Expand Down
81 changes: 81 additions & 0 deletions cmd/gaia/cmd/gaiacli/query.go
Original file line number Diff line number Diff line change
@@ -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
}
83 changes: 83 additions & 0 deletions cmd/gaia/cmd/gaiacli/tx.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 8 additions & 4 deletions examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 9 additions & 4 deletions examples/democoin/cmd/democli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4fb6d4

Please sign in to comment.