output

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package output renders API responses for the sectors CLI.

The CLI is agent-facing first: the default output is the API's raw JSON, emitted losslessly. When stdout is an interactive terminal we pretty-print for humans; when piped (the common case for an agent) we stay compact and machine-parseable. Errors are emitted as JSON on stderr with a non-zero exit code so callers can branch on them programmatically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Category

func Category(status int) (category string, retryable bool)

Category classifies an HTTP status into a stable, agent-friendly bucket and reports whether the condition is retryable. status 0 means a client-side or transport error.

func Count

func Count(body []byte) ([]byte, error)

Count reduces a response to {"count": n}, where n is the length of the primary list (or 1 for a single object).

func EmitError

func EmitError(status int, msg string, body []byte)

EmitError writes a structured error to stderr as JSON. status is the HTTP status code when the error came from the API (0 for client-side errors), and body is the API's raw error payload if any. The envelope always carries a `category` and (when true) `retryable` so agents can branch programmatically.

func EmitJSON

func EmitJSON(w io.Writer, body []byte, format Format) error

EmitJSON writes a raw JSON document (as returned by the API) to w in the requested format. body must already be valid JSON.

func IsTTY

func IsTTY(f *os.File) bool

IsTTY reports whether the given file is an interactive terminal.

func Project

func Project(body []byte, paths []string) ([]byte, error)

Project returns a new JSON document containing only the requested dotted paths. Use `a.b` to descend objects and `key[]` to map over an array (e.g. `results[].symbol`, or `[].symbol` for a top-level array). Paths that don't exist are skipped.

func Truncate

func Truncate(body []byte, max int) ([]byte, error)

Truncate caps the primary list to max items. For object-wrapped lists (`{results:[…]}` / `{data:[…]}`) it adds `"_truncated": true` when it cut anything; bare top-level arrays are simply shortened.

Types

type Format

type Format string

Format selects how successful responses are rendered.

const (
	// FormatAuto picks Pretty on a TTY, JSON otherwise.
	FormatAuto Format = "auto"
	// FormatJSON is compact single-line JSON.
	FormatJSON Format = "json"
	// FormatPretty is indented JSON.
	FormatPretty Format = "pretty"
	// FormatTable renders tabular responses as a table, falling back to Pretty
	// for data that isn't a list of records.
	FormatTable Format = "table"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat validates a --output value.

Jump to

Keyboard shortcuts

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