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

cli: refactor flag reading #6884

Merged
merged 17 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ func ValidateCmd(cmd *cobra.Command, args []string) error {
// flags that do not necessarily change with context. These must be checked if
// the caller explicitly changed the values.
func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, error) {
if flagSet.Changed(cli.OutputFlag) {
if clientCtx.OutputFormat == "" || flagSet.Changed(cli.OutputFlag) {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
output, _ := flagSet.GetString(cli.OutputFlag)
clientCtx = clientCtx.WithOutputFormat(output)
}

if flagSet.Changed(flags.FlagHome) {
if clientCtx.HomeDir == "" || flagSet.Changed(flags.FlagHome) {
homeDir, _ := flagSet.GetString(flags.FlagHome)
clientCtx = clientCtx.WithHomeDir(homeDir)
}

if flagSet.Changed(flags.FlagChainID) {
if clientCtx.ChainID == "" || flagSet.Changed(flags.FlagChainID) {
chainID, _ := flagSet.GetString(flags.FlagChainID)
clientCtx = clientCtx.WithChainID(chainID)
}
Expand Down Expand Up @@ -126,11 +126,15 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont
// be considered "persistent" (e.g. KeyBase or Client) and these should be checked
// if the caller explicitly set those.
func ReadQueryCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, error) {
height, _ := flagSet.GetInt64(flags.FlagHeight)
clientCtx = clientCtx.WithHeight(height)
if clientCtx.Height == 0 || flagSet.Changed(flags.FlagHeight) {
height, _ := flagSet.GetInt64(flags.FlagHeight)
clientCtx = clientCtx.WithHeight(height)
}

useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
if !clientCtx.UseLedger || flagSet.Changed(flags.FlagUseLedger) {
useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
}

return ReadPersistentCommandFlags(clientCtx, flagSet)
}
Expand All @@ -148,31 +152,45 @@ func ReadTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
return clientCtx, err
}

genOnly, _ := flagSet.GetBool(flags.FlagGenerateOnly)
clientCtx = clientCtx.WithGenerateOnly(genOnly)

dryRun, _ := flagSet.GetBool(flags.FlagDryRun)
clientCtx = clientCtx.WithSimulation(dryRun)
if !clientCtx.GenerateOnly || flagSet.Changed(flags.FlagGenerateOnly) {
genOnly, _ := flagSet.GetBool(flags.FlagGenerateOnly)
clientCtx = clientCtx.WithGenerateOnly(genOnly)
}

offline, _ := flagSet.GetBool(flags.FlagOffline)
clientCtx = clientCtx.WithOffline(offline)
if !clientCtx.Simulate || flagSet.Changed(flags.FlagDryRun) {
dryRun, _ := flagSet.GetBool(flags.FlagDryRun)
clientCtx = clientCtx.WithSimulation(dryRun)
}

useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
if !clientCtx.Offline || flagSet.Changed(flags.FlagOffline) {
offline, _ := flagSet.GetBool(flags.FlagOffline)
clientCtx = clientCtx.WithOffline(offline)
}

bMode, _ := flagSet.GetString(flags.FlagBroadcastMode)
clientCtx = clientCtx.WithBroadcastMode(bMode)
if !clientCtx.UseLedger || flagSet.Changed(flags.FlagUseLedger) {
useLedger, _ := flagSet.GetBool(flags.FlagUseLedger)
clientCtx = clientCtx.WithUseLedger(useLedger)
}

skipConfirm, _ := flagSet.GetBool(flags.FlagSkipConfirmation)
clientCtx = clientCtx.WithSkipConfirmation(skipConfirm)
if clientCtx.BroadcastMode == "" || flagSet.Changed(flags.FlagBroadcastMode) {
bMode, _ := flagSet.GetString(flags.FlagBroadcastMode)
clientCtx = clientCtx.WithBroadcastMode(bMode)
}

from, _ := flagSet.GetString(flags.FlagFrom)
fromAddr, fromName, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
if err != nil {
return clientCtx, err
if !clientCtx.SkipConfirm || flagSet.Changed(flags.FlagSkipConfirmation) {
skipConfirm, _ := flagSet.GetBool(flags.FlagSkipConfirmation)
clientCtx = clientCtx.WithSkipConfirmation(skipConfirm)
}

clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName)
if clientCtx.From == "" || flagSet.Changed(flags.FlagFrom) {
from, _ := flagSet.GetString(flags.FlagFrom)
fromAddr, fromName, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName)
}

return clientCtx, nil
}
Expand Down
13 changes: 6 additions & 7 deletions x/auth/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {

// Ensure foo has right amount of funds
startTokens := sdk.TokensFromConsensusPower(400)
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), val1.Address)
s.Require().NoError(err)

var coins sdk.Coins
Expand All @@ -286,15 +286,15 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().NoError(s.network.WaitForNextBlock())

// Ensure destiny account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, account.GetAddress())
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), account.GetAddress())
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(sendTokens, coins.AmountOf(s.cfg.BondDenom))

// Ensure origin account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx.WithOutputFormat("json"), val1.Address)
s.Require().NoError(err)

