diff --git a/client/flags.go b/client/flags.go index ff9354937..394cf0c7a 100644 --- a/client/flags.go +++ b/client/flags.go @@ -35,6 +35,7 @@ const ( FlagPrintResponse = "print-response" FlagDryRun = "dry-run" FlagGenerateOnly = "generate-only" + FlagIndentResponse= "indent" ) // LineBreak can be included in a command list to provide a blank line @@ -47,12 +48,13 @@ var ( // GetCommands adds common flags to query commands func GetCommands(cmds ...*cobra.Command) []*cobra.Command { for _, c := range cmds { + c.Flags().Bool(FlagIndentResponse, false, "Add indent to JSON response") c.Flags().Bool(FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)") c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") c.Flags().String(FlagChainID, "", "Chain ID of tendermint node") c.Flags().String(FlagNode, "tcp://localhost:26657", ": 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)) @@ -63,6 +65,7 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command { // PostCommands adds common flags for commands to post tx func PostCommands(cmds ...*cobra.Command) []*cobra.Command { for _, c := range cmds { + c.Flags().Bool(FlagIndentResponse, false, "Add indent to JSON response") c.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") c.Flags().Int64(FlagAccountNumber, 0, "AccountNumber number to sign the tx") c.Flags().Int64(FlagSequence, 0, "Sequence number to sign the tx") @@ -81,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)) diff --git a/client/lcd/root.go b/client/lcd/root.go index 663b7c1ad..0de6f839f 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -123,6 +123,7 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command { cmd.Flags().String(client.FlagNode, "tcp://localhost:26657", "Address of the node to connect to") cmd.Flags().Int(flagMaxOpenConnections, 1000, "The number of maximum open connections") cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)") + cmd.Flags().Bool(client.FlagIndentResponse, false, "Add indent to JSON response") viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) viper.BindPFlag(client.FlagChainID, cmd.Flags().Lookup(client.FlagChainID)) viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode)) diff --git a/client/rpc/block.go b/client/rpc/block.go index e5873152e..a4bb5ee16 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -12,6 +12,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" tmliteProxy "github.com/tendermint/tendermint/lite/proxy" + "github.com/cosmos/cosmos-sdk/client/utils" ) //BlockCommand returns the verified block data for a given heights @@ -62,13 +63,12 @@ func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { } } - // TODO move maarshalling into cmd/rest functions - // output, err := tmcodec.MarshalJSON(res) - output, err := cdc.MarshalJSONIndent(res, "", " ") - if err != nil { - return nil, err + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + return cdc.MarshalJSONIndent(res, "", " ") + } else { + return cdc.MarshalJSON(res) } - return output, nil } // get the current blockchain height @@ -133,8 +133,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, output) } } @@ -153,7 +152,6 @@ func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { w.Write([]byte(err.Error())) return } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, output) } } diff --git a/client/rpc/status.go b/client/rpc/status.go index 596870bf3..11448edc5 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/spf13/viper" + "github.com/cosmos/cosmos-sdk/client/utils" ) func statusCommand() *cobra.Command { @@ -22,6 +23,7 @@ func statusCommand() *cobra.Command { cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to") viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode)) + cmd.Flags().Bool(client.FlagIndentResponse, false, "Add indent to JSON response") return cmd } @@ -43,8 +45,13 @@ func printNodeStatus(cmd *cobra.Command, args []string) error { return err } - output, err := cdc.MarshalJSON(status) - // output, err := cdc.MarshalJSONIndent(res, " ", "") + var output []byte + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + output, err = cdc.MarshalJSONIndent(status, "", " ") + } else { + output, err = cdc.MarshalJSON(status) + } if err != nil { return err } @@ -66,14 +73,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } nodeInfo := status.NodeInfo - output, err := cdc.MarshalJSONIndent(nodeInfo, "", " ") - if err != nil { - w.WriteHeader(500) - w.Write([]byte(err.Error())) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, nodeInfo) } } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index f0c04e75e..021f2cea5 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -98,12 +98,13 @@ func getValidators(cliCtx context.CLIContext, height *int64) ([]byte, error) { } } - output, err := cdc.MarshalJSONIndent(outputValidatorsRes, "", " ") - if err != nil { - return nil, err - } + indent := viper.GetBool(client.FlagIndentResponse) - return output, nil + if indent { + return cdc.MarshalJSONIndent(outputValidatorsRes, "", " ") + } else { + return cdc.MarshalJSON(outputValidatorsRes) + } } // CMD diff --git a/client/tx/broadcast.go b/client/tx/broadcast.go index 42fe64dee..4876cc887 100644 --- a/client/tx/broadcast.go +++ b/client/tx/broadcast.go @@ -4,14 +4,14 @@ import ( "net/http" "github.com/cosmos/cosmos-sdk/client/context" - "io/ioutil" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/client/utils" + "github.com/cosmos/cosmos-sdk/codec" + "io/ioutil" ) const ( // Returns with the response from CheckTx. - flagSync = "sync" + flagSync = "sync" // Returns right away, with no response flagAsync = "async" // Only returns error if mempool.BroadcastTx errs (ie. problem with the app) or if we timeout waiting for tx to commit. @@ -21,7 +21,7 @@ const ( // BroadcastBody Tx Broadcast Body type BroadcastBody struct { TxBytes []byte `json:"tx"` - Return string `json:"return"` + Return string `json:"return"` } // BroadcastTxRequest REST Handler @@ -55,12 +55,6 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - output, err := cdc.MarshalJSONIndent(res, "", " ") - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, res) } } diff --git a/client/tx/query.go b/client/tx/query.go index 9999d663a..30b6145aa 100644 --- a/client/tx/query.go +++ b/client/tx/query.go @@ -17,6 +17,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/spf13/viper" + "github.com/cosmos/cosmos-sdk/client/utils" ) // QueryTxCmd implements the default command for a tx query. @@ -78,7 +79,12 @@ func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) ([] return nil, err } - return codec.MarshalJSONIndent(cdc, info) + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + return cdc.MarshalJSONIndent(info, "", " ") + } else { + return cdc.MarshalJSON(info) + } } // ValidateTxResult performs transaction verification @@ -142,7 +148,6 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H w.Write([]byte(err.Error())) return } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, output) } } diff --git a/client/tx/search.go b/client/tx/search.go index 11e84f419..17339cade 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -16,6 +16,7 @@ import ( "github.com/spf13/viper" ctypes "github.com/tendermint/tendermint/rpc/core/types" + "github.com/cosmos/cosmos-sdk/client/utils" ) const ( @@ -51,7 +52,14 @@ $ gaiacli tendermint txs --tag test1,test2 --any return err } - output, err := cdc.MarshalJSON(txs) + var output []byte + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + output, err = cdc.MarshalJSONIndent(txs, "", " ") + } else { + output, err = cdc.MarshalJSON(txs) + } + if err != nil { return err } @@ -174,13 +182,6 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http. return } - output, err := cdc.MarshalJSONIndent(txs, "", " ") - if err != nil { - w.WriteHeader(500) - w.Write([]byte(err.Error())) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, txs) } } diff --git a/client/utils/rest.go b/client/utils/rest.go index 52c253781..cc1557941 100644 --- a/client/utils/rest.go +++ b/client/utils/rest.go @@ -1,5 +1,6 @@ package utils +import "C" import ( "fmt" "io/ioutil" @@ -8,12 +9,13 @@ import ( "strconv" "strings" - client "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - auth "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" + "github.com/spf13/viper" ) const ( @@ -240,10 +242,26 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c return } - output, err := codec.MarshalJSONIndent(cdc, res) - if err != nil { - WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return + PostProcessResponse(w, cdc, res) +} + +func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}) { + var output []byte + switch response.(type) { + default: + indent := viper.GetBool(client.FlagIndentResponse) + var err error + if indent { + output, err = cdc.MarshalJSONIndent(response, "", " ") + } else { + output, err = cdc.MarshalJSON(response) + } + if err != nil { + WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + case []byte: + output = response.([]byte) } w.Header().Set("Content-Type", "application/json") w.Write(output) diff --git a/x/auth/client/cli/account.go b/x/auth/client/cli/account.go index 13c3230cf..6c5d9d009 100644 --- a/x/auth/client/cli/account.go +++ b/x/auth/client/cli/account.go @@ -9,6 +9,8 @@ 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. @@ -58,7 +60,13 @@ func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecod return err } - output, err := codec.MarshalJSONIndent(cdc, acc) + var output []byte + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + output, err = cdc.MarshalJSONIndent(acc, "", " ") + } else { + output, err = cdc.MarshalJSON(acc) + } if err != nil { return err } diff --git a/x/auth/client/cli/sign.go b/x/auth/client/cli/sign.go index 31648e3de..0c586233b 100644 --- a/x/auth/client/cli/sign.go +++ b/x/auth/client/cli/sign.go @@ -57,7 +57,13 @@ func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra. if err != nil { return err } - json, err := cdc.MarshalJSON(newTx) + var json []byte + indent := viper.GetBool(client.FlagIndentResponse) + if indent { + json, err = cdc.MarshalJSONIndent(newTx, "", " ") + } else { + json, err = cdc.MarshalJSON(newTx) + } if err != nil { return err } diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index c03d47cba..8d5763915 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -65,14 +65,7 @@ func QueryAccountRequestHandlerFn( return } - // print out whole account - output, err := cdc.MarshalJSON(account) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error())) - return - } - - w.Write(output) + utils.PostProcessResponse(w, cdc, account) } } @@ -111,13 +104,6 @@ func QueryBalancesRequestHandlerFn( return } - // print out whole account - output, err := cdc.MarshalJSONIndent(account.GetCoins(), "", " ") - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error())) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, account.GetCoins()) } } diff --git a/x/auth/client/rest/sign.go b/x/auth/client/rest/sign.go index 13da2fd4d..dd93d8979 100644 --- a/x/auth/client/rest/sign.go +++ b/x/auth/client/rest/sign.go @@ -52,12 +52,6 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha return } - output, err := codec.MarshalJSONIndent(cdc, signedTx) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, signedTx) } } diff --git a/x/bank/client/rest/broadcast.go b/x/bank/client/rest/broadcast.go index 5d9ffa71e..e590f19ec 100644 --- a/x/bank/client/rest/broadcast.go +++ b/x/bank/client/rest/broadcast.go @@ -33,13 +33,7 @@ func BroadcastTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) ht return } - output, err := codec.MarshalJSONIndent(cdc, res) - if err != nil { - utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(output) + utils.PostProcessResponse(w, cdc, res) } }