output

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package output prints command results and errors as text or JSON.

Results go to stdout. In JSON mode errors also go to stdout, so a caller always reads exactly one JSON document there. In text mode errors go to stderr.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    ExitCode
	Message string
	Hint    string
	Err     error
	// Silent errors only set the exit code. The command has already printed
	// its result, and printing an error too would break the one-JSON-document rule.
	Silent bool
}

Error is an error with an exit code and an optional hint that tells the user how to fix it.

func Exit

func Exit(code ExitCode, message string) *Error

Exit returns a silent error that only sets the exit code.

func NewError

func NewError(code ExitCode, message, hint string) *Error

NewError returns an error with the given exit code.

func Usage

func Usage(message, hint string) *Error

Usage returns an error for bad usage or input.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ExitCode

type ExitCode int

ExitCode is the process exit code. The values are part of the public CLI contract.

const (
	ExitOK           ExitCode = 0
	ExitError        ExitCode = 1 // generic error
	ExitUsage        ExitCode = 2 // bad usage or input
	ExitPrerequisite ExitCode = 3 // prerequisite missing
	ExitUnhealthy    ExitCode = 4 // service not healthy
	ExitRemote       ExitCode = 5 // remote API error
	ExitAuth         ExitCode = 6 // authentication failed
)

func CodeOf

func CodeOf(err error) ExitCode

CodeOf returns the exit code for err. Errors that are not *Error map to ExitError.

func (ExitCode) Name

func (c ExitCode) Name() string

Name returns the stable name of the exit code.

type ExitCodeInfo added in v0.3.1

type ExitCodeInfo struct {
	Code    int    `json:"code"`
	Name    string `json:"name"`
	Meaning string `json:"meaning"`
}

ExitCodeInfo describes one exit code.

func ExitCodes added in v0.3.1

func ExitCodes() []ExitCodeInfo

ExitCodes lists every exit code, in order.

type Format

type Format string

Format is an output format.

const (
	Text Format = "text"
	JSON Format = "json"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat validates s as an output format.

type Printer

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

Printer writes results and errors in one format.

func NewPrinter

func NewPrinter(format Format, out, errw io.Writer) *Printer

NewPrinter returns a printer that writes results to out and text errors to errw.

func (*Printer) Error

func (p *Printer) Error(err error)

Error writes err. Write failures are ignored because there is nowhere left to report them.

func (*Printer) Format

func (p *Printer) Format() Format

Format returns the printer format.

func (*Printer) Print

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

Print writes a command result.

func (*Printer) Warn

func (p *Printer) Warn(msg string)

Warn writes a warning to stderr in every format, so JSON stdout is not affected.

type Texter

type Texter interface {
	WriteText(w io.Writer) error
}

Texter is implemented by results that have a human-readable form.

Jump to

Keyboard shortcuts

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