Documentation ¶
Overview ¶
Package klo is a "kubectl-like outputter" that prints Go values, such as structs, maps, et cetera, in several output formats, very similar to what "kubectl -o" has to offer. In particular, it offers ASCII table formatting with custom columns including column headers and self-adjusting column widths.
Example (Sortedtable) ¶
package main import ( "os" "github.com/thediveo/klo" "github.com/thediveo/klo/testutil" ) func main() { /* ignore/for testing */ out := testutil.NewTestWriter(os.Stdout) /* end ignore */ // Some data structure we want to print in tables. type myobj struct { Name string Foo int Bar string } // A slice of objects we want to print as a table with custom columns. list := []myobj{ {Name: "One", Foo: 42}, {Name: "Two", Foo: 666, Bar: "Bar"}, {Name: "Another Two", Foo: 123, Bar: "Bar"}, } // Create a table printer with custom columns, to be filled from fields // of the objects (namely, Name, Foo, and Bar fields). prn, err := klo.PrinterFromFlag("", &klo.Specs{DefaultColumnSpec: "NAME:{.Name},FOO:{.Foo},BAR:{.Bar}"}) if err != nil { panic(err) } // Use a table sorter and tell it to sort by the Name field of our column objects. table, err := klo.NewSortingPrinter("{.Name}", prn) if err != nil { panic(err) } table.Fprint(out, list) }
Output: NAME________FOO__BAR↵ Another_Two_123__Bar↵ One_________42___↵ Two_________666__Bar↵
Index ¶
- type Column
- type CustomColumnsPrinter
- type GoTemplatePrinter
- type JSONPathPrinter
- type JSONPrinter
- type SortingPrinter
- type Specs
- type ValuePrinter
- func NewCustomColumnsPrinterFromSpec(spec string) (ValuePrinter, error)
- func NewCustomColumnsPrinterFromTemplate(tr io.Reader) (ValuePrinter, error)
- func NewGoTemplatePrinter(tmpl string) (ValuePrinter, error)
- func NewGoTemplatePrinterWithFuncs(tmpl string, funcMap template.FuncMap) (ValuePrinter, error)
- func NewJSONPathPrinter(expr string) (ValuePrinter, error)
- func NewJSONPrinter() (ValuePrinter, error)
- func NewSortingPrinter(expr string, p ValuePrinter) (ValuePrinter, error)
- func NewYAMLPrinter() (ValuePrinter, error)
- func PrinterFromFlag(flagvalue string, specs *Specs) (ValuePrinter, error)
- type YAMLPrinter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct { Name string // Column name, for error reporting. Header string // Column header text. Template *jsonpath.JSONPath // Compiled JSONPath expression. Raw string // Original JSONPath expression. }
Column stores the header text and the JSONPath for fetching column values. In addition, it features a column name, which is used to identify a specific column when reporting errors.
func (*Column) SetExpression ¶
SetExpression sets the JSONPath expression for a specific column. It accepts a more relaxed JSONPath expression syntax in the same way kubectl does for its custom columns. In particular, it accepts:
- x.y.z ... without leading "." or curly braces.
- {x.y.z} ... without leading ".", but at least curly braces.
- .x.y.z ... without curly braces.
- {.x.y.z} ... and finally as "standard".
Additionally, the empty expression "" also gets accepted.
type CustomColumnsPrinter ¶
type CustomColumnsPrinter struct { // The individual columns with their headers and JSONPath expressions. Columns []*Column // Hide column headers HideHeaders bool // Padding between columns Padding int }
CustomColumnsPrinter prints neatly formatted tables with custom columns.
func (*CustomColumnsPrinter) Fprint ¶
func (p *CustomColumnsPrinter) Fprint(w io.Writer, v interface{}) error
Fprint prints the value v in a neatly formatted table according to the custom-column spec or template given when creating this custom-columns printer. The table is then written to the specified writer. If this writer is already a tabwriter, then it is the caller's responsibility to flush the tabwriter when it's the right point to do so.
type GoTemplatePrinter ¶
type GoTemplatePrinter struct { Template *template.Template // The compiled golang template. // contains filtered or unexported fields }
GoTemplatePrinter prints values in JSON format.
type JSONPathPrinter ¶
type JSONPathPrinter struct { Expr *jsonpath.JSONPath // Compiled JSONPath expression. // contains filtered or unexported fields }
JSONPathPrinter prints values in JSON format.
type SortingPrinter ¶
type SortingPrinter struct { ChainedPrinter ValuePrinter // Next ValuePrinter we chain to. SortExpr *jsonpath.JSONPath // Compiled JSONPath expression. // contains filtered or unexported fields }
SortingPrinter sorts slice values first, before it writes them to the next printer in the chain.
type Specs ¶
type Specs struct { // default custom-columns spec in format // "<header>:<json-path-expr>[,<header>:json-path-expr>]..." DefaultColumnSpec string // wide custom-columns spec in format // "<header>:<json-path-expr>[,<header>:json-path-expr>]..." WideColumnSpec string // optional separate Go template argument to output formats "go-template" // and "go-template-file". For "go-template" the arg contains the // template, for "go-template-file" it contains the template filename. GoTemplateArg string // optional any functions to be made available in go template" GoTemplateFuncMap template.FuncMap }
Specs specifies custom-column formats for the default columns in "-o=customcolumns" mode, and for the "-o=wide" wide columns mode.
type ValuePrinter ¶
ValuePrinter neatly prints values (especially slices of structs) to a writer, applying printer-specific formatting.
func NewCustomColumnsPrinterFromSpec ¶
func NewCustomColumnsPrinterFromSpec(spec string) (ValuePrinter, error)
NewCustomColumnsPrinterFromSpec returns a new custom columns printer for the given specification. This specification is in form of a string consisting of a series of <column-header-name>:<json-path-expr> elements, separated by ",". The default padding between columns is set to 0, but can be changed later using the Padding field of the printer returned.
func NewCustomColumnsPrinterFromTemplate ¶
func NewCustomColumnsPrinterFromTemplate(tr io.Reader) (ValuePrinter, error)
NewCustomColumnsPrinterFromTemplate returns a new custom columns printer for a template read from the given template stream. The template must consist of two lines, the first specifying the column headers, and the second giving the JSONPath expressions for each column. The
func NewGoTemplatePrinter ¶
func NewGoTemplatePrinter(tmpl string) (ValuePrinter, error)
NewGoTemplatePrinter returns a printer for outputting values in JSON format.
func NewGoTemplatePrinterWithFuncs ¶ added in v1.1.0
func NewGoTemplatePrinterWithFuncs(tmpl string, funcMap template.FuncMap) (ValuePrinter, error)
NewGoTemplatePrinterWithFuncs returns a printer for outputting values in JSON format, providing template functions.
func NewJSONPathPrinter ¶
func NewJSONPathPrinter(expr string) (ValuePrinter, error)
NewJSONPathPrinter returns a printer for outputting the values that were filtered using a JSONPath expression. Please note that the JSONPath expression here must be strictly conforming to the syntax rules, in particular: it must be enclosed in "{...}" and leading "." must be present. In addition, the elements addresses must exist, or otherwise an error will be raised when printing objects using this expression. In consequence, JSONPath printers are much less forgiving than the custom-column printers.
func NewJSONPrinter ¶
func NewJSONPrinter() (ValuePrinter, error)
NewJSONPrinter returns a printer for outputting values in JSON format.
func NewSortingPrinter ¶
func NewSortingPrinter(expr string, p ValuePrinter) (ValuePrinter, error)
NewSortingPrinter returns a printer for outputting values in YAML format.
func NewYAMLPrinter ¶
func NewYAMLPrinter() (ValuePrinter, error)
NewYAMLPrinter returns a printer for outputting values in YAML format.
func PrinterFromFlag ¶
func PrinterFromFlag(flagvalue string, specs *Specs) (ValuePrinter, error)
PrinterFromFlag returns a suitable value printer according to the output format specified as the flagvalue. The "-o" flag value is passed in via the flagvalue parameter (without the "-o") and should denote one of the supported output formats, such as "json", "yaml", "custom-columns", et cetera. The Specs parameter specifies the default custom-columns output format for "-o=" and "-o=wide". If Specs is nil, then no default custom-column formats will apply.