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

WIP: CLI Response Formatting #3301

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions client/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
)

// PrintOutput prints output while respecting output and indent flags
// NOTE: pass in marshalled structs that have been unmarshaled
// because this function doesn't return errors for marshaling
func PrintOutput(cdc *codec.Codec, toPrint fmt.Stringer) {
switch viper.Get(cli.OutputFlag) {
case "text":
fmt.Println(toPrint.String())
case "json":
if viper.GetBool(FlagIndentResponse) {
out, err := codec.MarshalJSONIndent(cdc, toPrint)
if err != nil {
panic(err)
}
fmt.Println(string(out))
} else {
fmt.Println(string(cdc.MustMarshalJSON(toPrint)))
}
}
}
1 change: 1 addition & 0 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 30
cmd.Flags().String(flagTags, "", "tag:value list of tags that must match")
cmd.Flags().Int32(flagPage, defaultPage, "Query a specific page of paginated results")
cmd.Flags().Int32(flagLimit, defaultLimit, "Query number of transactions results per page returned")
cmd.MarkFlagRequired(flagTags)
return cmd
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
fooAcc := f.QueryAccount(fooAddr)
require.Equal(t, int64(50), fooAcc.GetCoins().AmountOf(stakingTypes.DefaultBondDenom).Int64())

// Ensure there are no proposals to start
proposalsQuery := f.QueryGovProposals()
require.Equal(t, "No matching proposals found", proposalsQuery)
require.Empty(t, proposalsQuery)

// Test submit generate only for submit proposal
success, stdout, stderr := f.TxGovSubmitProposal(
Expand Down Expand Up @@ -369,7 +370,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {

// Ensure query proposals returns properly
proposalsQuery = f.QueryGovProposals()
require.Equal(t, " 1 - Test", proposalsQuery)
require.Equal(t, uint64(1), proposalsQuery[0].GetProposalID())

// Query the deposits on the proposal
deposit := f.QueryGovDeposit(1, fooAddr)
Expand Down Expand Up @@ -440,19 +441,19 @@ func TestGaiaCLISubmitProposal(t *testing.T) {

// Ensure no proposals in deposit period
proposalsQuery = f.QueryGovProposals("--status=DepositPeriod")
require.Equal(t, "No matching proposals found", proposalsQuery)
require.Empty(t, proposalsQuery)

// Ensure the proposal returns as in the voting period
proposalsQuery = f.QueryGovProposals("--status=VotingPeriod")
require.Equal(t, " 1 - Test", proposalsQuery)
require.Equal(t, uint64(1), proposalsQuery[0].GetProposalID())

// submit a second test proposal
f.TxGovSubmitProposal(keyFoo, "Text", "Apples", "test", sdk.NewInt64Coin(denom, 5))
tests.WaitForNextNBlocksTM(1, f.Port)

// Test limit on proposals query
proposalsQuery = f.QueryGovProposals("--limit=1")
require.Equal(t, " 2 - Apples", proposalsQuery)
require.Equal(t, uint64(2), proposalsQuery[0].GetProposalID())

f.Cleanup()
}
Expand Down
13 changes: 10 additions & 3 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (f *Fixtures) QueryStakingPool(flags ...string) staking.Pool {

// QueryStakingParameters is gaiacli query staking parameters
func (f *Fixtures) QueryStakingParameters(flags ...string) staking.Params {
cmd := fmt.Sprintf("gaiacli query staking parameters %v", f.Flags())
cmd := fmt.Sprintf("gaiacli query staking params %v", f.Flags())
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var params staking.Params
cdc := app.MakeCodec()
Expand Down Expand Up @@ -406,11 +406,18 @@ func (f *Fixtures) QueryGovParamTallying() gov.TallyParams {
}

// QueryGovProposals is gaiacli query gov proposals
func (f *Fixtures) QueryGovProposals(flags ...string) string {
func (f *Fixtures) QueryGovProposals(flags ...string) gov.Proposals {
cmd := fmt.Sprintf("gaiacli query gov proposals %v", f.Flags())
stdout, stderr := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
if stderr == "ERROR: No matching proposals found" {
return gov.Proposals{}
}
require.Empty(f.T, stderr)
return stdout
var out gov.Proposals
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(stdout), &out)
require.NoError(f.T, err)
return out
}

// QueryGovProposal is gaiacli query gov proposal
Expand Down
43 changes: 43 additions & 0 deletions x/auth/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"errors"
"fmt"
"time"

"github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -35,6 +36,8 @@ type Account interface {
// Calculates the amount of coins that can be sent to other accounts given
// the current time.
SpendableCoins(blockTime time.Time) sdk.Coins

String() string
}

// VestingAccount defines an account type that vests coins via a vesting schedule.
Expand Down Expand Up @@ -71,6 +74,15 @@ type BaseAccount struct {
Sequence uint64 `json:"sequence"`
}

func (acc BaseAccount) String() string {
return fmt.Sprintf(`Account %s:
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d`, acc.Address, acc.Coins,
acc.PubKey.Address(), acc.AccountNumber, acc.Sequence)
}

// Prototype function for BaseAccount
func ProtoBaseAccount() Account {
return &BaseAccount{}
Expand Down Expand Up @@ -161,6 +173,21 @@ type BaseVestingAccount struct {
EndTime time.Time // when the coins become unlocked
}

func (bva BaseVestingAccount) String() string {
return fmt.Sprintf(`Vesting Account %s:
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d
OriginalVesting: %s
DelegatedFree: %s
DelegatedVesting: %s
EndTime: %s `, bva.Address, bva.Coins,
bva.PubKey.Address(), bva.AccountNumber, bva.Sequence,
bva.OriginalVesting, bva.DelegatedFree,
bva.DelegatedVesting, bva.EndTime)
}

// spendableCoins returns all the spendable coins for a vesting account given a
// set of vesting coins.
//
Expand Down Expand Up @@ -323,6 +350,22 @@ type ContinuousVestingAccount struct {
StartTime time.Time // when the coins start to vest
}

func (cva ContinuousVestingAccount) String() string {
return fmt.Sprintf(`Vesting Account %s:
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d
OriginalVesting: %s
DelegatedFree: %s
DelegatedVesting: %s
StartTime: %s
EndTime: %s `, cva.Address, cva.Coins,
cva.PubKey.Address(), cva.AccountNumber, cva.Sequence,
cva.OriginalVesting, cva.DelegatedFree,
cva.DelegatedVesting, cva.StartTime, cva.EndTime)
}

func NewContinuousVestingAccount(
addr sdk.AccAddress, origCoins sdk.Coins, StartTime, EndTime time.Time,
) *ContinuousVestingAccount {
Expand Down
25 changes: 5 additions & 20 deletions x/auth/client/cli/account.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -20,38 +18,25 @@ func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command {
Short: "Query account balance",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// find the key to look up the account
addr := args[0]

key, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return err
}

cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)

if err = cliCtx.EnsureAccountExistsFromAddr(key); err != nil {
key, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

acc, err := cliCtx.GetAccount(key)
if err != nil {
if err = cliCtx.EnsureAccountExistsFromAddr(key); err != nil {
return err
}

var output []byte
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else {
output, err = cdc.MarshalJSON(acc)
}
acc, err := cliCtx.GetAccount(key)
if err != nil {
return err
}

fmt.Println(string(output))
client.PrintOutput(cdc, acc)
return nil
},
}
Expand Down
Loading