output

package
v4.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Formats is the list of supported output formats.
	Formats = []string{"json", "yaml", "csv", "long", "short"}
	// ErrZeroValueOf is returned if when taking the reflect ValueOf an item we get a zero Value.
	ErrZeroValueOf = errors.New("ValueOf is zero")
	// ErrNotAStruct is returned when we expect a struct, but find something else.
	ErrNotAStruct = errors.New("not a struct")
	// ErrFieldNotFound is returned if we cannot find the requested field on a reflect.Value.
	ErrFieldNotFound = errors.New("field not found")
	// ErrPanic is returned if an unhandled panic occurs - this should not happen.
	ErrPanic = errors.New("unhandled panic")
	// ErrShortExtra is returned if not enough values are given for an extra field.
	ErrShortExtra = errors.New("not enough values for extra field")
)

Functions

func DescribeStruct

func DescribeStruct(header string, s any) (out string, err error)

DescribeStruct returns a human readable representation of an arbitrary struct. A provided header is prepended, as a title to the output.

func Format

func Format(format string, data []any, extra *map[string][]string, spec FormatSpec) (string, error)

Format returns a string containing output in the requested format. data is a generic interface{} representation of the data retrieved from the API. extra is a map of computed values that can be used to add an extra field to TSV/CSV output. spec specifies fields to display, and transformations to apply on fields. Note: json and yaml output are raw dumps from the API, ignoring extra and spec.

func HumanByteTransform

func HumanByteTransform(s string, _ *api.Clients) string

HumanByteTransform will format a number of bytes into K/M/G/TiB.

func TimezoneTransform

func TimezoneTransform(s string, _ *api.Clients) string

TimezoneTransform will reformat a date/time into the current timezone.

func Truncate50Transform

func Truncate50Transform(s string, _ *api.Clients) string

Truncate50Transform will truncate a string to 50 characters.

func UpperCaseTransform

func UpperCaseTransform(in string, _ *api.Clients) string

UpperCaseTransform will transform a string to upper-case.

func UsernameTransform

func UsernameTransform(id string, clients *api.Clients) string

UsernameTransform will transform a bson user ID to a username for display.

Types

type FormatSpec

type FormatSpec struct {
	// ShortFields indicates which fields are to be included in a short output format.
	ShortFields []string
	// ShortHeaders provides header text per entry in ShortFields.
	ShortHeaders []string
	// LongFields indicates which fields are to be included in a long output format.
	LongFields []string
	// LongFields provides header text per entry in LongFields.
	LongHeaders []string
	// Transforms can be used to specifies an optional function per field that will transform output values.
	// Only applies to CSV and TSV output. JSON & YAML output is raw, and is not transformed.
	Transforms map[string]TransformFunc
	// Clients provides service clients for transformations that may need API access.
	Clients *api.Clients
}

FormatSpec defines the way in which output will be displayed.

type PrefixWriter

type PrefixWriter interface {
	// Write writes text with the specified indentation level.
	Writef(level int, format string, a ...any)
	// WriteLine writes an entire line with no indentation level.
	WriteLine(a ...any)
	// Flush forces indentation to be reset.
	Flush()
}

PrefixWriter can write text at various indentation levels.

func NewNestedPrefixWriter

func NewNestedPrefixWriter(out PrefixWriter, indent int) PrefixWriter

NewPrefixWriter creates a new PrefixWriter.

func NewPrefixWriter

func NewPrefixWriter(out io.Writer) PrefixWriter

NewPrefixWriter creates a new PrefixWriter.

type TransformFunc

type TransformFunc func(in string, clients *api.Clients) string

TransformFunc are used to convert a string into a different representation for display. Service clients are provided so transformations can be performed that involve API calls.

type TransformMap

type TransformMap map[string]TransformFunc

TransformMap is a map from field name to a TransformFunc.

Jump to

Keyboard shortcuts

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