printers

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsShowable

func IsShowable(value any) bool

Types

type FieldRenderOptions

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

type FieldValue

type FieldValue struct {
	Name  string
	Value any
	// contains filtered or unexported fields
}

func NewFieldValue

func NewFieldValue(name string, value any, opts ...FieldValueOption) FieldValue

func (FieldValue) ValueString

func (f FieldValue) ValueString() string

type FieldValueOption

type FieldValueOption func(*FieldValue)

func WithListKey

func WithListKey() FieldValueOption

WithListKey sets this field as the key when the item is rendered as a list

func WithListKeyRender

func WithListKeyRender(render RenderFunc) FieldValueOption

WithListKeyRender sets this field as the key when the item is rendered as a list and provides a function to render the key

func WithRenderValueFunc

func WithRenderValueFunc(render RenderFunc) FieldValueOption

type JsonPrinter

type JsonPrinter[T any] struct {
	Sanitizer *sanitize.Sanitizer
}

func NewJsonPrinter

func NewJsonPrinter[T any]() (*JsonPrinter[T], error)

func (JsonPrinter[T]) PrintResource

func (p JsonPrinter[T]) PrintResource(ctx context.Context, r PrintableResource[T], writer io.Writer) error

type Listable

type Listable interface {
	GetListData() *RowData
}

Listable is an interface implemented by objects which support the `list` command for pretty/plain output format

type PrintableResource

type PrintableResource[T any] interface {
	GetItems() []T
	GetTable() (*Table, error)
}

type Printer

type Printer int

type RenderFunc

type RenderFunc func(opts sanitize.RenderOptions) string

type ResourcePrinter

type ResourcePrinter[T any] interface {
	// PrintResource receives a runtime object, formats it and prints it to a writer.
	PrintResource(context.Context, PrintableResource[T], io.Writer) error
}

ResourcePrinter is an interface that knows how to print runtime objects.

func GetPrinter

func GetPrinter[T any](cmd *cobra.Command) (ResourcePrinter[T], error)

type RowData

type RowData struct {
	// slice of column names (converted to lower case)
	Columns []string

	// map of fields keyed by lower case column name
	Fields map[string]FieldValue
	// contains filtered or unexported fields
}

RowData is a struct that holds the data for a single row of a table

func NewRowData

func NewRowData(fields ...FieldValue) *RowData

func (*RowData) AddField

func (d *RowData) AddField(f FieldValue)

func (*RowData) GetDisplayColumns

func (d *RowData) GetDisplayColumns() []string

func (*RowData) GetRow

func (d *RowData) GetRow() *TableRow

func (*RowData) Merge

func (d *RowData) Merge(other *RowData)

Merge merges the other RowData columns from other are placed first, however our fields take precedence this is to ensure when merging base showdata the base columns come first, but derived typeds can override values

type ShowPrinter

type ShowPrinter[T any] struct{}

func NewShowPrinter

func NewShowPrinter[T any]() (*ShowPrinter[T], error)

func (ShowPrinter[T]) PrintResource

func (p ShowPrinter[T]) PrintResource(_ context.Context, r PrintableResource[T], writer io.Writer) error

type Showable

type Showable interface {
	GetShowData() *RowData
}

Showable is an interface implemented by objects which support the `show` command for pretty/plain output format

func AsShowable

func AsShowable(value any) Showable

type StringPrinter

type StringPrinter[T any] struct {
	Sanitizer *sanitize.Sanitizer
	// contains filtered or unexported fields
}

func NewStringPrinter

func NewStringPrinter[T any]() (*StringPrinter[T], error)

func (StringPrinter[T]) PrintResource

func (p StringPrinter[T]) PrintResource(_ context.Context, r PrintableResource[T], writer io.Writer) error

type Table

type Table struct {
	Rows      []TableRow
	Columns   []string
	FieldOpts map[string]FieldRenderOptions
}

func NewTable

func NewTable() *Table

func (*Table) AddRow

func (t *Table) AddRow(row *TableRow)

func (*Table) WithData

func (t *Table) WithData(tableRows []TableRow, columns []string) *Table

func (*Table) WithRow

func (t *Table) WithRow(fields ...FieldValue) *Table

type TablePrinter

type TablePrinter[T any] struct {
	Sanitizer *sanitize.Sanitizer
}

TablePrinter decodes table objects into typed objects before delegating to another printer. Non-table types are simply passed through

func NewTablePrinter

func NewTablePrinter[T any]() (*TablePrinter[T], error)

func (TablePrinter[T]) PrintResource

func (p TablePrinter[T]) PrintResource(_ context.Context, items PrintableResource[T], writer io.Writer) error

func (TablePrinter[T]) PrintTable

func (p TablePrinter[T]) PrintTable(table *Table, writer io.Writer) error

type TableRow

type TableRow struct {
	Columns []string
	Cells   []any
	// map of field name to field render options
	Opts map[string]FieldRenderOptions
}

func NewTableRow

func NewTableRow(fields ...FieldValue) *TableRow

func (*TableRow) Merge

func (r *TableRow) Merge(other *TableRow)

Merge merges the other row, adding its columns BEFORE ours

type YamlPrinter

type YamlPrinter[T any] struct {
	Sanitizer *sanitize.Sanitizer
}

Inspired by https://github.com/goccy/go-yaml/blob/master/cmd/ycat/ycat.go

func NewYamlPrinter

func NewYamlPrinter[T any]() (*YamlPrinter[T], error)

func (YamlPrinter[T]) PrintResource

func (px YamlPrinter[T]) PrintResource(ctx context.Context, r PrintableResource[T], writer io.Writer) error

Jump to

Keyboard shortcuts

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