prefixer

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

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

Go to latest
Published: Jan 18, 2016 License: MIT Imports: 2 Imported by: 40

README

Prefixer

Golang's io.Reader wrapper prepending every line with a given string.

GoDoc Travis

Use cases

  1. Logger that prefixes every line with a timestamp etc.

    16:54:49 My awesome server | Creating etcd client pointing to http://localhost:4001
    16:54:49 My awesome server | Listening on http://localhost:8080
    16:54:49 My awesome server | [restful/swagger] listing is available at 127.0.0.1:8080/swaggerapi
    
  2. SSH multiplexer prepending output from multiple servers with a hostname

    host1.example.com | SUCCESS
    host2.example.com | SUCCESS
    host3.example.com | -bash: cd: workdir: No such file or directory
    host4.example.com | SUCCESS
    
  3. Create an email reply ("> " prefix) from any text easily.

    $ ./prefix
    Dear John,               
    did you know that https://github.com/goware/prefixer is a golang pkg
    that prefixes every line with a given string and accepts any io.Reader?
    
    Cheers,
    - Jane
    ^D     
    > Dear John,               
    > did you know that https://github.com/goware/prefixer is a golang pkg
    > that prefixes every line with a given string and accepts any io.Reader?
    >
    > Cheers,
    > - Jane
    

Example

See the "Prefix Line Reader" example.

package main

import (
    "io/ioutil"
    "os"

    "github.com/goware/prefixer"
)

func main() {
    // Prefixer accepts anything that implements io.Reader interface
    prefixReader := prefixer.New(os.Stdin, "> ")

    // Read all prefixed lines from STDIN into a buffer
    buffer, _ := ioutil.ReadAll(prefixReader)

    // Write buffer to STDOUT
    os.Stdout.Write(buffer)
}

License

Prefixer is licensed under the MIT License.

Documentation

Overview

Package prefixer implements io.Reader wrapper prepending every line with a given string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Prefixer

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

Prefixer implements io.Reader and io.WriterTo. It reads data from the underlying reader and prepends every line with a given string.

func New

func New(r io.Reader, prefix string) *Prefixer

New creates a new instance of Prefixer.

func (*Prefixer) Read

func (r *Prefixer) Read(p []byte) (n int, err error)

Read implements io.Reader. It reads data into p from the underlying reader and prepends every line with a prefix. It does not block if no data is available yet. It returns the number of bytes read into p.

func (*Prefixer) WriteTo

func (r *Prefixer) WriteTo(w io.Writer) (n int64, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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