gout

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: MPL-2.0 Imports: 7 Imported by: 31

README

GOUT (Previously go-output-format)

Test codecov Go Report Card Go Reference Mentioned in Awesome Go

gout is the Go OUTput Formatter for serializing data in a standard way.

Helper utility to output data structures in to standardized formats, much like what is built in to vault, az and kubectl

I really like how these apps provide for flexible output, but wanted a way to do it without needing to re-write or copy it for every new tool.

Need to parse some output with jq? JSON is your format. Want to put it out in an easy to read yet still standardized format? YAML is for you!

This tool is intended to provide all that in a single reusable package.

Usage

import "github.com/drewstinnett/gout/v2"
Builtin

Gout now comes with a builtin instance, similar to the way Viper does things.

Example Usage:

gout.MustPrint(struct {
  FirstName string
  LastName  string
}{
  FirstName: "Bob",
  LastName:  "Ross",
})

Full example code here

Custom

Example Usage:

// Create a new instance
w, err := gout.New()
if err != nil {
  panic(err)
}

// Use a custom writer
w.SetWriter(os.Stderr)

// Print something!
w.MustPrint(struct {
  FirstName string
  LastName  string
}{
  FirstName: "Bob",
  LastName:  "Ross",
})
// {"FirstName":"Bob","LastName":"Ross"}

Built-in Formatters

YAML

Uses the standard gopkg.in/yaml.v3 library.

JSON

Uses the standard encoding/json library.

TOML

Uses github.com/pelletier/go-toml/v2 library

CSV

Uses github.com/jszwec/csvutil library. NOTE: You must pass an iterable interface in when using this format. It won't do a single struct.

XML

Uses encoding/xml library. NOTE: This plugin only works with structs supported by the base library

Plain

This is just vanilla old Golang output, using the %+v format.

GoTemplate

Use this format to parse the data in to a golang template. Useful for spitting data out in a more arbitrary format. This uses the text/template package to parse each item in the return slice. See the example here for full details.

  • Gout-Cobra - Configure a cobra.Command to output using Gout

Coming Soon?

TSV (Tab Separated Values)

Intention here is to have a simple way to print out a data structure in a way that grep and the like can parse it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustPrint added in v2.1.2

func MustPrint(v interface{})

MustPrint print an interface and panic if there is any sort of error

func MustPrintMulti added in v2.1.2

func MustPrintMulti(v ...interface{})

MustPrintMulti print an multiple interfaces and panic if there is any sort of error

func Print added in v2.1.2

func Print(v interface{}) (err error)

Print print an interface using the given Formatter and io.Writer

func PrintMulti added in v2.1.2

func PrintMulti(v ...interface{}) (err error)

PrintMulti useful when wanting to print multiple interfaces to a single serialized item

func SetFormatter added in v2.1.2

func SetFormatter(f formats.Formatter)

SetFormatter sets the Formatter to use for the text.

func SetWriter added in v2.1.2

func SetWriter(i io.Writer)

SetWriter set the io.Writer that will be used for printing. By default, this will be os.Stdout

Types

type Gout

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

Gout is a structure you can use that contains a formatter, and a target io.Writer

func Get added in v2.3.0

func Get() *Gout

Get gets the default Gout instance

func New

func New(opts ...Option) *Gout

New creates a pointer to a new Gout, with some sensible defaults

func (*Gout) MustPrint

func (g *Gout) MustPrint(v interface{})

func (*Gout) MustPrintMulti

func (g *Gout) MustPrintMulti(v ...interface{})

func (*Gout) Print

func (g *Gout) Print(v interface{}) (err error)

func (*Gout) PrintMulti

func (g *Gout) PrintMulti(v ...interface{}) (err error)

func (*Gout) SetFormatter

func (g *Gout) SetFormatter(f formats.Formatter)

func (*Gout) SetWriter

func (g *Gout) SetWriter(i io.Writer)

type Option added in v2.1.2

type Option func(*Gout)

Option is an option that can be passed in to help configure a Gout instance

func WithFormatter added in v2.1.2

func WithFormatter(f formats.Formatter) Option

WithFormatter can be passed to New(), specifying which Formatter should be used for output

func WithWriter added in v2.1.2

func WithWriter(w io.Writer) Option

WithWriter can be passed to New(), specifying which writer should be used for output

Directories

Path Synopsis
_examples
builtin
Package builtin is a helper to register all the built in formatters
Package builtin is a helper to register all the built in formatters
xml

Jump to

Keyboard shortcuts

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