filters

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

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

Go to latest
Published: Sep 12, 2017 License: GPL-3.0 Imports: 9 Imported by: 0

README

Filters

Filters Package for Golang

The Filters package is designed to allow the chaining of cli commands using stdin and stdout.

GoDoc Build Status Go Report Card

tty

The command allows setting the commands explicitly or loading from a yaml file.

Filter

A Filter is any cli command, but primarily focused on the idea of unix cli commands being used as byte filters used to mangle and transform text and piping commands together.

Chain

A Chain is a sequential list of Filters with each piping its output into the next.

Exec

From either a chain or filter an Exec can be generated.

Input for an Exec is provided as a io.Reader and output is assigned as an io.Writer.

On an Exec from a chain, input is assigned to the first process and output assigned to the last. Everything in between is automatically piped to the next Filter.

Chains

Chains allow the application to load multiple chains in one yaml file and retrive them by title.

Yaml file definitions

Filter YAML Example

Name: 'cat'
Domain: 'bash'
Version: '1.0'
Command: 'cat'
Arguments:

Chain YAML Example

Chain:
- Name: 'cat'
  Domain: 'bash'
  Version: '1.0'
  Command: 'cat'
  Arguments:
- Name: 'grep'
  Domain: 'bash'
  Version: '1.0'
  Command: 'grep'
  Arguments:
          - 'wrong'
- Name: 'xargs'
  Domain: 'bash'
  Version: '1.0'
  Command: 'xargs'
  Arguments:
          - '-n'
          - '3'

Chains YAML Example

FirstChain:
        Chain:
        - Name: 'ls'
          Domain: 'bash'
          Version: '1.0'
          Command: 'ls'
          Arguments:
          VCS:
                  Type: 'git'
                  Location: 'github.com'
        - Name: 'grep'
          Domain: 'bash'
          Version: '1.0'
          Command: 'grep'
          Arguments:
                  - 'filters'
          VCS:
                  Type: 'git'
                  Location: 'github.com'
        - Name: 'xargs'
          Domain: 'bash'
          Version: '1.0'
          Command: 'xargs'
          Arguments:
                  - '-n'
                  - '4'
          VCS:
                  Type: 'git'
                  Location: 'github.com'
SecondChain:
        Chain:
        - Name: 'ls'
          Domain: 'bash'
          Version: '1.0'
          Command: 'ls'
          Arguments:
          VCS:
                  Type: 'git'
                  Location: 'github.com'
        - Name: 'grep'
          Domain: 'bash'
          Version: '1.0'
          Command: 'grep'
          Arguments:
                  - 'filters'
          VCS:
                  Type: 'git'
                  Location: 'github.com'
        - Name: 'xargs'
          Domain: 'bash'
          Version: '1.0'
          Command: 'xargs'
          Arguments:
                  - '-n'
                  - '4'
          VCS:
                  Type: 'git'
                  Location: 'github.com'

See the godocs for more information and examples.

Documentation

Overview

Example (Chain)
text := "Alphabet city is haunted\n"
text += "Constantina feels right at home\n"
text += "She probably wont say youre wrong\n"
text += "Youre already wrong\nYoure already wrong\n"
filtercat := New()
filtercat.Command = "cat"
filtergrep := New()
filtergrep.Command = "grep"
filtergrep.Argument("wrong")
filterxargs := New()
filterxargs.Command = "xargs"
filterxargs.Argument("-n")
filterxargs.Argument("3")
c := Chain{
	Filters: []Filter{
		filtercat,
		filtergrep,
		filterxargs,
	},
}
exec, err := c.Exec()
if err != nil {
	log.Print(err)
}
exec.SetInput(bytes.NewReader([]byte(text)))
var buf bytes.Buffer
exec.SetOutput(&buf)
err = exec.Run()
if err != nil {
	log.Print(err)
}
fmt.Print(buf.String())
Output:

