output

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 9 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 results and errors.

Two rules govern everything here, and both exist so that piping works:

stdout carries result data and nothing else.
stderr carries progress, warnings and human-readable errors.

If those ever blur, `outplane app list --json | jq` starts failing on a warning banner, which is the kind of bug that is reported as "the CLI is broken" and takes a day to trace.

The renderer is chosen once from the execution context and then used everywhere. Commands never decide how to print; they return data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckFields

func CheckFields(requested, declared []string) error

CheckFields rejects a requested field the command does not offer.

It runs before the command does, which is the only place it can run for a command that streams: those write records as they arrive, so by the time anything could inspect the result the wrong output is already on the screen. Checking against the declared contract rather than against a response also makes the answer independent of the data, so `--fields updatedAt` cannot be accepted for one team and rejected for an empty one.

Types

type Table

type Table struct {
	// Columns are the headers, in display order. For JSON they are also the
	// key order, so that output is stable and diffable.
	Columns []string

	// Rows are maps rather than slices so that --fields can drop columns
	// without the caller having to re-index anything.
	Rows []map[string]any

	// Total is the number of items that exist, which may exceed len(Rows) if a
	// command ever paginates. Reported in band so a consumer never has to
	// guess whether it saw everything.
	Total int

	// Truncated says the result is incomplete. Silently returning a partial
	// list reads as "this is all of it", which is worse than an explicit flag.
	Truncated bool

	// Single marks a result that is one object rather than a collection, so
	// that `app get` emits an object and `app list` emits {items, total}.
	Single bool

	// Footer is printed after the rows, in text mode only.
	//
	// Some results need a closing sentence that belongs after the data rather
	// than before it: "the one you wanted is missing? grant access here". A
	// handler cannot write that itself, because the table is rendered after the
	// handler returns, so anything it printed would land above the table it
	// refers to.
	//
	// Text only. In a machine format the same fact belongs in the command's
	// automation notes, where a consumer can find it without parsing prose.
	Footer string

	// Streamed says the handler has already written the output itself, and
	// there is nothing left to render.
	//
	// Log commands need this. A table is built in memory and printed at the
	// end, which is the one thing a stream must not do: a follow has no end,
	// and even a finished build log is thousands of lines nobody wants
	// buffered. Those handlers write as they read and set this, rather than
	// pretending to return rows.
	Streamed bool

	// Headers override the text table's column titles.
	//
	// A title is derived from the field name, which is right almost everywhere:
	// the name is the contract and repeating it as the heading keeps --json and
	// the table talking about the same thing. It stops being right when the
	// name carries a unit the value already shows, where the heading reads
	// "MEMORY BYTES" above "697 MiB".
	//
	// Text only. The field name never changes.
	Headers map[string]string

	// Units say how a column is written in the text table. A column with no
	// entry is printed as it is, which is what every field of every command was
	// before one of them started reporting bytes.
	//
	// Text only, deliberately. The machine formats carry the raw number, so a
	// consumer never has to parse "697 MiB" back into something it can compare.
	Units map[string]Unit

	// Declared is every field this command documents, from its registry entry.
	// Filled in by the caller rather than by the handler, so that no handler
	// has to restate what the registry already says.
	//
	// It exists so --fields validates against the command's contract instead of
	// against whatever this particular response happened to contain. Without
	// it, `--fields updatedAt` is accepted against a team with applications and
	// rejected against an empty one, which is a script that works all week and
	// fails on a Monday.
	Declared []string
}

Table is a command's result in a form both a person and a machine can use.

Commands build one of these instead of formatting anything, which is what lets the same result become a terminal table, a JSON object, or an NDJSON stream without the command knowing which.

type Unit

type Unit int

Units for the text table.

A byte count is the clearest case for why this exists: 730365952 is a true answer to "how much memory" and a useless one, while "697 MiB" is the same number in a form somebody can act on. The conversion belongs here rather than in the command, because it must not reach --json: a consumer wants the number it can compare, not a string it has to parse back.

const (
	UnitNone Unit = iota
	UnitBytes
	UnitBytesPerSecond
	UnitMillicores
	UnitPercent
)

type Writer

type Writer struct {
	Out io.Writer
	Err io.Writer
	Ctx execctx.Context

	// Fields is --fields, already validated against what the command declares.
	//
	// Result takes the same list as an argument, because it narrows a whole
	// table at once. Item cannot: a stream is written record by record while
	// the command is still running, so the narrowing has to be here rather
	// than in something that sees the finished result.
	Fields []string

	// RawStream says stdout carries text this command did not shape: an
	// application's own log lines, not a result the CLI built.
	//
	// It changes where an error goes. Normally a machine-mode error is written
	// to stdout so a consumer can parse one stream and find either a result or
	// a failure. That reasoning does not survive here, because stdout is
	// already raw text and an envelope appended to it is not parseable
	// alongside the lines: `outplane logs -f > app.log` would end with a JSON
	// object in the middle of the log file.
	RawStream bool
}

Writer renders results and errors to the right streams.

func New

func New(out, errw io.Writer, ctx execctx.Context) *Writer

New builds a Writer for the given context.

func (*Writer) Error

func (w *Writer) Error(err error) int

Error renders a failure and returns the process exit code.

In a machine format the error envelope goes to STDOUT, replacing the result. That is deliberate and slightly unusual: it means a consumer can parse stdout unconditionally and find either a result or an error there, without having to capture two streams and decide which to believe. A short human line still goes to stderr so that a person watching a terminal sees something readable.

In text mode nothing goes to stdout at all, so `outplane app list > apps.txt` leaves an empty file on failure rather than a file full of error text.

func (*Writer) Item

func (w *Writer) Item(row map[string]any, text string)

Item writes one record of a stream to stdout, as it arrives.

Result renders a table that is complete; this is for output that is not, and never will be: a follow has no end, so there is nothing to buffer and align. The caller supplies both shapes because only it knows the columns, and the choice between them belongs here with every other format decision.

The text form is expected to be pre-aligned to fixed widths. A live stream cannot size its columns from the rows it has not received yet, and columns that jump on every poll are harder to read than columns that are too wide.

func (*Writer) Note

func (w *Writer) Note(format string, args ...any)

Note writes an informational line to stderr.

Always stderr, never stdout, and suppressed by --quiet. A note on stdout would corrupt the output of a command being piped, which is the single easiest way to break every script a user has written.

func (*Writer) Result

func (w *Writer) Result(t Table, fields []string) error

Result renders a successful outcome to stdout.

Jump to

Keyboard shortcuts

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