x/ibc: refactor CLI (#6639)
* x/ibc: refactor CLI * refactor client CLI * rename version ClientName * Update x/ibc/09-localhost/client/cli/cli.go Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * address comments from review Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
This commit is contained in:
co-authored by
Amaury Martiny
parent
e50c8df10c
commit
e6bb2e7ed7
@@ -4,22 +4,21 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types"
|
||||
)
|
||||
|
||||
// GetTxCmd returns the transaction commands for IBC
|
||||
func GetTxCmd(cdc *codec.Codec, storeKey string) *cobra.Command {
|
||||
ics09LocalhostTxCmd := &cobra.Command{
|
||||
// NewTxCmd returns a root CLI command handler for all ibc localhost transaction commands.
|
||||
func NewTxCmd() *cobra.Command {
|
||||
txCmd := &cobra.Command{
|
||||
Use: types.SubModuleName,
|
||||
Short: "Localhost transaction subcommands",
|
||||
Short: "Localhost client transaction subcommands",
|
||||
DisableFlagParsing: true,
|
||||
SuggestionsMinimumDistance: 2,
|
||||
}
|
||||
|
||||
ics09LocalhostTxCmd.AddCommand(flags.PostCommands(
|
||||
GetCmdCreateClient(cdc),
|
||||
txCmd.AddCommand(flags.PostCommands(
|
||||
NewCreateClientCmd(),
|
||||
)...)
|
||||
|
||||
return ics09LocalhostTxCmd
|
||||
return txCmd
|
||||
}
|
||||
|
||||
@@ -1,45 +1,39 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/client/tx"
|
||||
"github.com/cosmos/cosmos-sdk/version"
|
||||
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types"
|
||||
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
|
||||
)
|
||||
|
||||
// GetCmdCreateClient defines the command to create a new IBC Client as defined
|
||||
// NewCreateClientCmd defines the command to create a new IBC Loopback Client as defined
|
||||
// in https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics#create
|
||||
func GetCmdCreateClient(cdc *codec.Codec) *cobra.Command {
|
||||
func NewCreateClientCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "create new localhost client",
|
||||
Long: strings.TrimSpace(fmt.Sprintf(`create new localhost (loopback) client:
|
||||
Example:
|
||||
$ %s tx ibc client localhost create --from node0 --home ../node0/<app>cli --chain-id $CID
|
||||
`, version.AppName),
|
||||
),
|
||||
Args: cobra.ExactArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
|
||||
clientCtx := client.NewContextWithInput(inBuf).WithCodec(cdc).WithBroadcastMode(flags.BroadcastBlock)
|
||||
Use: "create",
|
||||
Short: "create new localhost client",
|
||||
Long: "create new localhost (loopback) client",
|
||||
Example: fmt.Sprintf("%s tx %s %s create --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, host.ModuleName, types.SubModuleName),
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
clientCtx := client.GetClientContextFromCmd(cmd)
|
||||
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := types.NewMsgCreateClient(clientCtx.GetFromAddress())
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return authclient.GenerateOrBroadcastMsgs(clientCtx, txBldr, []sdk.Msg{msg})
|
||||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
|
||||
Reference in New Issue
Block a user