prefixedio

package module
v0.0.0-...-5733675 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2019 License: MIT Imports: 7 Imported by: 328

README

prefixedio

prefixedio (Golang package: prefixedio) is a package for Go that takes an io.Reader and de-multiplexes line-oriented data based on a line prefix to a set of readers.

Installation and Usage

Install using go get github.com/mitchellh/prefixedio.

Full documentation is available at http://godoc.org/github.com/mitchellh/prefixedio

Below is an example of its usage ignoring errors:

// Assume r is some set io.Reader. Perhaps a file, network, anything.
var r io.Reader

// Initialize the prefixed reader
pr, _ := prefixedio.NewReader(r)

// Grab readers for a couple prefixes
errR, _ := pr.Prefix("err: ")
outR, _ := pr.Prefix("out: ")

// Copy the data to different places based on the prefix
go io.Copy(os.Stderr, errR)
go io.Copy(os.Stdout, outR)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	FlushTimeout time.Duration
	// contains filtered or unexported fields
}

Reader reads from another io.Reader and de-multiplexes line-oriented data into different io.Reader streams.

Lines are delimited with the '\n' character.

When `Read` is called, any data that doesn't currently have a prefix registered will be discarded. Data won't start being discarded until the first Read is called on a prefix. Once the first Read is called, data is read until EOF. Therefore, be sure to request all prefix readers before issuing any Read calls on any prefixes.

Reads will block if all the readers aren't routinely draining their buffers. Therefore, be sure to be actively reading from all registered prefixes, otherwise you can encounter deadlock scenarios.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader creates a new Reader with the given io.Reader.

func (*Reader) Prefix

func (r *Reader) Prefix(p string) (io.Reader, error)

Prefix returns a new io.Reader that will read data that is prefixed with the given prefix.

The read data is line-oriented so calling Read will result in a full line of output (including the line separator), but is exposed as an io.Reader for useful utility interoperating with other Go libraries.

The data read has the prefix stripped, but contains the line delimiter.

An empty prefix "" will read the data before any other prefix match is found, allowing you to have a default reader before a prefix is matched.

Jump to

Keyboard shortcuts

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