output

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package output provides text and JSON formatters for CLI tools.

Index

Constants

View Source
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.

View Source
const (
	ModeText = "text"
	ModeJSON = "json"
)

Mode constants for output formatting.

Variables

View Source
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 New

func New(mode string) *Output

New creates a new Output with the given mode and default writers.

func NewWithWriters

func NewWithWriters(mode string, w, errW io.Writer) *Output

NewWithWriters creates a new Output with custom writers.

func (*Output) ExitWithError

func (o *Output) ExitWithError(err error, code int)

ExitWithError prints the error and exits with the given code.

func (*Output) PrintError

func (o *Output) PrintError(err error, code int) error

PrintError prints an error message in the configured mode.

func (*Output) PrintErrorJSON

func (o *Output) PrintErrorJSON(msg string, code int) error

PrintErrorJSON writes { "error": msg, "code": N }.

func (*Output) PrintErrorText

func (o *Output) PrintErrorText(err error, code int) error

PrintErrorText writes an error message to the error writer.

func (*Output) PrintList

func (o *Output) PrintList(items []any) error

PrintList prints a list of items in the configured mode.

func (*Output) PrintListJSON

func (o *Output) PrintListJSON(items []any) error

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

func (o *Output) PrintListText(items []any) error

PrintListText writes items as a simple human-readable list.

func (*Output) PrintObject

func (o *Output) PrintObject(obj any) error

PrintObject prints a single object in the configured mode.

func (*Output) PrintObjectJSON

func (o *Output) PrintObjectJSON(obj any) error

PrintObjectJSON writes a single object directly without wrapping.

func (*Output) PrintObjectText

func (o *Output) PrintObjectText(obj any) error

PrintObjectText writes a single object as human-readable text.

Jump to

Keyboard shortcuts

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