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
fromAddress types.AccAddress
fromName string
Indent bool
}
// NewCLIContext returns a new initialized CLIContext with parameters from the
@ -76,15 +77,11 @@ func NewCLIContext() CLIContext {
GenerateOnly: viper.GetBool(client.FlagGenerateOnly),
fromAddress: fromAddress,
fromName: fromName,
Indent: viper.GetBool(client.FlagIndentResponse),
}
}
func createCertifier() tmlite.Certifier {
trustNodeDefined := viper.IsSet(client.FlagTrustNode)
if !trustNodeDefined {
return nil
}
trustNode := viper.GetBool(client.FlagTrustNode)
if trustNode {
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(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")
//viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID))
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))
@ -84,7 +84,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
// --gas can accept integers and "simulate"
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))
//viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID))
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 indent {
if cliCtx.Indent {
return cdc.MarshalJSONIndent(res, "", " ")
} else {
return cdc.MarshalJSON(res)
}
return cdc.MarshalJSON(res)
}
// get the current blockchain height
@ -133,7 +131,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
w.Write([]byte(err.Error()))
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()))
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
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 {
return err
}
var output []byte
indent := viper.GetBool(client.FlagIndentResponse)
if indent {
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(status, "", " ")
} else {
output, err = cdc.MarshalJSON(status)
@ -73,7 +75,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}
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 indent {
if cliCtx.Indent {
return cdc.MarshalJSONIndent(outputValidatorsRes, "", " ")
} else {
return cdc.MarshalJSON(outputValidatorsRes)
}
return cdc.MarshalJSON(outputValidatorsRes)
}
// CMD

View File

@ -55,6 +55,6 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
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
}
indent := viper.GetBool(client.FlagIndentResponse)
if indent {
if cliCtx.Indent {
return cdc.MarshalJSONIndent(info, "", " ")
} else {
return cdc.MarshalJSON(info)
}
return cdc.MarshalJSON(info)
}
// ValidateTxResult performs transaction verification
@ -148,6 +146,6 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H
w.Write([]byte(err.Error()))
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
indent := viper.GetBool(client.FlagIndentResponse)
if indent {
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(txs, "", " ")
} else {
output, err = cdc.MarshalJSON(txs)
@ -182,6 +181,6 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
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"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/spf13/viper"
)
const (
@ -242,14 +241,14 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
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
switch response.(type) {
default:
indent := viper.GetBool(client.FlagIndentResponse)
var err error
if indent {
output, err = cdc.MarshalJSONIndent(response, "", " ")

View File

@ -9,8 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"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.
@ -61,8 +59,7 @@ func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecod
}
var output []byte
indent := viper.GetBool(client.FlagIndentResponse)
if indent {
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else {
output, err = cdc.MarshalJSON(acc)

View File

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

View File

@ -65,7 +65,7 @@ func QueryAccountRequestHandlerFn(
return
}
utils.PostProcessResponse(w, cdc, account)
utils.PostProcessResponse(w, cdc, account, cliCtx.Indent)
}
}
@ -104,6 +104,6 @@ func QueryBalancesRequestHandlerFn(
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
}
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
}
utils.PostProcessResponse(w, cdc, res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}