Implement and use CLIContext.Print

This commit is contained in:
Aleksandr Bezobchuk 2020-03-25 12:24:13 -04:00
parent e77d5cb677
commit d962526b70
No known key found for this signature in database
GPG Key ID: 7DAC30FBD99879B0
2 changed files with 44 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package context
import (
"encoding/json"
"fmt"
"io"
"os"
@ -240,9 +241,49 @@ func (ctx CLIContext) WithBroadcastMode(mode string) CLIContext {
return ctx
}
// Print outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.Marshaler. An error is returned upon failure.
func (ctx CLIContext) Print(toPrint interface{}) error {
var (
out []byte
err error
)
switch ctx.OutputFormat {
case "text":
out, err = yaml.Marshal(&toPrint)
case "json":
out, err = ctx.Marshaler.MarshalJSON(toPrint)
// To JSON indent, we re-encode the already encoded JSON given there is no
// error. The re-encoded JSON uses the standard library as the initial encoded
// JSON should have the correct output produced by ctx.Marshaler.
if ctx.Indent && err == nil {
var generic interface{}
err = json.Unmarshal(out, &generic)
if err == nil {
out, err = json.MarshalIndent(generic, "", " ")
}
}
}
if err != nil {
return err
}
_, err = fmt.Fprintf(ctx.Output, "%s\n", out)
return err
}
// PrintOutput prints output while respecting output and indent flags
// NOTE: pass in marshalled structs that have been unmarshaled
// because this function will panic on marshaling errors
// because this function will panic on marshaling errors.
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
func (ctx CLIContext) PrintOutput(toPrint interface{}) error {
var (
out []byte
@ -254,7 +295,6 @@ func (ctx CLIContext) PrintOutput(toPrint interface{}) error {
out, err = yaml.Marshal(&toPrint)
case "json":
// TODO: Use ctx.Marshaler.
if ctx.Indent {
out, err = ctx.Codec.MarshalJSONIndent(toPrint, "", " ")
} else {

View File

@ -90,13 +90,7 @@ func GenerateTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
return err
}
out, err := ctx.Marshaler.MarshalJSON(tx)
if err != nil {
return err
}
_, _ = fmt.Fprintf(ctx.Output, "%s\n", out)
return nil
return ctx.Print(tx)
}
// BroadcastTx attempts to generate, sign and broadcast a transaction with the
@ -159,7 +153,7 @@ func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
return err
}
return ctx.PrintOutput(res)
return ctx.Print(res)
}
// BuildUnsignedTx builds a transaction to be signed given a set of messages. The