format

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterOutputFlags added in v1.35.0

func RegisterOutputFlags(cmd *cobra.Command)

func WriteTemplate added in v1.35.0

func WriteTemplate(w io.Writer, tmpl string, v any) error

WriteTemplate parses tmpl as a Go text/template (with the gh-compatible FuncMap) and executes it against v, writing the result to w.

func WriteYAML added in v1.35.0

func WriteYAML(w io.Writer, v any) error

WriteYAML marshals v to YAML and writes it to w.

Types

type Field

type Field[T any] struct {
	Name    string
	Header  string
	Extract func(T) any
	// Aliases are alternative names accepted by --json; they do not appear in
	// the valid-fields error list so the canonical name is always preferred.
	Aliases []string
	// JSONOnly marks a field that is available via --json but omitted from the
	// default table output (e.g. UUID, webURL columns that clutter the TTY view).
	JSONOnly bool
	// ColorFunc, if set, is applied to the rendered string in TTY table mode
	// only. It is intentionally NOT applied to JSON output, where consumers
	// expect raw values, nor in non-TTY table mode (piped output).
	ColorFunc func(string) string
}

Field describes one output column and JSON key for type T.

type OutputConfig added in v1.35.0

type OutputConfig struct {
	Format     OutputFormat
	JQExpr     string
	Template   string
	JSONFields []string // nil = all fields; non-nil = only these fields
}

OutputConfig captures the user-facing output-format selection — exactly one Format is active per invocation. JQExpr is only meaningful when Format == FormatJSON; Template only when Format == FormatTemplate. JSONFields, when non-nil, restricts JSON output to only the named fields; nil means all fields (backward compatible).

func ConfigFromCmd added in v1.35.0

func ConfigFromCmd(cmd *cobra.Command) OutputConfig

ConfigFromCmd reads the four output-format persistent flags from any cobra.Command. Works from any subcommand because Cobra merges persistent flags from ancestors. Falls back to FormatTable when the flags are not registered (e.g. a test calling a subcommand without a parent).

type OutputFormat added in v1.35.0

type OutputFormat string

OutputFormat enumerates supported output modes.

const (
	// FormatTable is the default human-readable table output.
	FormatTable OutputFormat = ""
	// FormatJSON emits JSON, optionally piped through jq.
	FormatJSON OutputFormat = "json"
	// FormatYAML emits YAML.
	FormatYAML OutputFormat = "yaml"
	// FormatTemplate renders results through a Go text/template.
	FormatTemplate OutputFormat = "template"
)

type Printer

type Printer[T any] struct {
	// contains filtered or unexported fields
}

Printer renders a slice of T in the correct output mode.

func New

func New[T any](w io.Writer, isTTY bool, cfg OutputConfig) *Printer[T]

New constructs a Printer.

func (*Printer[T]) AddField

func (p *Printer[T]) AddField(f Field[T])

AddField registers a field.

func (*Printer[T]) AddItem

func (p *Printer[T]) AddItem(item T)

AddItem enqueues one result item.

func (*Printer[T]) Render

func (p *Printer[T]) Render() error

Render writes all items in the appropriate format.

func (*Printer[T]) SetSingleItem

func (p *Printer[T]) SetSingleItem()

SetSingleItem marks the printer to emit a single object (JSON/YAML/template) instead of an array.

func (*Printer[T]) ValidateJSONFields added in v1.86.0

func (p *Printer[T]) ValidateJSONFields() error

ValidateJSONFields returns an error if any requested field is unknown. Only called when cfg.JSONFields is non-nil (i.e. --json field1,field2 was used).

Jump to

Keyboard shortcuts

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