output

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package output renders command results to stdout, choosing human text or JSON based on the --json flag. It is the analog of an HTTP response layer: the one place that decides how a result is shaped for the caller.

Golden rule: stdout is for machine/result output, stderr is for diagnostics (the logger). Never mix the two.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExitCode

func ExitCode(err error) int

ExitCode maps a (possibly wrapped) domain sentinel to a process exit code. Kept here so the CLI and the output layer agree on the mapping.

Types

type Event

type Event struct {
	Kind string `json:"kind"`
	Time string `json:"ts,omitempty"`
	Data any    `json:"data,omitempty"`
}

Event is one streaming output record. In JSON mode it is emitted as a single NDJSON line; in human mode it renders as a short progress line.

type Writer

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

Writer renders results either as human text or JSON.

func New

func New(jsonMode bool, out io.Writer) *Writer

New builds a Writer. Pass os.Stdout for out.

func (*Writer) Error

func (w *Writer) Error(err error)

Error renders a failure. JSON: {"ok":false,"error":{code,message}}. Human: "error: <message>". The code is derived from the wrapped domain sentinel.

func (*Writer) Event

func (w *Writer) Event(kind string, v any)

Event emits one streaming record. JSON: a compact NDJSON line. Human: a short bullet line.

func (*Writer) JSON

func (w *Writer) JSON() bool

JSON reports whether the writer is in JSON mode (so callers can suppress chatty human-only output).

func (*Writer) Result

func (w *Writer) Result(v any, render func() string)

Result renders a successful result. JSON: {"ok":true,"data":<v>} (pretty). Human: prints render() if non-nil.

func (*Writer) Table

func (w *Writer) Table(headers []string, rows [][]string, jsonRows any)

Table renders rows. JSON mode emits jsonRows via Result; human mode prints an aligned table.

Jump to

Keyboard shortcuts

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