err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
Expand Down Expand Up @@ -634,6 +634,7 @@ func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) {
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

cmd := authcli.GetBroadcastCommand()
_, out := testutil.ApplyMockIO(cmd)

testDir, cleanFunc := testutil.NewTestCaseDir(t)
t.Cleanup(cleanFunc)
Expand All @@ -645,10 +646,8 @@ func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) {
require.NoError(t, err)

cmd.SetArgs([]string{txFileName})
err = cmd.ExecuteContext(ctx)

// We test it tries to broadcast but we set unsupported tx to get the error.
require.EqualError(t, err, "unsupported return type ; supported types: sync, async, block")
require.Error(t, cmd.ExecuteContext(ctx))
require.Contains(t, out.String(), "unsupported return type ; supported types: sync, async, block")
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions x/bank/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() {
"total account balance",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
Expand All @@ -70,6 +72,7 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() {
"total account balance of a specific denom",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=%s", cli.FlagDenom, s.cfg.BondDenom),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
Expand All @@ -79,7 +82,7 @@ func (s *IntegrationTestSuite) TestGetBalancesCmd() {
},
{
"total account balance of a bogus denom",
[]string{val.Address.String(), fmt.Sprintf("--%s=foobar", cli.FlagDenom)},
[]string{val.Address.String(), fmt.Sprintf("--%s=foobar", cli.FlagDenom), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
false,
&sdk.Coin{},
sdk.NewCoin("foobar", sdk.ZeroInt()),
Expand Down Expand Up @@ -125,7 +128,10 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
}{
{
"total supply",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
[]string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coins{},
sdk.NewCoins(
Expand All @@ -138,6 +144,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
[]string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=%s", cli.FlagDenom, s.cfg.BondDenom),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coin{},
Expand All @@ -148,6 +155,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
[]string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=foobar", cli.FlagDenom),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coin{},
Expand Down
23 changes: 14 additions & 9 deletions x/distribution/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
expectedOutput string
}{
{
"default output",
[]string{},
"json output",
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"community_tax":"0.020000000000000000","base_proposer_reward":"0.010000000000000000","bonus_proposer_reward":"0.040000000000000000","withdraw_addr_enabled":true}`,
},
{
Expand Down Expand Up @@ -132,10 +132,11 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorOutstandingRewards() {
"",
},
{
"default output",
"json output",
[]string{
fmt.Sprintf("--%s=3", flags.FlagHeight),
sdk.ValAddress(val.Address).String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"rewards":[{"denom":"stake","amount":"232.260000000000000000"}]}`,
Expand Down Expand Up @@ -202,10 +203,11 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorCommission() {
"",
},
{
"default output",
"json output",
[]string{
fmt.Sprintf("--%s=3", flags.FlagHeight),
sdk.ValAddress(val.Address).String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`{"commission":[{"denom":"stake","amount":"116.130000000000000000"}]}`,
Expand Down Expand Up @@ -290,10 +292,11 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorSlashes() {
"",
},
{
"default output",
"json output",
[]string{
fmt.Sprintf("--%s=3", flags.FlagHeight),
sdk.ValAddress(val.Address).String(), "1", "3",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
"null",
Expand Down Expand Up @@ -369,19 +372,21 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegatorRewards() {
"",
},
{
"default output",
"json output",
[]string{
fmt.Sprintf("--%s=10", flags.FlagHeight),
addr.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
fmt.Sprintf(`{"rewards":[{"validator_address":"%s","reward":[{"denom":"stake","amount":"387.100000000000000000"}]}],"total":[{"denom":"stake","amount":"387.100000000000000000"}]}`, valAddr.String()),
},
{
"default output (specific validator)",
"json output (specific validator)",
[]string{
fmt.Sprintf("--%s=10", flags.FlagHeight),
addr.String(), valAddr.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`[{"denom":"stake","amount":"387.100000000000000000"}]`,
Expand Down Expand Up @@ -454,8 +459,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryCommunityPool() {
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=3", flags.FlagHeight)},
"json output",
[]string{fmt.Sprintf("--%s=3", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`[{"denom":"stake","amount":"4.740000000000000000"}]`,
},
{
Expand Down
12 changes: 6 additions & 6 deletions x/mint/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"mint_denom":"stake","inflation_rate_change":"0.130000000000000000","inflation_max":"1.000000000000000000","inflation_min":"1.000000000000000000","goal_bonded":"0.670000000000000000","blocks_per_year":"6311520"}`,
},
{
Expand Down Expand Up @@ -112,8 +112,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryInflation() {
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`"1.000000000000000000"`,
},
{
Expand Down Expand Up @@ -153,8 +153,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryAnnualProvisions() {
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`"500000000.000000000000000000"`,
},
{
Expand Down
3 changes: 2 additions & 1 deletion x/params/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func (s *IntegrationTestSuite) TestNewQuerySubspaceParamsCmd() {
expectedOutput string
}{
{
"default output",
"json output",
[]string{
"staking", "MaxValidators",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
`{"subspace":"staking","key":"MaxValidators","value":"100"}`,
},
Expand Down
7 changes: 4 additions & 3 deletions x/slashing/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() {
}{
{"invalid address", []string{"foo"}, true, ``},
{
"valid address (default output)",
"valid address (json output)",
[]string{
valConsPubKey,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
Expand Down Expand Up @@ -116,8 +117,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
expectedOutput string
}{
{
"default output",
[]string{},
"json output",
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"signed_blocks_window":"100","min_signed_per_window":"0.500000000000000000","downtime_jail_duration":"600000000000","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"}`,
},
{
Expand Down