Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Standardize CLI Exports from Modules #2840

Merged
merged 13 commits into from
Nov 19, 2018
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FEATURES
* [gov][cli] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Added governance
parameter query commands.
* [stake][cli] [\#2027] Add CLI query command for getting all delegations to a specific validator.
* [\#2840](https://github.com/cosmos/cosmos-sdk/pull/2840) Standardize CLI exports from modules

* Gaia
* [app] \#2791 Support export at a specific height, with `gaiad export --height=HEIGHT`.
Expand Down
16 changes: 14 additions & 2 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,22 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext {
return ctx
}

// GetAccountDecoder gets the account decoder for auth.DefaultAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
return func(accBytes []byte) (acct auth.Account, err error) {
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
}

return acct, err
}
}

// WithAccountDecoder returns a copy of the context with an updated account
// decoder.
func (ctx CLIContext) WithAccountDecoder(decoder auth.AccountDecoder) CLIContext {
ctx.AccDecoder = decoder
func (ctx CLIContext) WithAccountDecoder(cdc *codec.Codec) CLIContext {
ctx.AccDecoder = GetAccountDecoder(cdc)
return ctx
}

Expand Down
3 changes: 3 additions & 0 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
tmserver "github.com/tendermint/tendermint/rpc/lib/server"

// Import statik for light client stuff
_ "github.com/cosmos/cosmos-sdk/client/lcd/statik"
)

const (
Expand Down
94 changes: 75 additions & 19 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
package main

import (
"fmt"
"os"
"path"

"github.com/spf13/cobra"
"github.com/spf13/viper"

amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"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"

_ "github.com/cosmos/cosmos-sdk/client/lcd/statik"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
distClient "github.com/cosmos/cosmos-sdk/x/distribution/client"
govClient "github.com/cosmos/cosmos-sdk/x/gov/client"
slashingClient "github.com/cosmos/cosmos-sdk/x/slashing/client"
stakeClient "github.com/cosmos/cosmos-sdk/x/stake/client"
)

const (
storeAcc = "acc"
storeGov = "gov"
storeSlashing = "slashing"
storeStake = "stake"
queryRouteStake = "stake"
)

// rootCmd is the entry point for this binary
var (
rootCmd = &cobra.Command{
Use: "gaiacli",
Short: "Command line interface for interacting with gaiad",
}
storeAcc = "acc"
storeGov = "gov"
storeSlashing = "slashing"
storeStake = "stake"
)

func main() {
Expand All @@ -50,13 +48,27 @@ func main() {
// the below functions and eliminate global vars, like we do
// with the cdc

// Module clients hold cli commnads (tx,query) and lcd routes
// TODO: Make the lcd command take a list of ModuleClient
mc := []sdk.ModuleClients{
govClient.NewModuleClient(storeGov, cdc),
distClient.NewModuleClient("", cdc),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why empty string? Do we never query the distribution store?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is currently no query functionality in the dist module. Rigel is working on that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do already know the name of the store key though

stakeClient.NewModuleClient(storeStake, cdc),
slashingClient.NewModuleClient(storeSlashing, cdc),
}

rootCmd := &cobra.Command{
Use: "gaiacli",
Short: "Command line interface for interacting with gaiad",
}

// Construct Root Command
rootCmd.AddCommand(
rpc.InitClientCommand(),
rpc.StatusCommand(),
client.ConfigCmd(),
queryCmd(cdc),
txCmd(cdc),
queryCmd(cdc, mc),
txCmd(cdc, mc),
client.LineBreak,
lcd.ServeCommand(cdc),
client.LineBreak,
Expand All @@ -74,9 +86,53 @@ func main() {

err = executor.Execute()
if err != nil {
// handle with #870
panic(err)
fmt.Printf("Failed executing CLI command: %s, exiting...\n", err)
os.Exit(1)
}
}

func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
}

queryCmd.AddCommand(
rpc.ValidatorCommand(),
rpc.BlockCommand(),
tx.SearchTxCmd(cdc),
tx.QueryTxCmd(cdc),
client.LineBreak,
authcmd.GetAccountCmd(storeAcc, cdc),
)

for _, m := range mc {
queryCmd.AddCommand(m.GetQueryCmd())
}

return queryCmd
}

func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}

txCmd.AddCommand(
bankcmd.SendTxCmd(cdc),
client.LineBreak,
authcmd.GetSignCommand(cdc),
bankcmd.GetBroadcastCommand(cdc),
client.LineBreak,
)

for _, m := range mc {
txCmd.AddCommand(m.GetTxCmd())
}

return txCmd
}

func initConfig(cmd *cobra.Command) error {
Expand Down
83 changes: 0 additions & 83 deletions cmd/gaia/cmd/gaiacli/query.go

This file was deleted.

83 changes: 0 additions & 83 deletions cmd/gaia/cmd/gaiacli/tx.go

This file was deleted.

9 changes: 5 additions & 4 deletions cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package init

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -16,9 +20,6 @@ import (
"github.com/tendermint/tendermint/crypto"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/common"
"io/ioutil"
"os"
"path/filepath"
)

const (
Expand Down Expand Up @@ -85,7 +86,7 @@ following delegation and commission default parameters:
w.Close()

prepareFlagsForTxSign()
signCmd := authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc))
signCmd := authcmd.GetSignCommand(cdc)
if w, err = prepareOutputFile(config.RootDir, nodeID); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/docs/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/docs/examples/basecoin/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"
Expand Down Expand Up @@ -64,7 +63,7 @@ func main() {
stakecmd.GetCmdQueryRedelegation("stake", cdc),
stakecmd.GetCmdQueryRedelegations("stake", cdc),
slashingcmd.GetCmdQuerySigningInfo("slashing", cdc),
authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)),
authcmd.GetAccountCmd("acc", cdc),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "acc" be a constant somewhere (storeAcc maybe)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know really. This is how I found the code. I've opened this issue to track. I'll update to make it a const.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

)...)

rootCmd.AddCommand(
Expand Down
Loading