display

package
v0.7.0-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package display provides interfaces and functions to format the command line output and print.

Example (RespTxQuery_json)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse()}, nil, "json")
Output:

{
  "result": {
    "hash": "31303234",
    "height": 10,
    "tx": {
      "Signature": {
        "signature_bytes": "yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=",
        "signature_type": "secp256k1_ep"
      },
      "Body": {
        "Description": "This is a test transaction for cli",
        "Payload": "AAH4ULg5eGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0i2NyZWF0ZV91c2VyyMeDZm9vgjMy",
        "PayloadType": "execute_action",
        "Fee": 100,
        "Nonce": 10,
        "ChainID": "asdf"
      },
      "Serialization": "concat",
      "Sender": null
    },
    "tx_result": {
      "code": 0,
      "log": "This is log",
      "gas_used": 10,
      "gas_wanted": 10
    }
  },
  "error": ""
}
Example (RespTxQuery_text)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse()}, nil, "text")
Output:

Transaction ID: 31303234
Status: success
Height: 10
Log: This is log
Example (WrappedMsg_json)
msg := wrapMsg(&demoFormat{data: []byte("demo")}, nil)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": {
    "name_to_whatever": "demo_whatever"
  },
  "error": ""
}
Example (WrappedMsg_json_withError)
err := errors.New("an error")
msg := wrapMsg(&demoFormat{data: []byte("demo")}, err)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": "",
  "error": "an error"
}
Example (WrappedMsg_text)
msg := wrapMsg(&demoFormat{data: []byte("demo")}, nil)
prettyPrint(msg, "text", os.Stdout, os.Stderr)
Output:

Whatever format: demo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindOutputFormatFlag

func BindOutputFormatFlag(cmd *cobra.Command)

BindOutputFormatFlag binds the output format flag to the command. This should be added on the root command.

func BindSilenceFlag

func BindSilenceFlag(cmd *cobra.Command)

BindSilenceFlag binds the silence flag to the passed command. If bound, the command will silence logs. If true, display commands will not print to stdout or stderr. The flag will be bound to all subcommands of the given command.

func Print

func Print(msg MsgFormatter, err error, format OutputFormat) error

Print is a helper function to wrap and print message in given format.

func PrintCmd

func PrintCmd(cmd *cobra.Command, msg MsgFormatter) error

PrintCmd prints output based on the commands output format flag. If not format flag is provided, it will default to text in stdout.

func PrintErr

func PrintErr(cmd *cobra.Command, err error) error

PrintErr prints the error according to the commands output format flag.

func ShouldSilence

func ShouldSilence(cmd *cobra.Command) bool

ShouldSilence returns the value of the silence flag

Types

type MsgFormatter

type MsgFormatter interface {
	json.Marshaler
	encoding.TextMarshaler
}

MsgFormatter is an interface that wraps the MarshalText and MarshalJSON It defines the requirements for something to be printed.

type OutputFormat

type OutputFormat string

OutputFormat is the format for command output It implements the pflag.Value interface

type RespString

type RespString string

RespString is used to represent a string in cli It implements the MsgFormatter interface

func (RespString) MarshalJSON

func (s RespString) MarshalJSON() ([]byte, error)

func (RespString) MarshalText

func (s RespString) MarshalText() ([]byte, error)

type RespTxHash

type RespTxHash []byte

RespTxHash is used to represent a transaction hash in cli NOTE: it's different from transactions.TxHash, this is for display purpose. It implements the MsgFormatter interface

Example (Json)
msg := wrapMsg(RespTxHash("1024"), nil)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": {
    "tx_hash": "31303234"
  },
  "error": ""
}
Example (Json_withError)
err := errors.New("an error")
msg := wrapMsg(RespTxHash("1024"), err)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": "",
  "error": "an error"
}
Example (Text)
msg := wrapMsg(RespTxHash("1024"), nil)
prettyPrint(msg, "text", os.Stdout, os.Stderr)
Output:

TxHash: 31303234

func (RespTxHash) Hex

func (h RespTxHash) Hex() string

func (RespTxHash) MarshalJSON

func (h RespTxHash) MarshalJSON() ([]byte, error)

func (RespTxHash) MarshalText

func (h RespTxHash) MarshalText() ([]byte, error)

type RespTxQuery

type RespTxQuery struct {
	Msg *transactions.TcTxQueryResponse
}

RespTxQuery is used to represent a transaction response in cli

func (*RespTxQuery) MarshalJSON

func (r *RespTxQuery) MarshalJSON() ([]byte, error)

func (*RespTxQuery) MarshalText

func (r *RespTxQuery) MarshalText() ([]byte, error)

type TxHashAndExecResponse

type TxHashAndExecResponse struct {
	Hash      RespTxHash   // embedding breaks MarshalJSON of composing types
	QueryResp *RespTxQuery `json:"exec_result"`
}

TxHashAndExecResponse is meant to combine the "tx_hash" marshalling of RespTxHash with a RespTxQuery in an "exec_result" field.

func NewTxHashAndExecResponse

func NewTxHashAndExecResponse(resp *transactions.TcTxQueryResponse) *TxHashAndExecResponse

NewTxHashAndExecResponse makes a TxHashAndExecResponse from a TcTxQueryResponse.

func (*TxHashAndExecResponse) MarshalJSON

func (h *TxHashAndExecResponse) MarshalJSON() ([]byte, error)

func (TxHashAndExecResponse) MarshalText

func (h TxHashAndExecResponse) MarshalText() ([]byte, error)

MarshalText deduplicates the tx hash for a compact readable output, unlike the JSON marshalling that is meant to be a composition of both RespTxHash and RespTxQuery.

type TxHashResponse

type TxHashResponse struct {
	TxHash string `json:"tx_hash"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL