output

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 10 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),
	"custom-columns=NAME:.field,...",
}

Functions

func Normalize

func Normalize(value any) (any, error)

Normalize converts any value into the JSON data model — map[string]any, []any, string, float64, bool, nil.

Replies decoded from a service are already in it. Commands that build their own output (auth status, project list, spec list) hand over Go structs, and this is where they join the same pipeline, so -o json works for them exactly as it does for a passthrough call.

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"
)

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
}

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