script

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: MIT Imports: 5 Imported by: 204

README

GoDocGo Report Card

script is a collection of utilities for doing the kind of tasks that shell scripts are good at: reading files, counting lines, matching strings, and so on. Why shouldn't it be as easy to write system administration programs in Go as it is in a typical shell? script aims to make it just that easy.

Just as the shell allows you to chain operations together into a pipeline, script does the same:

numLines, err := script.File("test.txt").CountLines()

This works because File returns a Pipe object. Most script operations can be methods on a pipe, and will return another pipe, so that you can chain operations indefinitely.

Ultimately, you'll want to read the results from the pipe, and you can do that using the String() method, for example. If the pipe's original data source was something that needs closing, like a file, it will be automatically closed once all the data has been read. script programs will not leak file handles.

If any pipe operation results in an error, the pipe's Error() method will return that error, and all pipe operations will henceforth be no-ops. Thus you can safely chain a whole series of operations without having to check the error status at each stage.

When you eventually read the pipe, for convenience, you will also get the pipe's error status as an extra return value:

p := script.File("doesnt_exist.txt")
out, err := p.String() // err is non-nil
res, err := p.CountLines() // err is non-nil
fmt.Println(p.Error())
// Output: open doesnt_exist.txt: no such file or directory

Documentation

Overview

Package script aims to make it easy to write shell-type scripts in Go, for general system administration purposes: reading files, counting lines, matching strings, and so on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountLines

func CountLines(name string) (int, error)

CountLines counts lines in the specified file and returns the integer result, or an error, and closes the pipe after reading. If there is an error reading the pipe, the pipe's error status is also set.

Types

type Pipe

type Pipe struct {
	Reader io.ReadCloser
	// contains filtered or unexported fields
}

Pipe represents a pipe object with an associated Reader.

func File

func File(name string) *Pipe

File returns a *Pipe associated with the specified file. This is useful for starting pipelines. If there is an error opening the file, the pipe's error status will be set.

func NewPipe

func NewPipe() *Pipe

NewPipe returns a pointer to a new empty pipe.

func (*Pipe) Close

func (p *Pipe) Close() error

Close closes the pipe's associated reader. This is always safe to do, because pipes created from a non-closable source will have an `ioutil.NopCloser` to call.

func (*Pipe) CountLines

func (p *Pipe) CountLines() (int, error)

CountLines counts lines from the pipe's reader, and returns the integer result, or an error. If there is an error reading the pipe, the pipe's error status is also set.

func (Pipe) Echo added in v0.2.0

func (p Pipe) Echo(s string) *Pipe

Echo returns a pipe containing the supplied string.

func (Pipe) Error

func (p Pipe) Error() error

Error returns the last error returned by any pipe operation, or nil otherwise.

func (Pipe) Match added in v0.2.0

func (p Pipe) Match(s string) *Pipe

Match reads from the pipe, and returns a new pipe containing only lines which contain the specified string. If there is an error reading the pipe, the pipe's error status is also set.

func (Pipe) Reject added in v0.2.0

func (p Pipe) Reject(s string) *Pipe

Reject reads from the pipe, and returns a new pipe containing only lines which do not contain the specified string. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) SetError

func (p *Pipe) SetError(err error)

SetError sets the pipe's error status to the specified error.

func (*Pipe) String

func (p *Pipe) String() (string, error)

String returns the contents of the Pipe as a string, or an error, and closes the pipe after reading. If there is an error reading, the pipe's error status is also set.

func (*Pipe) WithCloser

func (p *Pipe) WithCloser(r io.ReadCloser) *Pipe

WithCloser takes an io.ReadCloser and associates the pipe with that source.

func (*Pipe) WithError

func (p *Pipe) WithError(err error) *Pipe

WithError sets the pipe's error status to the specified error and returns the modified pipe.

func (*Pipe) WithReader

func (p *Pipe) WithReader(r io.Reader) *Pipe

WithReader takes an io.Reader which does not need to be closed after reading, and associates the pipe with that reader.

Jump to

Keyboard shortcuts

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