Documentation
¶
Overview ¶
Package output provides text and JSON formatters for CLI tools.
Index ¶
- Constants
- Variables
- type Output
- func (o *Output) ExitWithError(err error, code int)
- func (o *Output) PrintError(err error, code int) error
- func (o *Output) PrintErrorJSON(msg string, code int) error
- func (o *Output) PrintErrorText(err error, code int) error
- func (o *Output) PrintList(items []any) error
- func (o *Output) PrintListJSON(items []any) error
- func (o *Output) PrintListText(items []any) error
- func (o *Output) PrintObject(obj any) error
- func (o *Output) PrintObjectJSON(obj any) error
- func (o *Output) PrintObjectText(obj any) error
Constants ¶
const ( // ExitSuccess indicates successful operation. ExitSuccess = 0 // ExitClientError indicates a client-side error (HTTP 4xx, invalid arguments). ExitClientError = 1 // ExitServerError indicates a server-side error (HTTP 5xx, internal errors). ExitServerError = 2 // ExitNetwork indicates a network error (DNS, connection refused, timeout). ExitNetwork = 3 // ExitConfig indicates a configuration error (missing config, invalid YAML). ExitConfig = 4 )
Exit codes for CLI tools.
const ( ModeText = "text" ModeJSON = "json" )
Mode constants for output formatting.
Variables ¶
var ExitCodeNames = map[int]string{ ExitSuccess: "success", ExitClientError: "client error", ExitServerError: "server error", ExitNetwork: "network error", ExitConfig: "config error", }
ExitCodeNames maps exit codes to human-readable names.
Functions ¶
This section is empty.
Types ¶
type Output ¶
type Output struct {
// Mode is the output mode: "text" or "json".
Mode string
// W is the writer for normal output (default: os.Stdout).
W io.Writer
// ErrW is the writer for error output (default: os.Stderr).
ErrW io.Writer
}
Output handles formatted output for CLI tools.
func NewWithWriters ¶
NewWithWriters creates a new Output with custom writers.
func (*Output) ExitWithError ¶
ExitWithError prints the error and exits with the given code.
func (*Output) PrintError ¶
PrintError prints an error message in the configured mode.
func (*Output) PrintErrorJSON ¶
PrintErrorJSON writes { "error": msg, "code": N }.
func (*Output) PrintErrorText ¶
PrintErrorText writes an error message to the error writer.
func (*Output) PrintListJSON ¶
PrintListJSON writes items wrapped in { "items": [...], "count": N }. Stable field order: count first, items second. Empty lists return {"count":0,"items":[]} not null.
func (*Output) PrintListText ¶
PrintListText writes items as a simple human-readable list.
func (*Output) PrintObject ¶
PrintObject prints a single object in the configured mode.
func (*Output) PrintObjectJSON ¶
PrintObjectJSON writes a single object directly without wrapping.
func (*Output) PrintObjectText ¶
PrintObjectText writes a single object as human-readable text.