Skip to content

Commit

Permalink
feat(cli): status cmd cli support output text (#17184)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Aug 1, 2023
1 parent 095a641 commit 31f7247
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (server) [#17177](https://github.com/cosmos/cosmos-sdk/pull/17177) Remove `iavl-lazy-loading` configuration.
* (rosetta) [#16276](https://github.com/cosmos/cosmos-sdk/issues/16276) Rosetta migration to standalone repo.
* (cli) [#17184](https://github.com/cosmos/cosmos-sdk/pull/17184) All json keys returned by the `status` command are now snake case instead of pascal case.

### State Machine Breaking

Expand Down
50 changes: 10 additions & 40 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,14 @@ package rpc
import (
"context"

"github.com/cometbft/cometbft/p2p"
cmtjson "github.com/cometbft/cometbft/libs/json"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

// ValidatorInfo is info about the node's validator, same as CometBFT,
// except that we use our own PubKey.
type validatorInfo struct {
Address []byte
PubKey cryptotypes.PubKey
VotingPower int64
}

// ResultStatus is node's info, same as CometBFT, except that we use our own
// PubKey.
type resultStatus struct {
NodeInfo p2p.DefaultNodeInfo
SyncInfo coretypes.SyncInfo
ValidatorInfo validatorInfo
}

// StatusCommand returns the command to return the status of the network.
func StatusCommand() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -45,35 +27,23 @@ func StatusCommand() *cobra.Command {
return err
}

var pk cryptotypes.PubKey
// `status` has TM pubkeys, we need to convert them to our pubkeys.
if status.ValidatorInfo.PubKey != nil {
pk, err = cryptocodec.FromCmtPubKeyInterface(status.ValidatorInfo.PubKey)
if err != nil {
return err
}
}
statusWithPk := resultStatus{
NodeInfo: status.NodeInfo,
SyncInfo: status.SyncInfo,
ValidatorInfo: validatorInfo{
Address: status.ValidatorInfo.Address,
PubKey: pk,
VotingPower: status.ValidatorInfo.VotingPower,
},
}

output, err := clientCtx.LegacyAmino.MarshalJSON(statusWithPk)
output, err := cmtjson.Marshal(status)
if err != nil {
return err
}

cmd.Println(string(output))
return nil
// In order to maintain backwards compatibility, the default json format output
outputFormat, _ := cmd.Flags().GetString(flags.FlagOutput)
if outputFormat == flags.OutputFormatJSON {
clientCtx = clientCtx.WithOutputFormat(flags.OutputFormatJSON)
}

return clientCtx.PrintRaw(output)
},
}

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().StringP(flags.FlagOutput, "o", "json", "Output format (text|json)")

return cmd
}
Expand Down

0 comments on commit 31f7247

Please sign in to comment.