Merge PR #6609: x/auth: CLI Remove Viper
This commit is contained in:
parent
bbe245ac1d
commit
feb69770ef
@ -27,7 +27,7 @@ $ <appcli> tx broadcast ./mytxn.json
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx = clientCtx.Init()
|
||||
|
||||
if clientCtx.Offline {
|
||||
if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
|
||||
return errors.New("cannot broadcast tx during offline mode")
|
||||
}
|
||||
|
||||
|
||||
@ -1,29 +1,29 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
||||
"github.com/cosmos/cosmos-sdk/tests"
|
||||
)
|
||||
|
||||
func TestGetBroadcastCommand_OfflineFlag(t *testing.T) {
|
||||
clientCtx := client.Context{}
|
||||
clientCtx := client.Context{}.WithOffline(true)
|
||||
clientCtx = clientCtx.WithTxGenerator(simappparams.MakeEncodingConfig().TxGenerator)
|
||||
|
||||
cmd := GetBroadcastCommand(clientCtx)
|
||||
cmd.SetOut(ioutil.Discard)
|
||||
cmd.SetErr(ioutil.Discard)
|
||||
cmd.SetArgs([]string{fmt.Sprintf("--%s=true", flags.FlagOffline), ""})
|
||||
|
||||
viper.Set(flags.FlagOffline, true)
|
||||
|
||||
err := cmd.RunE(nil, []string{})
|
||||
require.EqualError(t, err, "cannot broadcast tx during offline mode")
|
||||
require.EqualError(t, cmd.Execute(), "cannot broadcast tx during offline mode")
|
||||
}
|
||||
|
||||
func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) {
|
||||
@ -31,8 +31,6 @@ func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) {
|
||||
clientCtx = clientCtx.WithTxGenerator(simappparams.MakeEncodingConfig().TxGenerator)
|
||||
cmd := GetBroadcastCommand(clientCtx)
|
||||
|
||||
viper.Set(flags.FlagOffline, false)
|
||||
|
||||
testDir, cleanFunc := tests.NewTestCaseDir(t)
|
||||
t.Cleanup(cleanFunc)
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
@ -13,39 +12,35 @@ import (
|
||||
|
||||
const flagHex = "hex"
|
||||
|
||||
// GetDecodeCommand returns the decode command to take serialized bytes
|
||||
// and turn it into a JSONified transaction.
|
||||
// GetDecodeCommand returns the decode command to take serialized bytes and turn
|
||||
// it into a JSON-encoded transaction.
|
||||
func GetDecodeCommand(clientCtx client.Context) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "decode [amino-byte-string]",
|
||||
Short: "Decode an binary encoded transaction string.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runDecodeTxString(clientCtx),
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
clientCtx = clientCtx.Init().WithOutput(cmd.OutOrStdout())
|
||||
var txBytes []byte
|
||||
|
||||
if useHex, _ := cmd.Flags().GetBool(flagHex); useHex {
|
||||
txBytes, err = hex.DecodeString(args[0])
|
||||
} else {
|
||||
txBytes, err = base64.StdEncoding.DecodeString(args[0])
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := clientCtx.TxGenerator.TxDecoder()(txBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintOutput(tx)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
|
||||
return flags.PostCommands(cmd)[0]
|
||||
}
|
||||
|
||||
func runDecodeTxString(clientCtx client.Context) func(cmd *cobra.Command, args []string) (err error) {
|
||||
return func(cmd *cobra.Command, args []string) (err error) {
|
||||
clientCtx = clientCtx.Init().WithOutput(cmd.OutOrStdout())
|
||||
var txBytes []byte
|
||||
|
||||
if viper.GetBool(flagHex) {
|
||||
txBytes, err = hex.DecodeString(args[0])
|
||||
} else {
|
||||
txBytes, err = base64.StdEncoding.DecodeString(args[0])
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := clientCtx.TxGenerator.TxDecoder()(txBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintOutput(tx)
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -15,12 +16,14 @@ import (
|
||||
|
||||
func TestGetCommandEncode(t *testing.T) {
|
||||
encodingConfig := simappparams.MakeEncodingConfig()
|
||||
clientCtx := client.Context{}
|
||||
clientCtx = clientCtx.
|
||||
clientCtx := client.Context{}.
|
||||
WithTxGenerator(encodingConfig.TxGenerator).
|
||||
WithJSONMarshaler(encodingConfig.Marshaler)
|
||||
|
||||
cmd := GetEncodeCommand(clientCtx)
|
||||
cmd.SetErr(ioutil.Discard)
|
||||
cmd.SetOut(ioutil.Discard)
|
||||
|
||||
authtypes.RegisterCodec(encodingConfig.Amino)
|
||||
sdk.RegisterCodec(encodingConfig.Amino)
|
||||
|
||||
@ -43,12 +46,13 @@ func TestGetCommandEncode(t *testing.T) {
|
||||
func TestGetCommandDecode(t *testing.T) {
|
||||
encodingConfig := simappparams.MakeEncodingConfig()
|
||||
|
||||
clientCtx := client.Context{}
|
||||
clientCtx = clientCtx.
|
||||
clientCtx := client.Context{}.
|
||||
WithTxGenerator(encodingConfig.TxGenerator).
|
||||
WithJSONMarshaler(encodingConfig.Marshaler)
|
||||
|
||||
cmd := GetDecodeCommand(clientCtx)
|
||||
cmd.SetErr(ioutil.Discard)
|
||||
cmd.SetOut(ioutil.Discard)
|
||||
|
||||
sdk.RegisterCodec(encodingConfig.Amino)
|
||||
|
||||
@ -67,6 +71,6 @@ func TestGetCommandDecode(t *testing.T) {
|
||||
base64Encoded := base64.StdEncoding.EncodeToString(txBytes)
|
||||
|
||||
// Execute the command
|
||||
err = runDecodeTxString(clientCtx)(cmd, []string{base64Encoded})
|
||||
require.NoError(t, err)
|
||||
cmd.SetArgs([]string{base64Encoded})
|
||||
require.NoError(t, cmd.Execute())
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
@ -118,7 +117,8 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
|
||||
`, eventFormat, version.ClientName, flagEvents),
|
||||
),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
eventsStr := strings.Trim(viper.GetString(flagEvents), "'")
|
||||
eventsRaw, _ := cmd.Flags().GetString(flagEvents)
|
||||
eventsStr := strings.Trim(eventsRaw, "'")
|
||||
|
||||
var events []string
|
||||
if strings.Contains(eventsStr, "&") {
|
||||
@ -146,8 +146,8 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
|
||||
tmEvents = append(tmEvents, event)
|
||||
}
|
||||
|
||||
page := viper.GetInt(flags.FlagPage)
|
||||
limit := viper.GetInt(flags.FlagLimit)
|
||||
page, _ := cmd.Flags().GetInt(flags.FlagPage)
|
||||
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
|
||||
|
||||
clientCtx := client.NewContext().WithCodec(cdc)
|
||||
txs, err := authclient.QueryTxsByEvents(clientCtx, tmEvents, page, limit, "")
|
||||
@ -166,17 +166,11 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
|
||||
}
|
||||
|
||||
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
||||
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
||||
|
||||
cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
||||
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))
|
||||
|
||||
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
|
||||
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))
|
||||
|
||||
cmd.Flags().Int(flags.FlagPage, rest.DefaultPage, "Query a specific page of paginated results")
|
||||
cmd.Flags().Int(flags.FlagLimit, rest.DefaultLimit, "Query number of transactions results per page returned")
|
||||
cmd.Flags().String(flagEvents, "", fmt.Sprintf("list of transaction events in the form of %s", eventFormat))
|
||||
cmd.Flags().Uint32(flags.FlagPage, rest.DefaultPage, "Query a specific page of paginated results")
|
||||
cmd.Flags().Uint32(flags.FlagLimit, rest.DefaultLimit, "Query number of transactions results per page returned")
|
||||
cmd.MarkFlagRequired(flagEvents)
|
||||
|
||||
return cmd
|
||||
@ -205,11 +199,8 @@ func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
|
||||
}
|
||||
|
||||
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
||||
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
||||
cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
||||
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))
|
||||
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
|
||||
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
@ -52,7 +51,6 @@ recommended to set such parameters manually.
|
||||
cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit")
|
||||
cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT")
|
||||
|
||||
// Add the flags here and return the command
|
||||
return flags.PostCommands(cmd)[0]
|
||||
}
|
||||
|
||||
@ -66,9 +64,11 @@ func makeMultiSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []
|
||||
return
|
||||
}
|
||||
|
||||
backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
||||
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
|
||||
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
kb, err := keyring.New(sdk.KeyringServiceName(),
|
||||
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), inBuf)
|
||||
kb, err := keyring.New(sdk.KeyringServiceName(), backend, homeDir, inBuf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -130,7 +130,7 @@ func makeMultiSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []
|
||||
|
||||
var json []byte
|
||||
|
||||
sigOnly := viper.GetBool(flagSigOnly)
|
||||
sigOnly, _ := cmd.Flags().GetBool(flagSigOnly)
|
||||
if sigOnly {
|
||||
json, err = cdc.MarshalJSON(newTx.Signatures[0])
|
||||
} else {
|
||||
@ -141,21 +141,19 @@ func makeMultiSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetString(flags.FlagOutputDocument) == "" {
|
||||
outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
||||
if outputDoc == "" {
|
||||
fmt.Printf("%s\n", json)
|
||||
return
|
||||
}
|
||||
|
||||
fp, err := os.OpenFile(
|
||||
viper.GetString(flags.FlagOutputDocument), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644,
|
||||
)
|
||||
fp, err := os.OpenFile(outputDoc, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
fmt.Fprintf(fp, "%s\n", json)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
@ -66,7 +65,7 @@ func makeSignBatchCmd(cdc *codec.Codec) func(cmd *cobra.Command, args []string)
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
clientCtx := client.NewContextWithInput(inBuf).WithCodec(cdc)
|
||||
txBldr := types.NewTxBuilderFromCLI(inBuf)
|
||||
generateSignatureOnly := viper.GetBool(flagSigOnly)
|
||||
generateSignatureOnly, _ := cmd.Flags().GetBool(flagSigOnly)
|
||||
|
||||
var (
|
||||
err error
|
||||
@ -75,8 +74,8 @@ func makeSignBatchCmd(cdc *codec.Codec) func(cmd *cobra.Command, args []string)
|
||||
)
|
||||
|
||||
// validate multisig address if there's any
|
||||
if viper.GetString(flagMultisig) != "" {
|
||||
multisigAddr, err = sdk.AccAddressFromBech32(viper.GetString(flagMultisig))
|
||||
if ms, _ := cmd.Flags().GetString(flagMultisig); ms != "" {
|
||||
multisigAddr, err = sdk.AccAddressFromBech32(ms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -107,7 +106,8 @@ func makeSignBatchCmd(cdc *codec.Codec) func(cmd *cobra.Command, args []string)
|
||||
txBldr = txBldr.WithSequence(sequence)
|
||||
|
||||
if multisigAddr.Empty() {
|
||||
stdTx, err = authclient.SignStdTx(txBldr, clientCtx, viper.GetString(flags.FlagFrom), unsignedStdTx, false, true)
|
||||
homeDir, _ := cmd.Flags().GetString(flags.FlagFrom)
|
||||
stdTx, err = authclient.SignStdTx(txBldr, clientCtx, homeDir, unsignedStdTx, false, true)
|
||||
} else {
|
||||
stdTx, err = authclient.SignStdTxWithSignerAddress(txBldr, clientCtx, multisigAddr, clientCtx.GetFromName(), unsignedStdTx, true)
|
||||
}
|
||||
@ -133,7 +133,7 @@ func makeSignBatchCmd(cdc *codec.Codec) func(cmd *cobra.Command, args []string)
|
||||
}
|
||||
|
||||
func setOutputFile(cmd *cobra.Command) (func(), error) {
|
||||
outputDoc := viper.GetString(flags.FlagOutputDocument)
|
||||
outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
||||
if outputDoc == "" {
|
||||
cmd.SetOut(cmd.OutOrStdout())
|
||||
return func() {}, nil
|
||||
@ -193,7 +193,7 @@ be generated via the 'multisign' command.
|
||||
func preSignCmd(cmd *cobra.Command, _ []string) {
|
||||
// Conditionally mark the account and sequence numbers required as no RPC
|
||||
// query will be done.
|
||||
if viper.GetBool(flags.FlagOffline) {
|
||||
if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
|
||||
cmd.MarkFlagRequired(flags.FlagAccountNumber)
|
||||
cmd.MarkFlagRequired(flags.FlagSequence)
|
||||
}
|
||||
@ -209,8 +209,8 @@ func makeSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []strin
|
||||
|
||||
// if --signature-only is on, then override --append
|
||||
var newTx types.StdTx
|
||||
generateSignatureOnly := viper.GetBool(flagSigOnly)
|
||||
multisigAddrStr := viper.GetString(flagMultisig)
|
||||
generateSignatureOnly, _ := cmd.Flags().GetBool(flagSigOnly)
|
||||
multisigAddrStr, _ := cmd.Flags().GetString(flagMultisig)
|
||||
|
||||
if multisigAddrStr != "" {
|
||||
var multisigAddr sdk.AccAddress
|
||||
@ -224,7 +224,8 @@ func makeSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []strin
|
||||
)
|
||||
generateSignatureOnly = true
|
||||
} else {
|
||||
appendSig := viper.GetBool(flagAppend) && !generateSignatureOnly
|
||||
append, _ := cmd.Flags().GetBool(flagAppend)
|
||||
appendSig := append && !generateSignatureOnly
|
||||
newTx, err = authclient.SignStdTx(txBldr, clientCtx, clientCtx.GetFromName(), stdTx, appendSig, clientCtx.Offline)
|
||||
}
|
||||
|
||||
@ -237,21 +238,19 @@ func makeSignCmd(clientCtx client.Context) func(cmd *cobra.Command, args []strin
|
||||
return err
|
||||
}
|
||||
|
||||
if viper.GetString(flags.FlagOutputDocument) == "" {
|
||||
outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
||||
if outputDoc == "" {
|
||||
fmt.Printf("%s\n", json)
|
||||
return nil
|
||||
}
|
||||
|
||||
fp, err := os.OpenFile(
|
||||
viper.GetString(flags.FlagOutputDocument), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644,
|
||||
)
|
||||
fp, err := os.OpenFile(outputDoc, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer fp.Close()
|
||||
fmt.Fprintf(fp, "%s\n", json)
|
||||
|
||||
fmt.Fprintf(fp, "%s\n", json)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user