output

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatValue

func FormatValue(v any) string

FormatValue converts a value to its string representation for table/csv/plain output.

func IsTerminal added in v1.19.0

func IsTerminal(fd uintptr) bool

IsTerminal reports whether the given file descriptor is a character device. Used to choose human (table) vs machine (json) defaults and to gate color.

func ResolveFormat added in v1.19.0

func ResolveFormat(flagChanged bool, current, configDefault string, isTTY, hasOutFile bool) string

ResolveFormat decides the effective output format. Precedence: explicit --output flag > config default_output > auto (TTY -> table, otherwise json). Writing to a file is treated as non-interactive.

Types

type Format

type Format string

Format represents an output format

const (
	FormatTable     Format = "table"
	FormatJSON      Format = "json"
	FormatJSONMulti Format = "json-multi" // internal: like json but triggers column selection in selectTableColumns
	FormatCSV       Format = "csv"
	FormatYAML      Format = "yaml"
	FormatPlain     Format = "plain"
	FormatXML       Format = "xml"    // Classic API native format — pretty-printed XML
	FormatRaw       Format = "raw"    // Exact wire bytes, no conversion or formatting
	FormatNDJSON    Format = "ndjson" // newline-delimited JSON: one compact object per line, no array
)

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter handles output formatting

func New

func New(format string, noColor bool, wide bool) *Formatter

New creates a new formatter

func (*Formatter) Format

func (f *Formatter) Format() string

Format returns the current output format string.

func (*Formatter) PaginationProgress added in v1.22.0

func (f *Formatter) PaginationProgress() *progress.Reporter

PaginationProgress builds a progress reporter for an --all pagination loop, choosing the rendering mode from the formatter's state: silent when quiet, an in-place count line on an interactive color terminal, NDJSON page_fetch events otherwise.

func (*Formatter) Print

func (f *Formatter) Print(data any) error

Print outputs data in the configured format

func (*Formatter) PrintBytes

func (f *Formatter) PrintBytes(data []byte) error

PrintBytes writes bytes to the output. XML is pretty-printed unless the format is FormatRaw, which writes exact wire bytes. Used by Classic API commands to emit XML when no structured format is requested.

func (*Formatter) PrintError

func (f *Formatter) PrintError(err error, code string, details map[string]any)

PrintError outputs an error in the appropriate format

func (*Formatter) PrintRaw

func (f *Formatter) PrintRaw(data []byte) error

PrintRaw outputs raw bytes (usually JSON from the API). XML responses (from Classic API) are converted to JSON before formatting, unless the format is FormatXML (pretty-printed) or FormatRaw (exact wire bytes).

func (*Formatter) SetExplicitNoColor added in v1.22.0

func (f *Formatter) SetExplicitNoColor(v bool)

SetExplicitNoColor records whether the user explicitly disabled color (--no-color or NO_COLOR), as distinct from color being auto-disabled because stdout is piped. Pagination progress uses this so a piped stdout still shows the in-place stderr counter when stderr is a terminal.

func (*Formatter) SetNoHints added in v1.18.0

func (f *Formatter) SetNoHints(v bool)

SetNoHints suppresses advisory hints (e.g. the list-size hint) written to stderr. Unlike SetQuiet it leaves the spinner and progress output alone — a narrower opt-out. Errors and primary output on stdout are unaffected.

func (*Formatter) SetProjector added in v1.17.0

func (f *Formatter) SetProjector(p Projector)

SetProjector configures field-level projection (e.g. --compact) applied before format-specific rendering. A zero-value projector is a no-op.

func (*Formatter) SetQuiet added in v1.17.0

func (f *Formatter) SetQuiet(q bool)

SetQuiet suppresses advisory output written to stderr (e.g. the list-size hint). Errors and primary output on stdout are unaffected.

func (*Formatter) SetWriter

func (f *Formatter) SetWriter(w io.Writer)

SetWriter replaces the output destination.

func (*Formatter) Writer added in v1.17.0

func (f *Formatter) Writer() io.Writer

Writer returns the current output destination. Power commands that render their own text (e.g. `doctor`) need it to honour --out-file.

type Projector added in v1.17.0

type Projector struct {
	Compact bool
	Select  []string
}

Projector applies field-level projection to flattened rows before format-specific rendering. Compact keeps only scalar fields after flattening, dropping arrays and nested remnants. Select keeps only the listed dot paths. When both are set, Select wins. The projector is shared by every output format (json, table, csv, yaml, plain) so projection behaves consistently.

func (Projector) Apply added in v1.17.0

func (p Projector) Apply(rows []map[string]any) []map[string]any

Apply returns rows projected per the configured rules. Always flattens nested objects to dot keys so projection sees a flat shape. Empty rows pass through unchanged.

Select uses flattenRowsRaw (no common-prefix stripping) so user-supplied dot paths like "general.name" still match on single-section responses where stripCommonPrefix would otherwise rewrite "general.name" → "name" and silently produce empty projections.

func (Projector) IsZero added in v1.17.0

func (p Projector) IsZero() bool

IsZero reports whether the projector has no rules configured.

Jump to

Keyboard shortcuts

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