display

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

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

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

{
  "result": {
    "hash": "0102030400000000000000000000000000000000000000000000000000000000",
    "height": 10,
    "tx": {
      "signature": {
        "sig": "yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=",
        "type": "secp256k1_ep"
      },
      "body": {
        "desc": "This is a test transaction for cli",
        "payload": "AAA5AAAAeGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0CwAAAGNyZWF0ZV91c2VyAQABAB4AAAAAAA8AAAAAAAAAAAR0ZXh0AAAAAAABAAMAAABmb28=",
        "type": "execute",
        "fee": "100",
        "nonce": 10,
        "chain_id": "asdf"
      },
      "serialization": "concat",
      "sender": null
    },
    "tx_result": {
      "code": 0,
      "gas": 10,
      "log": "This is log"
    },
    "raw": "00009e0141cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758000c736563703235366b315f6570e602225468697320697320612074657374207472616e73616374696f6e20666f7220636c69e8010000390000007866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064340b0000006372656174655f75736572010001001e00000000000f000000000000000004746578740000000000010003000000666f6f076578656375746501033130300a00000000000000046173646606636f6e63617400",
    "warning": "HASH MISMATCH: requested 0102030400000000000000000000000000000000000000000000000000000000; received 53096abc68a1f0a09823a4d8dea302b0ea930715627fc80be7607a9fa714fe60"
  },
  "error": ""
}
Example (RespTxQuery_json)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse()}, nil, "json")
Output:

{
  "result": {
    "hash": "0102030400000000000000000000000000000000000000000000000000000000",
    "height": 10,
    "tx": {
      "signature": {
        "sig": "yz/tf2/zblkFTASoMbIV5RQFJ1PuNT5v4x1LTvc2rNYVUSfbVV0wBroU/LTHm7rVbI5juBqYljGbsFOp4lNHWAA=",
        "type": "secp256k1_ep"
      },
      "body": {
        "desc": "This is a test transaction for cli",
        "payload": "AAA5AAAAeGY2MTdhZjFjYTc3NGViYmQ2ZDIzZThmZTEyYzU2ZDQxZDI1YTIyZDgxZTg4ZjY3YzZjNmVlMGQ0CwAAAGNyZWF0ZV91c2VyAQABAB4AAAAAAA8AAAAAAAAAAAR0ZXh0AAAAAAABAAMAAABmb28=",
        "type": "execute",
        "fee": "100",
        "nonce": 10,
        "chain_id": "asdf"
      },
      "serialization": "concat",
      "sender": null
    },
    "tx_result": {
      "code": 0,
      "gas": 10,
      "log": "This is log"
    },
    "warning": "HASH MISMATCH: requested 0102030400000000000000000000000000000000000000000000000000000000; received 53096abc68a1f0a09823a4d8dea302b0ea930715627fc80be7607a9fa714fe60"
  },
  "error": ""
}
Example (RespTxQuery_text)
Print(&RespTxQuery{Msg: getExampleTxQueryResponse(), WithRaw: true}, nil, "text")
// Transaction ID: 31303234
// Status: success
// Height: 10
// Log: This is log
// Raw: 0001f8eaf850b841cb3fed7f6ff36e59054c04a831b215e514052753ee353e6fe31d4b4ef736acd6155127db555d3006ba14fcb4c79bbad56c8e63b81a9896319bb053a9e2534758008c736563703235366b315f6570f88ea25468697320697320612074657374207472616e73616374696f6e20666f7220636c69b85a0001f856b8397866363137616631636137373465626264366432336538666531326335366434316432356132326438316538386636376336633665653064348b6372656174655f75736572cecdccc6847465787480c483666f6f8765786563757465640a846173646686636f6e63617480
// WARNING! HASH MISMATCH:
// 	Requested 31303234
// 	Received  f866b4251d21552de1bc5b819a4b563a540146954e956e8150163574ce5325ac
Output:

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": null,
  "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 BindTableFlags

func BindTableFlags(cmd *cobra.Command)

BindTableFlags binds the flags to the table config

func FormatTable

func FormatTable(cmd *cobra.Command, columns []string, rows [][]string) ([]byte, error)

FormatTable prints the table with the given columns and rows. It is meant to be used in a MarshalText method.

func FormattedError

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

FormattedError returns a WrappedCmdErr with the error and output format from the command, which specifies the output format to use. This seemed like a nice idea, and it may be in the future, however, for now Cobra will always at least partially hijack the output, so we will keep using PrintErr, which swallows the error to prevent Cobra from using its printing conventions.

func Log

func Log(cmd *cobra.Command, msg string)

Log prints the message to stdout if the silence flag is not set.

func Print

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

Print is a helper function to wrap and print message in given format. THIS SHOULD NOT BE USED IN COMMANDS. Use PrintCmd instead.

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. The returned error is nil if the message it was printed successfully. Thus, this function must ONLY be called from within a cobra.Command's RunE function or or returned directly by the RunE function, NOT used to direct application logic since the returned error no longer pertains to the initial error. This also stores the error the command's Context, accessible via the shared.CtxKeyCmdErr key. This allows the main function to determine if a non-zero exit code should be returned.

func ShouldSilence

func ShouldSilence(cmd *cobra.Command) bool

ShouldSilence returns the value of the silence flag

Types

type MessageReader

type MessageReader[T any] struct {
	Result T      `json:"result"`
	Error  string `json:"error"`
}

MessageReader is a utility to help unmarshal a message from a reader.

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 RespResolutionBroadcast

type RespResolutionBroadcast struct {
	TxHash types.Hash `json:"tx_hash"`
	ID     types.UUID `json:"id"`
}

RespResolutionBroadcast is used to represent the result of creating a new resolution with the CLI. This includes the transaction hash and the ID of the resolution as computed from the resolution body and resolution type. This does not mean it is a unique resolution, and it is important to check that the transaction referenced by the returned hash was executed without error.

func (*RespResolutionBroadcast) MarshalJSON

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

func (*RespResolutionBroadcast) MarshalText

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

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 types.Hash

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

Example (Json)
msg := wrapMsg(RespTxHash{1, 2, 3, 4}, nil)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

{
  "result": {
    "tx_hash": "0102030400000000000000000000000000000000000000000000000000000000"
  },
  "error": ""
}
Example (Json_withError)
err := errors.New("an error")
msg := wrapMsg(RespTxHash{1, 2, 3, 4}, err)
prettyPrint(msg, "json", os.Stdout, os.Stderr)
Output:

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

TxHash: 0102030400000000000000000000000000000000000000000000000000000000

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)

func (*RespTxHash) UnmarshalJSON

func (h *RespTxHash) UnmarshalJSON(b []byte) error

type RespTxQuery

type RespTxQuery struct {
	Msg     *types.TxQueryResponse
	WithRaw bool
}

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 {
	Res *types.TxQueryResponse
}

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

func NewTxHashAndExecResponse

func NewTxHashAndExecResponse(resp *types.TxQueryResponse) *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.

func (*TxHashAndExecResponse) UnmarshalJSON

func (h *TxHashAndExecResponse) UnmarshalJSON(b []byte) error

type TxHashResponse

type TxHashResponse struct {
	TxHash types.Hash `json:"tx_hash"`
}

type WrappedCmdErr

type WrappedCmdErr struct {
	Err          error
	OutputFormat OutputFormat
}

func (*WrappedCmdErr) Error

func (wce *WrappedCmdErr) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