She probably wont
say youre wrong
Youre already wrong
Youre already wrong
Example (Chainfile)
text := "Alphabet city is haunted\n"
text += "Constantina feels right at home\n"
text += "She probably wont say youre wrong\n"
text += "Youre already wrong\nYoure already wrong\n"
c, err := ChainFile("chain.yml")
if err != nil {
	log.Print(err)
}
exec, err := c.Exec()
if err != nil {
	log.Print(err)
}
exec.SetInput(bytes.NewReader([]byte(text)))
var buf bytes.Buffer
exec.SetOutput(&buf)
err = exec.Run()
if err != nil {
	log.Print(err)
}
fmt.Print(buf.String())
Output:

She probably wont
say youre wrong
Youre already wrong
Youre already wrong
Example (Input)
text := []byte("hello world\ngoodbye world")
filter := New()
filter.Command = "grep"
filter.Argument("goodbye")
var buf bytes.Buffer
exec := filter.Exec()
exec.SetInput(bytes.NewReader(text))
exec.SetOutput(&buf)
err := exec.Run()
if err != nil {
	log.Print(err)
}
fmt.Println(buf.String())
Output:

goodbye world
Example (Simple)
text := "hello world"
filter := New()
filter.Command = "echo"
filter.Argument(text)
var buf bytes.Buffer
exec := filter.Exec()
exec.SetOutput(&buf)
err := exec.Run()
if err != nil {
	log.Print(err)
}
fmt.Println(buf.String())
Output:

hello world

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotPointer the passed in vvariable was not a pointer
	ErrNotPointer = errors.New("Unmarshal requires pointer")
	// ErrChainDoesNotExist the chain was not found in the list of chains
	ErrChainDoesNotExist = errors.New("Chain does not exist")
)

Functions

This section is empty.

Types

type Chain

type Chain struct {
	Filters []Filter `yaml:"Chain"`
}

Chain of commands where each feeds into stdin of the next

func ChainFile

func ChainFile(file string) (Chain, error)

ChainFile Loads a chain from a file

func NewChain

func NewChain() Chain

NewChain returns a new chain

func (*Chain) Exec

func (c *Chain) Exec() (*Exec, error)

Exec creates an exec from a chain of filters

type Chains

type Chains map[string]Chain

Chains is a map of chains that can be retrieved by name

func ChainsFile

func ChainsFile(file string) (Chains, error)

ChainsFile returns Chains loaded with data from a file

func NewChains

func NewChains() Chains

NewChains returns a new map of chains

func (Chains) Get

func (c Chains) Get(name string) (Chain, error)

Get returns a chain by name or error if not found.

type Error

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

Error is a filter error

func (Error) Error

func (e Error) Error() string

Error implements Error interface

func (*Error) Write

func (e *Error) Write(p []byte) (n int, err error)

Write implements Writer interface

type Exec

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

Exec is a command and with input and output attached

func (*Exec) Detach

func (e *Exec) Detach()

Detach, detaches the execution from the calling process

func (*Exec) Errors

func (e *Exec) Errors() []error

Errors returns an array of errors

func (*Exec) Run

func (e *Exec) Run() error

Run the command against the inputs and outputs

func (*Exec) SetInput

func (e *Exec) SetInput(r io.Reader)

SetInput sets input for the Exec

func (*Exec) SetOutput

func (e *Exec) SetOutput(w io.Writer)

SetOutput sets input for the Exec

type Filter

type Filter struct {
	// Program name
	Name string `yaml:"Name"`
	// Domain of program
	Domain string `yaml:"Domain"`
	// Version number
	Version string `yaml:"Version"`
	// Command to execute
	Command string `yaml:"Command"`
	// Arguments to command
	Arguments []string `yaml:"Arguments"`
	// VCS where filter can be found
	VCS `yaml:"VCS"`
}

Filter is one command to be used as a filter

func FilterFile

func FilterFile(file string) (Filter, error)

FilterFile returns a new Filter from a file definitions

func New

func New() Filter

New returns a new filter with defaults set

func (*Filter) Argument

func (f *Filter) Argument(a string)

Argument appends an argument

func (*Filter) Exec

func (f *Filter) Exec() *Exec

Exec creates an executable command that is attached to input and output

type VCS

type VCS struct {
	// VCS type
	Type string `yaml:"Type"`
	// VCS location
	Location string `yaml:"Location"`
}

VCS Defines the type and location of a version control system

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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