output

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package output renders API objects as tables, JSON or YAML.

Index

Constants

This section is empty.

Variables

View Source
var Formats = []string{"table", "wide", "json", "yaml", "name"}

Formats accepted by -o.

Functions

func Age

func Age(v any) string

Age renders an epoch timestamp (seconds) as time elapsed since then.

func Decode

func Decode(raw json.RawMessage) map[string]any

Decode turns a raw JSON object into a generic map (numbers kept exact).

func Duration

func Duration(v any) string

Duration renders a number of seconds as a compact duration (3d4h).

func Get

func Get(obj map[string]any, path string) any

Get walks a dotted path through nested maps.

func HumanDuration

func HumanDuration(d time.Duration) string

HumanDuration formats d like kubectl ages: 45s, 12m, 5h3m, 3d4h, 2y.

func KV

func KV(w io.Writer, pairs [][2]string)

KV prints aligned "Key: value" lines (used by describe headers).

func Safe

func Safe(s string) string

Safe makes a value safe to print on a terminal: control characters (C0, DEL, C1, which include ESC and CSI), line and paragraph separators and bidirectional overrides are shown as \xNN / \uNNNN escapes, and a tab becomes a space so it cannot break table alignment. Use it for every value that came from the API in human-readable output; JSON and YAML have their own escaping and keep the data as is.

func SortBy

func SortBy(items []json.RawMessage, c Column)

SortBy sorts items by the display value of a column (case-insensitive).

func Text

func Text(v any) string

Text renders scalars and short lists. Trailing NUL bytes, which some clients report at the end of their hostname, are dropped.

func ValidFormat

func ValidFormat(f string) bool

ValidFormat reports whether f is a known output format.

func WriteYAML

func WriteYAML(w io.Writer, raw []byte) error

WriteYAML converts JSON to block-style YAML while keeping key order.

Types

type Column

type Column struct {
	Header string
	// Paths are dotted JSON paths tried in order; the first non-empty wins.
	Paths []string
	// Format converts the raw value to display text. Defaults to Text.
	Format func(v any) string
	// Wide columns only appear with -o wide.
	Wide bool
}

Column describes one table column.

func Col

func Col(header string, paths ...string) Column

Col is a shorthand constructor.

func (Column) AsWide

func (c Column) AsWide() Column

AsWide returns a copy of c shown only in wide mode.

func (Column) Value

func (c Column) Value(obj map[string]any) string

Value returns the first non-empty value of the column for obj.

func (Column) WithFormat

func (c Column) WithFormat(f func(any) string) Column

WithFormat returns a copy of c using f.

type Printer

type Printer struct {
	Format    string
	NoHeaders bool
	// NameFunc returns the identifier printed by -o name.
	NameFunc func(obj map[string]any) string
}

Printer writes objects in the selected format.

func (Printer) PrintList

func (p Printer) PrintList(w io.Writer, items []json.RawMessage, cols []Column) error

PrintList prints a list of raw JSON objects.

func (Printer) PrintObject

func (p Printer) PrintObject(w io.Writer, raw json.RawMessage) error

PrintObject prints a single raw JSON document (json or yaml; table/wide fall back to yaml, which is the most readable form for nested objects).

Jump to

Keyboard shortcuts

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