output

package
v3.0.0-...-c77a060 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// List is the canonical name of the List output format
	List Format = "list"
	// Table is the canonical name of the Table output format
	Table = "table"
	// JSON is the canonical name of the JSON output format
	JSON = "json"
	// Human is the canonical name of the Human output format
	Human = "human"
	// Debug is the canonical name of the Debug output format
	Debug = "debug"
)

Variables

View Source
var FormatFns = map[Format]Fn{
	Debug: func(wr io.Writer, cfg Config, obj Outputtable) error {
		_, err := fmt.Fprintf(wr, "%#v", obj)
		return err
	},
	JSON: func(wr io.Writer, _ Config, obj Outputtable) error {
		encoder := json.NewEncoder(wr)
		encoder.SetIndent("", "    ")
		return encoder.Encode(obj)
	},
	List:  outputTable,
	Table: outputTable,
	Human: func(wr io.Writer, _ Config, obj Outputtable) error {
		return obj.PrettyPrint(wr, prettyprint.Full)
	},
}

FormatFns is a map which contains all the supported output format functions -- except 'human' because that's implemented in the OutputInDesiredForm method, by necessity.

Functions

func RenderTable

func RenderTable(wr io.Writer, cfg Config, obj interface{}) error

RenderTable creates a table for the given object. This makes most sense when it's an array, but a regular struct-y object works fine too.

func SetupTable

func SetupTable(wr io.Writer, cfg Config) (table *tablewriter.Table)

SetupTable creates a tablewriter.Table for the given writer and output config.

func SupportedOutputFormats

func SupportedOutputFormats() (outputFormats []string)

SupportedOutputFormats returns a list of all suppported output forms, including 'human'

func Write

func Write(wr io.Writer, cfg Config, obj Outputtable) error

Write writes obj to writer in the manner specified by the config, using one of the functions in OutputFormatFns

Types

type Config

type Config struct {
	Fields []string
	Format Format
}

Config is a simple struct to configure whichever OutputFormatFn is selected all its fields should be set regardless of Format.

type DefaultFieldsHaver

type DefaultFieldsHaver interface {
	// DefaultFields must return a string of valid field names (according to github.com/BytemarkHosting/row - i.e. they must be in the list output by row.FieldsFrom) for the type it is implemented on.
	// It is used to discover what fields should be output by output.Write when there's no list of user-specified fields.
	DefaultFields(f Format) string
}

DefaultFieldsHaver is an interface that must be implemented in order to pass objects to output.Write

type Fn

type Fn func(wr io.Writer, config Config, obj Outputtable) error

Fn is a function for outputting an object to the terminal in some way See the FormatFns map to see examples

type Format

type Format string

Format is a canonical name of output formats

func FormatByName

func FormatByName(name string) Format

FormatByName returns the Format for the given format name. If the name is not valid, returns Human

type Outputtable

type Outputtable interface {
	prettyprint.PrettyPrinter
	DefaultFieldsHaver
}

Outputtable is an interface that means the object has a default set of fields and an implementation of prettyprint.PrettyPrinter. This means it can be output as a table or list, or in a simpler human-readable format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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