output

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package output implements szwm's stdout contract.

In json mode stdout carries exactly one JSON document per run, success or failure. In ndjson mode each item is one line and the last line is a summary. A failure also prints a one-line message to stderr, so it stays visible when stdout is piped into another tool.

Index

Constants

View Source
const SchemaVersion = "1"

SchemaVersion versions the envelope, error codes and exit codes.

Variables

This section is empty.

Functions

func Flatten

func Flatten(m map[string]any) map[string]any

Flatten turns nested objects into dotted keys. Arrays stay as values.

func Normalize

func Normalize(v any) any

Normalize converts any value to generic JSON types, keeping exact integers.

func Select

func Select(v any, paths []string) any

Select keeps only the dotted paths in v. Arrays are mapped over, so items.url picks url from every element. Missing paths are simply absent.

Types

type Envelope

type Envelope struct {
	SchemaVersion string        `json:"schema_version"`
	OK            bool          `json:"ok"`
	Command       string        `json:"command"`
	Data          any           `json:"data,omitempty"`
	Error         *apperr.Error `json:"error,omitempty"`
	Meta          Meta          `json:"meta"`
}

Envelope wraps every JSON result.

type Format

type Format string

Format is an output format.

const (
	JSON   Format = "json"
	NDJSON Format = "ndjson"
	Table  Format = "table"
	CSV    Format = "csv"
)

Output formats.

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat validates a format name.

type Meta

type Meta struct {
	Version         string   `json:"szwm_version"`
	Profile         string   `json:"profile,omitempty"`
	Site            string   `json:"site,omitempty"`
	DurationMS      int64    `json:"duration_ms"`
	Requests        int64    `json:"requests"`
	Retries         int64    `json:"retries"`
	Truncated       bool     `json:"truncated,omitempty"`
	Hints           []string `json:"hints,omitempty"`
	Warnings        []string `json:"warnings,omitempty"`
	UntrustedFields []string `json:"untrusted_fields,omitempty"`
}

Meta describes how a result was produced.

type View

type View struct {
	Rows     []map[string]any
	Columns  []string
	Raw      []byte
	Streamed bool
}

View carries the tabular form of a result next to its envelope.

type Writer

type Writer struct {
	Format Format
	Pretty bool
	Fields []string
	Stdout io.Writer
	Stderr io.Writer
}

Writer renders results.

func (*Writer) Final

func (w *Writer) Final(env Envelope, v View)

Final writes the result and returns nothing: callers derive the exit code from env.Error.

func (*Writer) Item

func (w *Writer) Item(item any)

Item writes one ndjson line immediately, so a long batch shows progress and survives being killed. In other formats it does nothing.

Jump to

Keyboard shortcuts

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