output

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package output centralises CLI output routing.

Two design rules drive the package:

  1. In JSON mode, stdout carries ONLY the JSON payload (or a structured error envelope). Every status line, spinner, prompt, progress bar, and human-readable note goes to stderr. Agents and `jq` pipelines can rely on `vulncheck <cmd> --json` producing parseable output.

  2. In text mode, everything goes to stdout, matching today's behaviour. Styling is suppressed when stdout is not a TTY or when NO_COLOR is set.

Commands obtain a Renderer once and call its methods instead of reaching for fmt.Print / pkg/ui directly. The Renderer is the single seam between "thing happened" and "bytes on a file descriptor".

Index

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the integer version of the CLI's machine-readable payload shapes (the error envelope, auth status, version info, batch results, sync summaries). Bump it on a breaking change so agents can detect a CLI they don't speak.

Stability contract:

  • schema_version is stamped only on CLI-shaped payloads. Pass-through API responses (cpe / purl / tag / pdns / indices list) keep the server's existing shape.
  • Adding optional fields is NOT a breaking change.
  • Removing or renaming a field IS a breaking change → bump.

Variables

This section is empty.

Functions

func ColorEnabled

func ColorEnabled() bool

ColorEnabled reports whether ANSI colour output should be emitted on stdout. Honours NO_COLOR (https://no-color.org) and TERM=dumb, and falls back to "stdout is a terminal" otherwise.

func Interactive

func Interactive() bool

Interactive reports whether the process can safely show interactive UI (prompts, spinners, full-screen TUIs). False whenever stdin or stdout is non-TTY, or when CI/NO_COLOR-style signals are set.

func IsCI

func IsCI() bool

IsCI reports whether the process appears to be running under a CI system. Mirrors the heuristic used elsewhere in the codebase; kept here so the output package has no dependency on pkg/config.

func IsTTY

func IsTTY(f *os.File) bool

IsTTY reports whether the given file is attached to a terminal.

func WithContext

func WithContext(ctx context.Context, r *Renderer) context.Context

WithContext attaches a Renderer to ctx. The root command's PersistentPreRunE installs one; subcommands retrieve it via FromCmd.

Types

type Mode

type Mode int

Mode selects the output shape.

const (
	// ModeText emits human-readable, optionally styled output. Default.
	ModeText Mode = iota
	// ModeJSON emits a single JSON document (or structured error) on stdout.
	// All informational output is routed to stderr.
	ModeJSON
)

func (Mode) String

func (m Mode) String() string

type Options

type Options struct {
	Mode        Mode
	Color       bool // when false, callers should render without ANSI sequences
	Quiet       bool
	Interactive bool      // when false, commands MUST NOT block on prompts
	Stdout      io.Writer // defaults to os.Stdout
	Stderr      io.Writer // defaults to os.Stderr
}

Options controls Renderer construction.

type Renderer

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

Renderer routes informational, payload, and error output to the right stream based on the selected Mode. Zero value is not useful; use New().

func FromCmd

func FromCmd(cmd *cobra.Command) *Renderer

FromCmd is shorthand for FromContext(cmd.Context()). Use in command RunE bodies: `r := output.FromCmd(cmd)`.

func FromContext

func FromContext(ctx context.Context) *Renderer

FromContext returns the Renderer attached to ctx, or a sensible default (text mode, env-derived) when none is present. Always returns non-nil.

func New

func New(opts Options) *Renderer

New constructs a Renderer from explicit options. Callers that want environment defaults should use NewFromEnv.

func NewFromEnv

func NewFromEnv(mode Mode) *Renderer

NewFromEnv builds a Renderer using TTY / NO_COLOR / CI detection. Mode is taken from the caller (typically a --json flag); everything else is inferred from the environment.

func (*Renderer) Color

func (r *Renderer) Color() bool

Color reports whether ANSI styling should be applied. Callers that build their own styled strings (lipgloss, etc.) should consult this.

func (*Renderer) Info

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

Info writes an informational status line. Routed to stderr in JSON mode so the stdout payload stays clean. Suppressed entirely when quiet.

func (*Renderer) InfoStream

func (r *Renderer) InfoStream() io.Writer

InfoStream returns the stream that informational output should go to. stderr in JSON mode, stdout otherwise. Useful when wiring a third-party component (taskin, bubbletea) that takes an io.Writer.

func (*Renderer) Interactive

func (r *Renderer) Interactive() bool

Interactive reports whether commands may block on TUI prompts (huh forms, bubbletea screens). Returns false under CI, --no-interactive, when stdin is non-TTY, or when --json is set (JSON output is for machines — never block them on prompts).

func (*Renderer) IsJSON

func (r *Renderer) IsJSON() bool

IsJSON is the common shorthand: `if r.IsJSON() { … }`.

func (*Renderer) JSON

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

JSON marshals payload as indented JSON and writes it, followed by a newline, to stdout. HTML-escaping (< > &) is disabled: the CLI's stdout is not an HTML embedding, so escaping "<name>" to "<name>" just makes example strings and validation messages unreadable.

func (*Renderer) Mode

func (r *Renderer) Mode() Mode

Mode returns the renderer's mode. Useful for commands that need to choose between two payload shapes (table vs JSON) at a single call site.

func (*Renderer) Printf

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

Printf writes a plain text fragment to the payload stream. Has no effect in JSON mode.

func (*Renderer) Println

func (r *Renderer) Println(args ...any)

Println writes a plain text line to the payload stream. Has no effect in JSON mode (where stdout is reserved for the JSON document).

func (*Renderer) Quiet

func (r *Renderer) Quiet() bool

Quiet reports whether informational output is suppressed.

func (*Renderer) Stat

func (r *Renderer) Stat(label, value string)

Stat writes a "label: value" pair. Same routing as Info.

func (*Renderer) Stderr

func (r *Renderer) Stderr() io.Writer

Stderr returns the diagnostic stream. Use for progress bars, spinners, and anything that must not contaminate stdout in JSON mode.

func (*Renderer) Stdout

func (r *Renderer) Stdout() io.Writer

Stdout returns the payload stream. Use for raw payload writes when the structured helpers (JSON, Println) are not a fit — e.g. a command that streams bytes (backup download to stdout).

func (*Renderer) Success

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

Success writes a positive status line. Same routing as Info.

func (*Renderer) Warn

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

Warn writes a warning line. Always emitted (even when quiet), since a warning that the user doesn't see is rarely the right call. Routed to stderr regardless of mode.

Jump to

Keyboard shortcuts

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