output

package
v0.12.0 Latest Latest
Warning

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

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

Documentation

Overview

Package output provides formatters for CLI output: text tables, JSON envelopes, jq filtering, and error rendering. All commands use this package to produce output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyJQ

func ApplyJQ(w io.Writer, jsonData []byte, expr string) error

ApplyJQ filters jsonData with a jq expression and writes raw text output to w. Matches gh CLI --jq behavior: strings are printed unquoted, other values as JSON.

func MarshalForJQ

func MarshalForJQ(data interface{}) ([]byte, error)

MarshalForJQ serializes data to JSON bytes, suitable for ApplyJQ input.

func NewTable

func NewTable(w io.Writer) table.Writer

NewTable creates a go-pretty table writer with a clean, borderless style suitable for CLI output. The table writes to w.

func OutputError

func OutputError(w io.Writer, err error, asJSON bool)

OutputError renders err to w (typically stderr). When asJSON is true, outputs the CLIError JSON envelope. When asJSON is false, outputs human-readable text with suggestion.

func RelativeTime

func RelativeTime(iso8601 string) string

RelativeTime converts an ISO 8601 timestamp to a human-readable relative string such as "5 minutes ago" or "yesterday". If parsing fails, the raw input is returned unchanged (graceful degradation). Only used for table output — JSON always preserves the original ISO 8601 value.

Types

type Formatter

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

Formatter handles output routing between JSON and text (table) modes. Commands call its methods instead of writing directly to stdout.

func NewFormatter

func NewFormatter(ios *iostreams.IOStreams, asJSON bool, jqExpr string) *Formatter

NewFormatter creates a Formatter. asJSON enables JSON output, jqExpr applies a jq filter (implies JSON mode).

func (*Formatter) IsJSON

func (f *Formatter) IsJSON() bool

IsJSON reports whether the formatter is in JSON mode.

func (*Formatter) OutputData

func (f *Formatter) OutputData(data interface{}, tableFn TableFunc) error

OutputData renders a single data object: bare JSON or text table.

func (*Formatter) OutputDryRun

func (f *Formatter) OutputDryRun(payload interface{}, validation string, tableFn TableFunc) error

OutputDryRun renders a dry-run preview. JSON includes dry_run:true plus payload and validation. Text renders via the supplied table function.

func (*Formatter) OutputDryRunWithContext

func (f *Formatter) OutputDryRunWithContext(extras map[string]interface{}, payload interface{}, validation string, tableFn TableFunc) error

OutputDryRunWithContext renders a dry-run preview with optional extra context fields merged at the JSON top level (e.g., key, comment_id). Text output is unaffected by extras.

func (*Formatter) OutputList

func (f *Formatter) OutputList(items interface{}, pagination *PaginationMeta, tableFn TableFunc) error

OutputList renders a list with pagination: JSON envelope or text table.

func (*Formatter) OutputMutation

func (f *Formatter) OutputMutation(extras map[string]interface{}, tableFn TableFunc) error

OutputMutation renders a mutation result: {"ok":true, ...} or text.

func (*Formatter) RawJSON

func (f *Formatter) RawJSON(data interface{}) error

RawJSON serializes data to JSON and writes it. Useful for pass-through output.

type PaginationMeta

type PaginationMeta = api.PaginationMeta

PaginationMeta describes the pagination state returned in list JSON envelopes. Re-exported from api package to avoid breaking callers.

type TableFunc

type TableFunc func(t table.Writer)

TableFunc is a callback that populates a table writer for rendering. Commands implement this to define their text output layout.

Jump to

Keyboard shortcuts

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