Skip to content

Commit

Permalink
Merge PR cosmos#6609: x/auth: CLI Remove Viper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Jul 6, 2020
1 parent bbe245a commit feb6977
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 86 deletions.
2 changes: 1 addition & 1 deletion x/auth/client/cli/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
20 changes: 9 additions & 11 deletions x/auth/client/cli/broadcast_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
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)

viper.Set(flags.FlagOffline, true)
cmd := GetBroadcastCommand(clientCtx)
cmd.SetOut(ioutil.Discard)
cmd.SetErr(ioutil.Discard)
cmd.SetArgs([]string{fmt.Sprintf("--%s=true", flags.FlagOffline), ""})

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) {
clientCtx := client.Context{}
clientCtx = clientCtx.WithTxGenerator(simappparams.MakeEncodingConfig().TxGenerator)
cmd := GetBroadcastCommand(clientCtx)

viper.Set(flags.FlagOffline, false)

testDir, cleanFunc := tests.NewTestCaseDir(t)
t.Cleanup(cleanFunc)

Expand Down
49 changes: 22 additions & 27 deletions x/auth/client/cli/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,42 @@ import (
"encoding/hex"

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

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
)

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)
}
}
16 changes: 10 additions & 6 deletions x/auth/client/cli/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/base64"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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())
}
21 changes: 6 additions & 15 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, "&") {
Expand Down Expand Up @@ -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, "")
Expand All @@ -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
Expand Down Expand Up @@ -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
}
18 changes: 8 additions & 10 deletions x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down
Loading

0 comments on commit feb6977

Please sign in to comment.