output

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package output turns a decoded JSON reply into something on screen.

The default is a table for people; -o json is for pipes. The relationship between them is stated in --output's help: tables may be restyled at any time, JSON will not.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadColumnSpec = errors.New("malformed column specification")

	ErrBadFieldPath = errors.New("malformed field path")
)
View Source
var (
	ErrUnknownFormat = errors.New("unknown output format")

	ErrNotRenderable = errors.New("value cannot be rendered")
)
View Source
var Formats = []string{
	string(FormatTable),
	string(FormatJSON),
	string(FormatYAML),
	string(FormatXML),
	"custom-columns=NAME:.field,...",
}

Functions

func ParseFormat

func ParseFormat(value string) (Format, []Column, error)

ParseFormat reads the value of --output, which is either a plain format name or `custom-columns=HEADER:.path,...`.

Types

type Column

type Column struct {
	Header string
	Path   []step
}

Column is one user-defined table column, as in -o custom-columns=NAME:.name,SIZE:.size_gb

The syntax follows kubectl's, deliberately: it is the one people already know, and it is small enough to specify exactly — a dotted field path with optional array indices, not general JSONPath.

func ParseColumns

func ParseColumns(text string) ([]Column, error)

ParseColumns reads `HEADER:.path,HEADER:.path`.

func (Column) Resolve

func (c Column) Resolve(value any) any

Resolve walks the path. A missing field yields nil, which prints as "-": absent is a normal state across a list of mixed resources, not an error.

type Format

type Format string
const (
	FormatTable Format = "table"

	FormatJSON Format = "json"

	FormatYAML Format = "yaml"

	// FormatXML is for a model reading the reply. See xml.go for why tags beat
	// braces for that reader.
	FormatXML Format = "xml"
)

type Printer

type Printer struct {
	Format Format

	// Columns, when set, replaces the automatic column choice for table output.
	Columns []Column

	Out io.Writer

	// Width is the terminal width used to decide how many columns fit. Zero
	// means unknown, and then a fixed cap applies instead.
	Width int

	// Notice receives a line about anything hidden, on stderr, so that stdout
	// stays exactly the table.
	Notice io.Writer

	// Root names the outermost XML element. A reply is an unnamed value and XML
	// has no way to write one, so something has to be chosen; a caller that knows
	// what it asked for can say so instead of taking "result".
	Root string
}

func (*Printer) Print

func (p *Printer) Print(value any) error

Print renders one value in the selected format.

Everything is normalised first, so all three formats describe the same value and convert into each other losslessly. Without it, a Go struct would render under its json tags via -o json and its yaml tags via -o yaml, giving two different field names for one field.

Jump to

Keyboard shortcuts

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