output

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package output renders categorized toolkit data to stdout in machine-friendly formats (json / jsonl / yaml / csv / tsv) or a human table.

It is intentionally TUI-free: it depends only on stdlib + yaml so the headless `toolkit get` command can be used in scripts and from LLM agents without paying the Bubble Tea cost.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten added in v0.4.0

func Flatten[T any](grouped map[string][]T) []T

Flatten concatenates a grouped map[string][]T into a flat []T with deterministic key ordering. Use when the group key is already preserved on each value (so injecting it again would just duplicate — see GpuNode.NodePool / ModelArtifact.ModelName). The returned slice preserves T's full type so the caller can keep using struct tags / custom JSON marshaling without the FlattenWithKey JSON round-trip.

func FlattenWithKey

func FlattenWithKey[T any](grouped map[string][]T, groupField string) []map[string]any

FlattenWithKey turns a map[string][]T into []map[string]any, injecting the map key into each element under groupField. Used to expose grouped loader data (e.g. GpuNodeMap, DedicatedAIClusterMap) as a uniform array of objects without leaking the underlying map shape to MCP / JSON consumers.

The output is stable: map keys are sorted before iteration. The implementation round-trips through JSON so the caller's struct tags (omitempty, custom names, etc.) are honored.

Collision rule: if T's JSON encoding already contains a field whose name equals groupField, the map key wins — the existing value is silently overwritten. Callers should choose a groupField that doesn't collide with any of T's tagged fields. The current production callers pick "pool" / "tenant" / "model", none of which clash with the underlying pkg/models types. Test coverage pins this behavior (TestFlattenWithKey_CollisionOverwrites).

func WriteDelimited

func WriteDelimited(w io.Writer, headers []string, rows [][]string, opts Options, sep rune) error

WriteDelimited emits headers + rows as delimiter-separated values using encoding/csv, which handles quoting for fields containing the separator, double quotes, or newlines. Pass ',' for CSV or '\t' for TSV. opts.NoHeaders suppresses the header row.

func WriteJSON

func WriteJSON(w io.Writer, items any, opts Options) error

WriteJSON emits items as a single JSON value (typically an array). A nil items value emits "[]" so pipelines like `| jq '.[]'` never see a null document.

func WriteJSONL

func WriteJSONL(w io.Writer, items any, _ Options) error

WriteJSONL emits one JSON object per line. items must be a slice (each element becomes a line) or any single JSON-encodable value (emitted as one line).

Grouped/map data should be flattened with Flatten (when the group key is already a field on the value) or FlattenWithKey (when the caller needs to inject the map key) before reaching this function.

func WriteTable

func WriteTable(w io.Writer, headers []string, rows [][]string, opts Options) error

WriteTable emits a tab-aligned table. opts.NoHeaders suppresses the header row even when headers is non-empty.

func WriteYAML

func WriteYAML(w io.Writer, items any, opts Options) error

WriteYAML emits items as a YAML document.

Types

type Format

type Format string

Format is the on-the-wire encoding for `toolkit get`.

const (
	FormatTable Format = "table"
	FormatJSON  Format = "json"
	FormatJSONL Format = "jsonl"
	FormatYAML  Format = "yaml"
	FormatCSV   Format = "csv"
	FormatTSV   Format = "tsv"
)

Supported output formats for `toolkit get` and consumers that share the same encoding contract.

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat returns the Format for s, or an error listing valid choices.

type Options

type Options struct {
	Format    Format
	NoHeaders bool // table/csv/tsv: omit header row
	Pretty    bool // json/yaml: pretty-print
}

Options controls how renderers emit data.

Jump to

Keyboard shortcuts

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