output

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package output is the single rendering, error, and exit-code authority for the CLI. Commands return errors and typed results through this package; nothing else writes to stdout/stderr or decides process exit codes.

Index

Constants

View Source
const (
	// ExitOK means the command succeeded.
	ExitOK = 0
	// ExitError means a generic failure.
	ExitError = 1
	// ExitCancelled means the user or the environment cancelled the run.
	ExitCancelled = 2
	// ExitNotFound means a requested resource does not exist.
	ExitNotFound = 3
	// ExitAuth means authentication is required or failed.
	ExitAuth = 4
)

Process exit codes. These are a public contract for scripts and agents.

Variables

This section is empty.

Functions

func RenderError

func RenderError(w io.Writer, err error, jsonMode bool) int

RenderError writes err to w — as a one-line JSON envelope {"error":{"code":"...","message":"...","hint":"..."}} when jsonMode is true, otherwise as human-readable "error: ..." / "hint: ..." lines — and returns the process exit code for the error. It must be called exactly once per failed invocation.

func RenderFields

func RenderFields(w io.Writer, rows [][2]string)

RenderFields writes label/value rows as two aligned columns — labels padded to the widest label, a two-space gutter, values verbatim with no trailing padding. It is the human layout for `show` commands.

func RenderTable

func RenderTable(w io.Writer, headers []string, rows [][]string)

RenderTable writes headers and rows as plain aligned columns: each column is padded to its widest cell, columns are separated by a two-space gutter, and the last cell of each line carries no trailing padding. No color, no external dependencies.

Types

type CodedError

type CodedError struct {
	Code    int
	ErrCode string
	Message string
	Hint    string
}

CodedError is an error carrying a process exit code, a machine-readable error code for the JSON envelope, and an optional remediation hint.

func AuthError

func AuthError(msg, hint string) *CodedError

AuthError returns a CodedError for missing or failed authentication (exit 4).

func CancelledError

func CancelledError(msg string) *CodedError

CancelledError returns a CodedError for a cancelled run (exit 2).

func ErrorWithHint

func ErrorWithHint(msg, hint string) *CodedError

ErrorWithHint returns a generic CodedError (exit 1) carrying a remediation hint for the envelope/human output.

func Errorf

func Errorf(format string, a ...any) *CodedError

Errorf returns a generic CodedError (exit 1) with a formatted message.

func NotFoundError

func NotFoundError(msg, hint string) *CodedError

NotFoundError returns a CodedError for a missing resource (exit 3).

func (*CodedError) Error

func (e *CodedError) Error() string

Error implements the error interface.

type Mode

type Mode struct {
	// TTY is true when stdout is a terminal.
	TTY bool
	// CI is true when running under a CI system (CI or GITHUB_ACTIONS set).
	CI bool
	// Agent identifies a detected coding agent ("claude-code", "gemini-cli",
	// "cursor") or is empty when none is detected.
	Agent string
	// Color is true when ANSI color output is appropriate.
	Color bool
	// JSON is true when the user requested machine-readable JSON output.
	JSON bool
}

Mode describes the execution environment and the resulting rendering preferences for a single CLI invocation.

func DetectMode

func DetectMode(jsonFlag, noColorFlag bool) Mode

DetectMode inspects flags and the environment and returns the output Mode for this invocation.

func (Mode) Interactive

func (m Mode) Interactive() bool

Interactive reports whether it is appropriate to prompt the user: stdout is a TTY and we are not running under CI or a coding agent.

type Printer

type Printer struct {
	Out  io.Writer
	Err  io.Writer
	Mode Mode
	// Quiet suppresses Status lines (--quiet); data on Out is never affected.
	Quiet bool
}

Printer is the single writer for command output. Data goes to Out (stdout), progress goes to Err (stderr) so that piped/JSON consumers see clean data.

func New

func New(mode Mode) *Printer

New returns a Printer bound to os.Stdout and os.Stderr.

func (*Printer) EmitJSON

func (p *Printer) EmitJSON(v any) error

EmitJSON writes v as 2-space-indented JSON followed by a newline to Out.

func (*Printer) EmitJSONLine

func (p *Printer) EmitJSONLine(v any) error

EmitJSONLine writes v as one compact JSON line to Out — the NDJSON building block for streaming commands (e.g. `login --json`).

func (*Printer) Human

func (p *Printer) Human(format string, a ...any)

Human writes formatted primary output to Out.

func (*Printer) Status

func (p *Printer) Status(format string, a ...any)

Status writes a formatted progress line to Err. It is suppressed in JSON mode — so machine consumers never see interleaved progress noise — and by Quiet (--quiet).

Jump to

Keyboard shortcuts

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