output

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TimestampDisplay is for human-readable output in terminal.
	TimestampDisplay = "2006-01-02 15:04:05"

	// TimestampISO is for JSON/machine output (RFC3339).
	TimestampISO = time.RFC3339
)

Timestamp format constants for consistent display across commands.

Variables

This section is empty.

Functions

func FormatTime

func FormatTime(t *time.Time, format string) string

FormatTime formats a time pointer for display. Returns "-" if the time is nil or zero.

func JSONOutput

func JSONOutput(w io.Writer, data any) error

JSONOutput writes data as indented JSON to the writer.

func List

func List[T any](p *Printer, headers []string, items []T, rowFunc func(T) []string) error

List prints items as either a table or structured (JSON/YAML) based on the Printer's format. Items should be the output type with json/yaml tags. rowFunc extracts table columns.

func YAMLOutput

func YAMLOutput(w io.Writer, data any) error

YAMLOutput writes data as YAML to the writer.

Types

type CommandOutput

type CommandOutput interface {
	OutOrStdout() io.Writer
	ErrOrStderr() io.Writer
}

CommandOutput is the interface for cobra.Command output methods.

type Format

type Format string

Format specifies the output format.

const (
	FormatText Format = "text"
	FormatJSON Format = "json"
	FormatYAML Format = "yaml"
)

func ParseFormat

func ParseFormat(s string) Format

ParseFormat converts a string to Format, defaulting to FormatText.

type Printer

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

Printer provides consistent output formatting for CLI commands.

func NewPrinter

func NewPrinter(cmd CommandOutput) *Printer

NewPrinter creates a Printer from a cobra.Command (or any CommandOutput).

func (*Printer) Errorf

func (p *Printer) Errorf(format string, args ...any)

Errorf prints a formatted message to stderr.

func (*Printer) IsStructured

func (p *Printer) IsStructured() bool

IsStructured returns true if the printer is in JSON or YAML mode.

func (*Printer) NewTable

func (p *Printer) NewTable(headers ...string) *Table

NewTable creates a new table with the given headers.

func (*Printer) Print

func (p *Printer) Print(msg string)

Print prints a message to stdout.

func (*Printer) Printf

func (p *Printer) Printf(format string, args ...any)

Printf prints a formatted message to stdout.

func (*Printer) Structured

func (p *Printer) Structured(data any) error

Structured writes data as JSON or YAML based on the printer's format.

func (*Printer) WithFormat

func (p *Printer) WithFormat(f Format) *Printer

WithFormat returns a new Printer with the specified format.

type Table

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

Table provides tabwriter-based table formatting.

func (*Table) Flush

func (t *Table) Flush() error

Flush writes the table to the output.

func (*Table) Row

func (t *Table) Row(values ...string)

Row adds a row to the table.

Jump to

Keyboard shortcuts

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