Add indent option to context and fix a bug in node status command

This commit is contained in:
HaoyangLiu 2018-10-02 23:29:50 +08:00
parent 5d259b1b39
commit 9830acc5b9
14 changed files with 32 additions and 45 deletions

View File

@ -44,6 +44,7 @@ type CLIContext struct {
GenerateOnly bool GenerateOnly bool
fromAddress types.AccAddress fromAddress types.AccAddress
fromName string fromName string
Indent bool
} }
// NewCLIContext returns a new initialized CLIContext with parameters from the // NewCLIContext returns a new initialized CLIContext with parameters from the
@ -76,15 +77,11 @@ func NewCLIContext() CLIContext {
GenerateOnly: viper.GetBool(client.FlagGenerateOnly), GenerateOnly: viper.GetBool(client.FlagGenerateOnly),
fromAddress: fromAddress, fromAddress: fromAddress,
fromName: fromName, fromName: fromName,
Indent: viper.GetBool(client.FlagIndentResponse),
} }
} }
func createCertifier() tmlite.Certifier { func createCertifier() tmlite.Certifier {
trustNodeDefined := viper.IsSet(client.FlagTrustNode)
if !trustNodeDefined {
return nil
}
trustNode := viper.GetBool(client.FlagTrustNode) trustNode := viper.GetBool(client.FlagTrustNode)
if trustNode { if trustNode {
return nil return nil

View File

@ -54,7 +54,7 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node") c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
c.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain") c.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Int64(FlagHeight, 0, "block height to query, omit to get most recent provable block") c.Flags().Int64(FlagHeight, 0, "block height to query, omit to get most recent provable block")
//viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode)) viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger)) viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID)) viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID))
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode)) viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))
@ -84,7 +84,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
// --gas can accept integers and "simulate" // --gas can accept integers and "simulate"
c.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf( c.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf(
"gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", GasFlagSimulate, DefaultGasLimit)) "gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", GasFlagSimulate, DefaultGasLimit))
//viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode)) viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger)) viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID)) viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID))
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode)) viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))

View File

@ -63,12 +63,10 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
} }
} }
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
return cdc.MarshalJSONIndent(res, "", " ") return cdc.MarshalJSONIndent(res, "", " ")
} else {
return cdc.MarshalJSON(res)
} }
return cdc.MarshalJSON(res)
} }
// get the current blockchain height // get the current blockchain height
@ -133,7 +131,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
utils.PostProcessResponse(w, cdc, output) utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
} }
} }
@ -152,6 +150,6 @@ func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
utils.PostProcessResponse(w, cdc, output) utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
} }
} }

View File

@ -40,14 +40,16 @@ func getNodeStatus(cliCtx context.CLIContext) (*ctypes.ResultStatus, error) {
// CMD // CMD
func printNodeStatus(cmd *cobra.Command, args []string) error { func printNodeStatus(cmd *cobra.Command, args []string) error {
status, err := getNodeStatus(context.NewCLIContext()) // No need to verify proof in getting node status
viper.Set(client.FlagTrustNode, true)
cliCtx := context.NewCLIContext()
status, err := getNodeStatus(cliCtx)
if err != nil { if err != nil {
return err return err
} }
var output []byte var output []byte
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
output, err = cdc.MarshalJSONIndent(status, "", " ") output, err = cdc.MarshalJSONIndent(status, "", " ")
} else { } else {
output, err = cdc.MarshalJSON(status) output, err = cdc.MarshalJSON(status)
@ -73,7 +75,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
} }
nodeInfo := status.NodeInfo nodeInfo := status.NodeInfo
utils.PostProcessResponse(w, cdc, nodeInfo) utils.PostProcessResponse(w, cdc, nodeInfo, cliCtx.Indent)
} }
} }

View File

