output

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package output provides JSON/Markdown output formatting and error handling.

Index

Constants

View Source
const (
	ExitOK        = 0 // Success
	ExitUsage     = 1 // Invalid arguments or flags
	ExitNotFound  = 2 // Resource not found
	ExitAuth      = 3 // Not authenticated
	ExitForbidden = 4 // Access denied (scope issue)
	ExitRateLimit = 5 // Rate limited (429)
	ExitNetwork   = 6 // Connection/DNS/timeout error
	ExitAPI       = 7 // Server returned error
	ExitAmbiguous = 8 // Multiple matches for name
)

Exit codes matching the Bash implementation.

View Source
const (
	CodeUsage     = "usage"
	CodeNotFound  = "not_found"
	CodeAuth      = "auth_required"
	CodeForbidden = "forbidden"
	CodeRateLimit = "rate_limit"
	CodeNetwork   = "network"
	CodeAPI       = "api_error"
	CodeAmbiguous = "ambiguous"
)

Error codes for JSON envelope.

Variables

This section is empty.

Functions

func ExitCodeFor

func ExitCodeFor(code string) int

ExitCodeFor returns the exit code for a given error code.

func NormalizeData

func NormalizeData(data any) any

NormalizeData converts json.RawMessage and other types to standard Go types.

func TruncationNotice

func TruncationNotice(count, defaultLimit int, all bool, explicitLimit int) string

TruncationNotice returns a notice string if results may be truncated. Returns empty string if no truncation warning is needed.

func TruncationNoticeWithTotal

func TruncationNoticeWithTotal(count, totalCount int) string

TruncationNoticeWithTotal returns a truncation notice when results are truncated. Uses totalCount from API's X-Total-Count header to show accurate counts. Returns empty string if no truncation or totalCount is 0 (unavailable).

Types

type Breadcrumb struct {
	Action      string `json:"action"`
	Cmd         string `json:"cmd"`
	Description string `json:"description"`
}

Breadcrumb is a suggested follow-up action.

type Error

type Error struct {
	Code       string
	Message    string
	Hint       string
	HTTPStatus int
	Retryable  bool
	Cause      error
}

Error is a structured error with code, message, and optional hint.

func AsError

func AsError(err error) *Error

AsError attempts to convert an error to an *Error.

func ErrAPI

func ErrAPI(status int, msg string) *Error

func ErrAmbiguous

func ErrAmbiguous(resource string, matches []string) *Error

func ErrAuth

func ErrAuth(msg string) *Error

func ErrForbidden

func ErrForbidden(msg string) *Error

func ErrForbiddenScope

func ErrForbiddenScope() *Error

func ErrNetwork

func ErrNetwork(cause error) *Error

func ErrNotFound

func ErrNotFound(resource, identifier string) *Error

func ErrNotFoundHint

func ErrNotFoundHint(resource, identifier, hint string) *Error

func ErrRateLimit

func ErrRateLimit(retryAfter int) *Error

func ErrUsage

func ErrUsage(msg string) *Error

func ErrUsageHint

func ErrUsageHint(msg, hint string) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) ExitCode

func (e *Error) ExitCode() int

ExitCode returns the appropriate exit code for this error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ErrorResponse

type ErrorResponse struct {
	OK    bool           `json:"ok"`
	Error string         `json:"error"`
	Code  string         `json:"code"`
	Hint  string         `json:"hint,omitempty"`
	Meta  map[string]any `json:"meta,omitempty"`
}

ErrorResponse is the error envelope for JSON output.

type ErrorResponseOption

type ErrorResponseOption func(*ErrorResponse)

ErrorResponseOption modifies an ErrorResponse.

type Format

type Format int

Format specifies the output format.

const (
	FormatAuto     Format = iota // Auto-detect: TTY -> Styled, non-TTY -> JSON
	FormatJSON                   // JSON envelope
	FormatMarkdown               // Literal Markdown syntax
	FormatStyled                 // ANSI styled output
	FormatQuiet                  // Raw JSON data, no envelope
	FormatIDs                    // One ID per line
	FormatCount                  // Integer count only
)

type Options

type Options struct {
	Format  Format
	Writer  io.Writer
	Verbose bool
}

Options controls output behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns options for standard output.

type Response

type Response struct {
	OK          bool           `json:"ok"`
	Data        any            `json:"data,omitempty"`
	Summary     string         `json:"summary,omitempty"`
	Notice      string         `json:"notice,omitempty"`
	Breadcrumbs []Breadcrumb   `json:"breadcrumbs,omitempty"`
	Context     map[string]any `json:"context,omitempty"`
	Meta        map[string]any `json:"meta,omitempty"`
}

Response is the success envelope for JSON output.

type ResponseOption

type ResponseOption func(*Response)

ResponseOption modifies a Response.

func WithBreadcrumbs

func WithBreadcrumbs(b ...Breadcrumb) ResponseOption

WithBreadcrumbs adds breadcrumbs to the response.

func WithContext

func WithContext(key string, value any) ResponseOption

WithContext adds context to the response.

func WithMeta

func WithMeta(key string, value any) ResponseOption

WithMeta adds metadata to the response.

func WithNotice

func WithNotice(s string) ResponseOption

WithNotice adds an informational notice to the response.

func WithSummary

func WithSummary(s string) ResponseOption

WithSummary adds a summary to the response.

func WithoutBreadcrumbs

func WithoutBreadcrumbs() ResponseOption

WithoutBreadcrumbs removes all breadcrumbs from the response.

type Writer

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

Writer handles all output formatting.

func New

func New(opts Options) *Writer

New creates a new output writer.

func (*Writer) EffectiveFormat

func (w *Writer) EffectiveFormat() Format

EffectiveFormat resolves FormatAuto based on TTY detection.

func (*Writer) Err

func (w *Writer) Err(err error, opts ...ErrorResponseOption) error

Err outputs an error response.

func (*Writer) OK

func (w *Writer) OK(data any, opts ...ResponseOption) error

OK outputs a success response.

Jump to

Keyboard shortcuts

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