Help us shape the future of Go! Take the 2026 Go Developer Survey

cmdutil

package
v0.1.120 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package cmdutil holds the plumbing shared by every `polycli heimdall` command package: config-resolving client constructors, render.Options assembly, JSON decoding, hex normalization, the gRPC-gateway error envelope, and generic cobra command builders for the common "GET endpoint → render" and "CometBFT RPC → render" shapes.

Index

Constants

View Source
const GRPCCodeUnavailable = 13

GRPCCodeUnavailable is the L1-unreachable code surfaced by the endpoints that fan out to L1 on the server side (clerk/topup sequence + is-old, stake is-old-tx, topup account-proof) when the Heimdall node lacks `eth_rpc_url`.

Variables

This section is empty.

Functions

func CallEmpty

func CallEmpty(ctx context.Context, rpc *client.RPCClient, method string) (json.RawMessage, error)

CallEmpty issues an RPC call with an explicit empty params object. CometBFT's reflect-based RPC layer rejects nil params on some methods with "reflect: Call with too few input arguments"; passing map[string]any{} avoids that trap while still producing a valid JSON-RPC envelope.

func DecodeGeneric

func DecodeGeneric(raw json.RawMessage) (any, error)

DecodeGeneric unmarshals raw into any (map/slice/string). Used when we want to emit --json passthrough or pluck via --field.

func DecodeJSONMap

func DecodeJSONMap(raw []byte, label string) (map[string]any, error)

DecodeJSONMap decodes raw into a map[string]any. Used by REST responses whose top-level shape is always an object.

func IsL1Unreachable

func IsL1Unreachable(body []byte, err error) bool

IsL1Unreachable inspects a REST body / error pair and returns true if the response looks like "gRPC code 13 because L1 RPC isn't configured on this Heimdall". The body may come either from a successful 2xx response that still carries a gRPC-error envelope, or from an HTTPError (4xx/5xx). The transport layer surfaces "dial tcp" / "connection refused" when the REST gateway itself can't reach L1.

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns true if w is an *os.File attached to a terminal.

func NormalizeAddress

func NormalizeAddress(raw string) (string, error)

NormalizeAddress normalizes a 20-byte Ethereum address.

func NormalizeHex

func NormalizeHex(raw string, byteLen int, label string) (string, error)

NormalizeHex accepts a fixed-length hex value with or without the `0x` prefix and returns the lower-case, `0x`-prefixed form expected by the Heimdall REST endpoints. label names the value in errors (e.g. "address", "signer", "tx hash").

func NormalizeHexBytes

func NormalizeHexBytes(raw, label string) (string, error)

NormalizeHexBytes accepts a variable-length hex string with or without the `0x` prefix and returns the lower-case form WITHOUT the prefix (for use as a bare query param). Empty input is an error.

func NormalizeTxHash

func NormalizeTxHash(raw string) (string, error)

NormalizeTxHash normalizes a 32-byte transaction hash. The REST endpoints expect the `0x` prefix and will 500 without it, so we re-add it unconditionally.

func RenderOpts

func RenderOpts(cmd *cobra.Command, cfg *config.Config, fields []string) render.Options

RenderOpts turns a resolved config into a render.Options instance, honouring --json, --field, --color, --raw, and TTY detection.

func Truncate

func Truncate(b []byte, n int) string

Truncate clips b to at most n bytes for inclusion in error messages.

Types

type GRPCErrorBody

type GRPCErrorBody struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

GRPCErrorBody is the standard gRPC-gateway error envelope returned on 4xx/5xx from Heimdall REST. Only `code` and `message` are used here.

type Get

type Get struct {
	Use     string
	Short   string
	Aliases []string
	// Args defaults to cobra.NoArgs.
	Args cobra.PositionalArgs
	// Label names the response in decode/gRPC errors, e.g. "milestone params".
	Label string
	// Path is the fixed REST path. Mutually exclusive with Build.
	Path string
	// Build computes the path and query from the CLI args.
	Build func(cmd *cobra.Command, args []string) (string, url.Values, error)
	// L1Hint surfaces render.HintL1NotConfigured when the call fails
	// with gRPC code 13 / a transport-level dial error, and checks the
	// 2xx body for a gRPC error envelope before rendering.
	L1Hint bool
	// FieldsUsage overrides the --field usage string.
	FieldsUsage string
	// Flags registers extra command flags.
	Flags func(fs *pflag.FlagSet)
	// Opts post-processes the render options (e.g. a --base64 sugar flag).
	Opts func(cmd *cobra.Command, opts *render.Options)
	// Render renders the decoded map when --json is NOT set. nil falls
	// back to RenderKV, after unwrapping UnwrapKey if present.
	Render func(cmd *cobra.Command, m map[string]any, opts render.Options) error
	// RenderBody takes over non-JSON rendering before map decoding
	// (e.g. printing one bare field from a typed struct).
	RenderBody func(cmd *cobra.Command, body []byte, opts render.Options) error
	// UnwrapKey names a single-key envelope to unwrap for KV output.
	UnwrapKey string
}

Get describes a REST GET command: fetch one endpoint, decode the JSON-object body, and render it as JSON or KV. The zero hooks cover the plain case; the optional ones absorb the per-command quirks.

type Pkg

type Pkg struct {
	// Name is the package label used in "not registered" errors.
	Name string
	// Flags is injected by the package's Register call.
	Flags *config.Flags
}

Pkg carries the per-command-package registration state. Each command package declares one and points Flags at the shared flag struct in its Register function.

func (*Pkg) NewGetCmd

func (p *Pkg) NewGetCmd(spec Get) *cobra.Command

NewGetCmd builds the cobra command for a Get spec.

func (*Pkg) NewRPCCmd

func (p *Pkg) NewRPCCmd(spec RPC) *cobra.Command

NewRPCCmd builds the cobra command for an RPC spec.

func (*Pkg) RESTClient

func (p *Pkg) RESTClient(cmd *cobra.Command) (*client.RESTClient, *config.Config, error)

RESTClient resolves the config and constructs a RESTClient. When --curl is set the HTTP call is replaced by a printed curl command.

func (*Pkg) RPCClient

func (p *Pkg) RPCClient(cmd *cobra.Command) (*client.RPCClient, *config.Config, error)

RPCClient resolves the config and constructs an RPCClient against the CometBFT endpoint. When --curl is set the RPC call does not execute; it prints an equivalent curl command instead.

type RPC

type RPC struct {
	Use     string
	Short   string
	Aliases []string
	// Args defaults to cobra.NoArgs.
	Args cobra.PositionalArgs
	// Method is the CometBFT RPC method, e.g. "abci_info".
	Method string
	// FieldsUsage overrides the --field usage string.
	FieldsUsage string
	// Flags registers extra command flags.
	Flags func(fs *pflag.FlagSet)
	// Render renders the raw RPC result when --json is NOT set.
	Render func(cmd *cobra.Command, raw json.RawMessage, opts render.Options) error
}

RPC describes a CometBFT JSON-RPC command: call one method with empty params, pass the raw result through for --json, and hand it to Render otherwise.

Jump to

Keyboard shortcuts

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