@ -98,13 +98,11 @@ func getValidators(cliCtx context.CLIContext, height *int64) ([]byte, error) {
} }
} }
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
return cdc.MarshalJSONIndent(outputValidatorsRes, "", " ") return cdc.MarshalJSONIndent(outputValidatorsRes, "", " ")
} else {
return cdc.MarshalJSON(outputValidatorsRes)
} }
return cdc.MarshalJSON(outputValidatorsRes)
} }
// CMD // CMD

View File

@ -55,6 +55,6 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
utils.PostProcessResponse(w, cdc, res) utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
} }
} }

View File

@ -79,12 +79,10 @@ func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) ([]
return nil, err return nil, err
} }
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
return cdc.MarshalJSONIndent(info, "", " ") return cdc.MarshalJSONIndent(info, "", " ")
} else {
return cdc.MarshalJSON(info)
} }
return cdc.MarshalJSON(info)
} }
// ValidateTxResult performs transaction verification // ValidateTxResult performs transaction verification
@ -148,6 +146,6 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
utils.PostProcessResponse(w, cdc, output) utils.PostProcessResponse(w, cdc, output, cliCtx.Indent)
} }
} }

View File

@ -53,8 +53,7 @@ $ gaiacli tendermint txs --tag test1,test2 --any
} }
var output []byte var output []byte
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
output, err = cdc.MarshalJSONIndent(txs, "", " ") output, err = cdc.MarshalJSONIndent(txs, "", " ")
} else { } else {
output, err = cdc.MarshalJSON(txs) output, err = cdc.MarshalJSON(txs)
@ -182,6 +181,6 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
return return
} }
utils.PostProcessResponse(w, cdc, txs) utils.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
} }
} }

View File

@ -15,7 +15,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/viper"
) )
const ( const (
@ -242,14 +241,14 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
return return
} }
PostProcessResponse(w, cdc, res) PostProcessResponse(w, cdc, res, cliCtx.Indent)
} }
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}) { // PostProcessResponse performs post process for rest response
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}, indent bool) {
var output []byte var output []byte
switch response.(type) { switch response.(type) {
default: default:
indent := viper.GetBool(client.FlagIndentResponse)
var err error var err error
if indent { if indent {
output, err = cdc.MarshalJSONIndent(response, "", " ") output, err = cdc.MarshalJSONIndent(response, "", " ")

View File

@ -9,8 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
) )
// GetAccountCmdDefault invokes the GetAccountCmd for the auth.BaseAccount type. // GetAccountCmdDefault invokes the GetAccountCmd for the auth.BaseAccount type.
@ -61,8 +59,7 @@ func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecod
} }
var output []byte var output []byte
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ") output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else { } else {
output, err = cdc.MarshalJSON(acc) output, err = cdc.MarshalJSON(acc)

View File

@ -58,8 +58,7 @@ func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra.
return err return err
} }
var json []byte var json []byte
indent := viper.GetBool(client.FlagIndentResponse) if cliCtx.Indent {
if indent {
json, err = cdc.MarshalJSONIndent(newTx, "", " ") json, err = cdc.MarshalJSONIndent(newTx, "", " ")
} else { } else {
json, err = cdc.MarshalJSON(newTx) json, err = cdc.MarshalJSON(newTx)

View File

@ -65,7 +65,7 @@ func QueryAccountRequestHandlerFn(
return return
} }
utils.PostProcessResponse(w, cdc, account) utils.PostProcessResponse(w, cdc, account, cliCtx.Indent)
} }
} }
@ -104,6 +104,6 @@ func QueryBalancesRequestHandlerFn(
return return
} }
utils.PostProcessResponse(w, cdc, account.GetCoins()) utils.PostProcessResponse(w, cdc, account.GetCoins(), cliCtx.Indent)
} }
} }

View File

@ -52,6 +52,6 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
return return
} }
utils.PostProcessResponse(w, cdc, signedTx) utils.PostProcessResponse(w, cdc, signedTx, cliCtx.Indent)
} }
} }

View File

@ -33,7 +33,7 @@ func BroadcastTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) ht
return return
} }
utils.PostProcessResponse(w, cdc, res) utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
} }
} }