output

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package output renders command results. stdout carries exactly one JSON document, because that is what an agent parses; everything a person needs to read goes to stderr.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeOf

func CodeOf(err error) string

CodeOf maps an error onto the CLI's stable error code, which is what an agent branches on. The matching exit codes live in the client package.

func EnsureSlice

func EnsureSlice[T any](values []T) []T

EnsureSlice returns a non-nil slice so an empty list encodes as [] rather than null: a caller iterating the result should not have to special-case it.

func ProtoValue

func ProtoValue(message proto.Message) (json.RawMessage, error)

ProtoValue renders a protobuf message for embedding in an envelope.

A message put into an envelope as-is would be encoded by encoding/json, which reads the struct tags protoc-gen-go emits: snake_case. The same relation would then look different depending on whether it was the whole result or one entry of a list.

func ProtoValues

func ProtoValues[T proto.Message](messages []T) (json.RawMessage, error)

ProtoValues renders a list of protobuf messages for embedding in an envelope. An empty list encodes as [], never as null.

Types

type Error

type Error struct {
	Code    string   `json:"code"`
	Message string   `json:"message"`
	Hint    string   `json:"hint,omitempty"`
	Details []string `json:"details,omitempty"`
}

Error is the machine-readable error envelope. Hint carries an actionable next step, which is what lets an agent recover without guessing.

type Format

type Format string

Format is the rendering the caller asked for.

const (
	// FormatJSON is the default: one JSON document on stdout.
	FormatJSON Format = "json"
	// FormatTable is for people.
	FormatTable Format = "table"
)

func ParseFormat

func ParseFormat(value string) (Format, error)

ParseFormat validates the --format value.

type Renderer

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

Renderer writes results to stdout and progress to stderr.

func New

func New(format Format) *Renderer

New returns a renderer writing to the process streams.

func NewWith

func NewWith(format Format, stdout, stderr io.Writer) *Renderer

NewWith returns a renderer writing to the given streams, for tests.

func (*Renderer) Envelope

func (r *Renderer) Envelope(value any, rows []Row) error

Envelope renders a command-specific result shape in JSON mode and the supplied rows in table mode.

func (*Renderer) Format

func (r *Renderer) Format() Format

Format reports the requested rendering.

func (*Renderer) JSON

func (r *Renderer) JSON(value any) error

JSON writes any value as one indented JSON document.

func (*Renderer) Message

func (r *Renderer) Message(message proto.Message, rows []Row) error

Message renders a protobuf message. Table mode uses the supplied rows, and falls back to JSON when the caller had none to give.

func (*Renderer) Progress

func (r *Renderer) Progress(format string, args ...any)

Progress writes a human-readable line to stderr. It is never part of the machine-readable contract.

func (*Renderer) ProtoJSON

func (r *Renderer) ProtoJSON(message proto.Message) error

ProtoJSON writes a protobuf message using protojson.

func (*Renderer) Table

func (r *Renderer) Table(rows []Row) error

Table renders rows as an aligned table on stdout.

func (*Renderer) WriteError

func (r *Renderer) WriteError(err Error)

WriteError writes the error envelope to stderr.

type Row

type Row []string

Row is one table row.

Jump to

Keyboard shortcuts

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