Documentation
¶
Overview ¶
Package printers is a helper for formatting and printing various objects into a primitive io.Writer interface.
- Heavily inspired by kubernetes's cli-runtime library, specifically the printers package and associated flags in the genericclioptions package.
Index ¶
- Constants
- Variables
- func GenerateTableData(data any) (headers []string, rows [][]string, _ error)
- func IsNoCompatiblePrinterError(err error) bool
- type CSVPrinter
- type FlaggablePrinter
- type JSONPrinter
- type NoCompatiblePrinterError
- type ObjectPrinter
- type ObjectPrinterFunc
- type PrintFlags
- type PrintOptions
- type TableCSVPrinterFlags
- type TablePrinter
- type TableReflectorFunc
- type YamlJSONPrinterFlags
- type YamlPrinter
Constants ¶
const (
YAMLIndentLevel = 4
)
Variables ¶
var ( DefaultTableHeaderStyle = lipgloss.NewStyle(). Foreground(purple). Bold(true). Align(lipgloss.Center) DefaultTableCellWidth = 14 DefaultTableCellStyle = lipgloss.NewStyle(). Padding(0, 1). Width(DefaultTableCellWidth). Align(lipgloss.Center). Foreground(lightGray) DefaultTableBorderType = lipgloss.NormalBorder() DefaultTableBorderStyle = lipgloss.NewStyle().Foreground(blue) DefaultTableCustomizeFunc = func(t *table.Table) *table.Table { return t.Border(DefaultTableBorderType).BorderStyle(DefaultTableBorderStyle) } )
Functions ¶
func GenerateTableData ¶
GenerateTableData is a TableReflectorFunc that will use reflection to resolve tabular data from a struct tor slice/array of structs. Every field in the input struct represents a column on the table.
Defining Headers ¶
Input struct(s) should use field tags to define table headers at compile-time.
Note: json field tags are parsed from various snake-case/camel-case/title-case/etc. to normalized all-caps (shouting) strings that isolate the words for easier reading and viewing. Header tags are not subject to this normalization, and can be considered overrides of this behavior.
There are 3 main ways headers are resolved:
1. header field tags
MyField `header:"MY FIELD"` -> "MY FIELD" MyField `header:"something completely different!"` -> "something completely different!"
2. json field tags
MyField `json:"my_field"` -> "MY FIELD" MyField `json:"myField"` -> "MY FIELD"
3. The field name itself
MyField -> "MY FIELD"
Complex data structures or nested/embedded structs:
type MyStruct struct {
Field1 string `header:"Field 1"`
Field2 int `header:"Field 2"`
}
type MyOtherStruct struct {
MyStruct
Field3 bool `header:"Field 3"`
}
myData := &MyOtherStruct{
MyStruct: MyStruct{
Field1: "value1",
Field2: 42,
},
Field3: true,
}
headers, rows, err := GenerateTableData(myData)
// headers == []string{"Field 1", "Field 2", "Field 3"}
// rows == [][]string{{"value1", "42", "true"}}
// example csv output:
// Field 1,Field 2,Field 3
// value1,42,true
func IsNoCompatiblePrinterError ¶
IsNoCompatiblePrinterError returns true if the error is a NoCompatiblePrinterError.
Types ¶
type CSVPrinter ¶
type CSVPrinter struct {
PrintOptions
TableReflectorFunc
}
type FlaggablePrinter ¶
type FlaggablePrinter interface {
// AllowedFormats should return a list of format options that are supported by ToPrinte
AllowedFormats() []string
// ToPrinter should return a ObjectPrinter for a given output format string.
// If the output format string is not valid, or cannot produce a ObjectPrinter,
// a NoCompatiblePrinterError must be returned.
ToPrinter(format string) (ObjectPrinter, error)
// AddFlags should call the given cobra.Command's Flags() method to add new flags to its
// pflag.FlagSet that may be specific to the available printer implementations.
AddFlags(cmd *cobra.Command)
}
FlaggablePrinter is used to provision a new group of printer implementations that may add custom flags in AddFlags and produce a ObjectPrinter via ToPrinter based on the allowed formats returned by AllowedFormats.
type JSONPrinter ¶
type JSONPrinter struct {
Indent bool
}
type NoCompatiblePrinterError ¶
func (NoCompatiblePrinterError) Error ¶
func (n NoCompatiblePrinterError) Error() string
type ObjectPrinter ¶
type ObjectPrinter interface {
// PrintObj prints the provided object to the provided [io.Writer], according to the printer's
// configuration.
//
// See [printers] for more information.
PrintObj(obj any, w io.Writer) error
}
ObjectPrinter is an interface that knows how to print objects.
func NewCSVPrinter ¶
func NewCSVPrinter(options PrintOptions) ObjectPrinter
func NewJSONPrinter ¶
func NewJSONPrinter(indent bool) ObjectPrinter
func NewTablePrinter ¶
func NewTablePrinter(options PrintOptions) ObjectPrinter
func NewYAMLPrinter ¶
func NewYAMLPrinter() ObjectPrinter
type ObjectPrinterFunc ¶
ObjectPrinterFunc is a function that can print objects.
type PrintFlags ¶
type PrintFlags struct {
RegisteredPrintFlaggers []FlaggablePrinter
OutputFormat *string
// OutputFlagSpecified indicates whether the user specifically requested a certain kind of
// output. using this function allows a sophisticated caller to change the flag binding logic
// if they so desire.
OutputFlagSpecified func() bool
}
PrintFlags composes common printer types used across all commands, and provides a method of retreiving a known printer based on the flag values provided.
func NewPrintFlags ¶
func NewPrintFlags() *PrintFlags
func (*PrintFlags) AddFlags ¶
func (f *PrintFlags) AddFlags(cmd *cobra.Command)
AddFlags takes a *cobra.Command by reference and binds flags related to printing to the command.
func (*PrintFlags) AllowedFormats ¶
func (f *PrintFlags) AllowedFormats() []string
AllowedFormats returns string slice of allowed printing formats.
func (*PrintFlags) ToPrinter ¶
func (f *PrintFlags) ToPrinter() (ObjectPrinter, error)
func (*PrintFlags) WithDefaultOutput ¶
func (f *PrintFlags) WithDefaultOutput(format string) *PrintFlags
WithDefaultOutput sets a default output format if one is not provided through a flag value.
type PrintOptions ¶
type PrintOptions struct {
// NoHeaders configures table-based printers to skip printing headers to the output.
NoHeaders bool
// Wide configures table-based printers to include additional columns in their output.
Wide bool
}
PrintOptions is a struct that holds options for printing objects.
type TableCSVPrinterFlags ¶
type TableCSVPrinterFlags struct {
NoHeaders *bool
}
func (*TableCSVPrinterFlags) AddFlags ¶
func (t *TableCSVPrinterFlags) AddFlags(cmd *cobra.Command)
AddFlags implements FlaggablePrinter.
func (*TableCSVPrinterFlags) AllowedFormats ¶
func (t *TableCSVPrinterFlags) AllowedFormats() []string
AllowedFormats implements FlaggablePrinter.
func (*TableCSVPrinterFlags) ToPrinter ¶
func (t *TableCSVPrinterFlags) ToPrinter(format string) (ObjectPrinter, error)
ToPrinter implements FlaggablePrinter.
type TablePrinter ¶
type TableReflectorFunc ¶
TableReflectorFunc should take any struct or collection of structs and use reflection to resolve tabular data. Depending on the implementation, this may use struct field tags similar to encoding/json to declare table header labels.
var (
DefaultTableReflectorFunc TableReflectorFunc = GenerateTableData
)
type YamlJSONPrinterFlags ¶
type YamlJSONPrinterFlags struct {
JSONIndent *bool
}
func (*YamlJSONPrinterFlags) AddFlags ¶
func (y *YamlJSONPrinterFlags) AddFlags(cmd *cobra.Command)
AddFlags implements FlaggablePrinter.
func (*YamlJSONPrinterFlags) AllowedFormats ¶
func (y *YamlJSONPrinterFlags) AllowedFormats() []string
AllowedFormats implements FlaggablePrinter.
func (*YamlJSONPrinterFlags) ToPrinter ¶
func (y *YamlJSONPrinterFlags) ToPrinter(format string) (ObjectPrinter, error)
ToPrinter implements FlaggablePrinter.
type YamlPrinter ¶
type YamlPrinter struct{}