report

package
v0.38.11 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2021 License: Apache-2.0 Imports: 9 Imported by: 78

Documentation

Overview

Package report provides helper structs/methods/funcs for formatting output

To format output for an array of structs:

w := report.NewWriterDefault(os.Stdout)
defer w.Flush()

headers := report.Headers(struct {
	ID string
}{}, nil)
t, _ := report.NewTemplate("command name").Parse("{{range .}}{{.ID}}{{end}}")
t.Execute(t, headers)
t.Execute(t, map[string]string{
	"ID":"fa85da03b40141899f3af3de6d27852b",
})
// t.IsTable() == false

or

w := report.NewWriterDefault(os.Stdout)
defer w.Flush()

headers := report.Headers(struct {
	CID string
}{}, map[string]string{
	"CID":"ID"})
t, _ := report.NewTemplate("command name").Parse("table {{.CID}}")
t.Execute(t, headers)
t.Execute(t,map[string]string{
	"CID":"fa85da03b40141899f3af3de6d27852b",
})
// t.IsTable() == true

Helpers:

if report.IsJSON(cmd.Flag("format").Value.String()) {
	... process JSON and output
}

Template Functions:

The following template functions are added to the template when parsed:

  • join strings.Join, {{join .Field separator}}
  • lower strings.ToLower {{ .Field | lower }}
  • split strings.Split {{ .Field | split }}
  • title strings.Title {{ .Field | title }}
  • upper strings.ToUpper {{ .Field | upper }}

report.Funcs() may be used to add additional template functions. Adding an existing function will replace that function for the life of that template.

Note: Your code should not ignore errors

Index

Constants

This section is empty.

Variables

View Source
var DefaultFuncs = FuncMap{
	"join": strings.Join,
	"json": func(v interface{}) string {
		buf := &bytes.Buffer{}
		enc := json.NewEncoder(buf)
		enc.SetEscapeHTML(false)
		enc.Encode(v)

		return strings.TrimSpace(buf.String())
	},
	"lower":    strings.ToLower,
	"pad":      padWithSpace,
	"split":    strings.Split,
	"title":    strings.Title,
	"truncate": truncateWithLength,
	"upper":    strings.ToUpper,
}

Functions

func EnforceRange added in v0.35.0

func EnforceRange(format string) string

EnforceRange ensures that the format string contains a range

func HasTable added in v0.35.0

func HasTable(format string) bool

HasTable returns whether the format is a table

func Headers

func Headers(object interface{}, overrides map[string]string) []map[string]string

Headers queries the interface for field names. Array of map is returned to support range templates Note: unexported fields can be supported by adding field to overrides Note: It is left to the developer to write out said headers

Podman commands use the general rules of:
1) unchanged --format includes headers
2) --format '{{.ID}"        # no headers
3) --format 'table {{.ID}}' # includes headers

func IsJSON

func IsJSON(s string) bool

JSONFormat test CLI --format string to be a JSON request

if report.IsJSON(cmd.Flag("format").Value.String()) {
  ... process JSON and output
}

func NormalizeFormat

func NormalizeFormat(format string) string

NormalizeFormat reads given go template format provided by CLI and munges it into what we need

Types

type FuncMap

type FuncMap template.FuncMap

FuncMap is aliased from template.FuncMap

type Template

type Template struct {
	*template.Template
	// contains filtered or unexported fields
}

Template embeds template.Template to add functionality to methods

func NewTemplate

func NewTemplate(name string) *Template

NewTemplate creates a new template object

func (*Template) Funcs

func (t *Template) Funcs(funcMap FuncMap) *Template

Funcs adds the elements of the argument map to the template's function map. A default template function will be replace if there is a key collision.

func (*Template) IsTable

func (t *Template) IsTable() bool

IsTable returns true if format string defines a "table"

func (*Template) Parse

func (t *Template) Parse(text string) (*Template, error)

Parse parses text as a template body for t

type Writer

type Writer struct {
	*tabwriter.Writer
}

Writer aliases tabwriter.Writer to provide Podman defaults

func NewWriter

func NewWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) (*Writer, error)

NewWriter initializes a new report.Writer with given values

func NewWriterDefault

func NewWriterDefault(output io.Writer) (*Writer, error)

NewWriterDefault initializes a new report.Writer with Podman defaults

func (*Writer) Flush

func (w *Writer) Flush() error

Flush any output left in buffers

Directories

Path Synopsis
Package camelcase is a micro package to split the words of a camelcase type string into a slice of words.
Package camelcase is a micro package to split the words of a camelcase type string into a slice of words.

Jump to

Keyboard shortcuts

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