Documentation
¶
Overview ¶
Package output is the single place the CLI writes results. A command hands the writer one Result; the writer decides between a JSON envelope, bare data, an ID list, a count, or a human table based on flags and the terminal.
Index ¶
- func ColorEnabled(format Format, stdoutIsTTY bool, noColorFlag bool, getenv func(string) string) bool
- func IsTerminal(w io.Writer) bool
- func Plural(n int, singular string) string
- func Truncate(s string, width int) string
- type Envelope
- type ErrorEnvelope
- type Flags
- type Format
- type Printer
- func (p *Printer) Blank()
- func (p *Printer) Bold(s string) string
- func (p *Printer) Cyan(s string) string
- func (p *Printer) Dim(s string) string
- func (p *Printer) Green(s string) string
- func (p *Printer) KeyValues(pairs [][2]string)
- func (p *Printer) Line(format string, args ...any)
- func (p *Printer) Red(s string) string
- func (p *Printer) Table(headers []string, rows [][]string)
- func (p *Printer) Yellow(s string) string
- type Result
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColorEnabled ¶
func ColorEnabled(format Format, stdoutIsTTY bool, noColorFlag bool, getenv func(string) string) bool
ColorEnabled applies NO_COLOR, TERM=dumb, and the --no-color flag.
func IsTerminal ¶
IsTerminal reports whether w is backed by a character device. Writers that do not expose file metadata, such as buffers and pipes wrapped in an arbitrary io.Writer, are treated as non-terminals.
Types ¶
type Envelope ¶
type Envelope struct {
OK bool `json:"ok"`
SchemaVersion int `json:"schemaVersion"`
Data any `json:"data"`
Summary string `json:"summary,omitempty"`
Notices []string `json:"notices,omitempty"`
Meta map[string]any `json:"meta,omitempty"`
Error string `json:"error,omitempty"`
Code string `json:"code,omitempty"`
ExitCode int `json:"exitCode,omitempty"`
Hint string `json:"hint,omitempty"`
}
Envelope is the JSON success shape.
type ErrorEnvelope ¶
type ErrorEnvelope struct {
OK bool `json:"ok"`
SchemaVersion int `json:"schemaVersion"`
Error string `json:"error"`
Code string `json:"code"`
ExitCode int `json:"exitCode"`
Hint string `json:"hint,omitempty"`
Command string `json:"command,omitempty"`
}
ErrorEnvelope is the JSON failure shape, written to stderr.
type Flags ¶
type Flags struct {
JSON bool
Plain bool
Quiet bool
IDsOnly bool
Count bool
Agent bool
NoColor bool
JQ string
}
Flags mirrors the root persistent flags that pick a format.
type Printer ¶
Printer renders the human view. Every method records the first write error so callers can check once.
func (*Printer) Bold ¶
Bold, Dim, Red, Green, Yellow, Cyan wrap s in ANSI-16 codes when color is on.
type Result ¶
type Result struct {
// Data is the structured value. Nil slices are emitted as [].
Data any
// Summary is one sentence for humans and the envelope.
Summary string
// Notices are things the caller should know that are not errors.
Notices []string
// Meta is merged into the envelope's meta object.
Meta map[string]any
// Plain renders the human view. When nil, pretty JSON of Data is used.
Plain func(p *Printer)
// Err marks the result as a failure that still carries data (a receipt
// with failed items, an unhealthy doctor). The envelope gets ok:false
// and the error fields; the caller exits with the error's code.
Err *clierr.Error
}
Result is what a command produces.
type Writer ¶
type Writer struct {
Format Format
Stdout io.Writer
Stderr io.Writer
Color bool
JQ string
Command string
SchemaVersion int
// contains filtered or unexported fields
}
Writer renders results and errors.
func New ¶
func New(format Format, stdout, stderr io.Writer, color bool, jq, command string, schemaVersion int) (*Writer, error)
New builds a writer. command is the space-joined command path used in meta. The jq expression is compiled here so a bad one fails before the command runs.
func (*Writer) AddNotice ¶
AddNotice queues a warning for the next Write. In plain mode it is printed to stderr right away.