script: github.com/bitfield/script Index | Files

package script

import "github.com/bitfield/script"

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

Package Files

doc.go filters.go pipes.go read_auto_closer.go sinks.go sources.go

type Pipe Uses

type Pipe struct {
    Reader ReadAutoCloser
    // contains filtered or unexported fields
}

Pipe represents a pipe object with an associated ReadAutoCloser.

func Args Uses

func Args() *Pipe

Args creates a pipe containing the program's command-line arguments, one per line.

func Echo Uses

func Echo(s string) *Pipe

Echo returns a pipe containing the supplied string.

func Exec Uses

func Exec(s string) *Pipe

Exec runs an external command and returns a pipe containing the output. If the command had a non-zero exit status, the pipe's error status will also be set to the string "exit status X", where X is the integer exit status.

func File Uses

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 IfExists Uses

func IfExists(filename string) *Pipe

IfExists tests whether the specified file exists, and returns a pipe whose error status reflects the result. If the file doesn't exist, the pipe's error status will be set, and if the file does exist, the pipe will have no error status.

func ListFiles Uses

func ListFiles(path string) *Pipe

ListFiles creates a pipe containing the files and directories matching the supplied path, one per line. The path may be a glob, conforming to filepath.Match syntax.

func NewPipe Uses

func NewPipe() *Pipe

NewPipe returns a pointer to a new empty pipe.

func Slice Uses

func Slice(s []string) *Pipe

Slice returns a pipe containing each element of the supplied slice of strings, one per line.

func Stdin Uses

func Stdin() *Pipe

Stdin returns a pipe which reads from the program's standard input.

func (*Pipe) AppendFile Uses

func (p *Pipe) AppendFile(fileName string) (int64, error)

AppendFile appends the contents of the Pipe to the specified file, and closes the pipe after reading. It returns the number of bytes successfully written, or an error. If there is an error reading or writing, the pipe's error status is also set.

func (*Pipe) Basename Uses

func (p *Pipe) Basename() *Pipe

Basename reads a list of filepaths from the pipe, one per line, and removes any leading directory components from each line. If a line is empty, Basename will produce '.'. Trailing slashes are removed.

func (*Pipe) Bytes Uses

func (p *Pipe) Bytes() ([]byte, error)

Bytes returns the contents of the Pipe as a slice of byte, or an error. If there is an error reading, the pipe's error status is also set.

func (*Pipe) Close Uses

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) Column Uses

func (p *Pipe) Column(col int) *Pipe

Column reads from the pipe, and returns a new pipe containing only the Nth column of each line in the input, where '1' means the first column, and columns are delimited by whitespace. Specifically, whatever Unicode defines as whitespace ('WSpace=yes'). If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Concat Uses

func (p *Pipe) Concat() *Pipe

Concat reads a list of filenames from the pipe, one per line, and returns a pipe which reads all those files in sequence. If there are any errors (for example, non-existent files), the pipe's error status will be set to the first error encountered, but execution will continue.

func (*Pipe) CountLines Uses

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) Dirname Uses

func (p *Pipe) Dirname() *Pipe

Dirname reads a list of pathnames from the pipe, one per line, and returns a pipe which contains only the parent directories of each pathname. If a line is empty, Dirname will produce a '.'. Trailing slashes are removed, unless Dirname returns the root folder.

func (*Pipe) EachLine Uses

func (p *Pipe) EachLine(process func(string, *strings.Builder)) *Pipe

EachLine calls the specified function for each line of input, passing it the line as a string, and a *strings.Builder to write its output to. The return value from EachLine is a pipe containing the contents of the strings.Builder.

func (*Pipe) Error Uses

func (p *Pipe) Error() error

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

func (*Pipe) Exec Uses

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

Exec runs an external command and returns a pipe containing the output. If the command had a non-zero exit status, the pipe's error status will also be set to the string "exit status X", where X is the integer exit status.

func (*Pipe) ExitStatus Uses

func (p *Pipe) ExitStatus() int

ExitStatus returns the integer exit status of a previous command, if the pipe's error status is set, and if the error matches the pattern "exit status %d". Otherwise, it returns zero.

func (*Pipe) First Uses

func (p *Pipe) First(lines int) *Pipe

First reads from the pipe, and returns a new pipe containing only the first N lines. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Freq Uses

func (p *Pipe) Freq() *Pipe

Freq reads from the pipe, and returns a new pipe containing only unique lines from the input, prefixed with a frequency count, in descending numerical order (most frequent lines first). Lines with equal frequency will be sorted alphabetically. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Join Uses

func (p *Pipe) Join() *Pipe

Join reads the contents of the pipe, line by line, and joins them into a single space-separated string. It returns a pipe containing this string. Any terminating newline is preserved.

func (*Pipe) Last Uses

func (p *Pipe) Last(lines int) *Pipe

Last reads from the pipe, and returns a new pipe containing only the last N lines. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Match Uses

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) MatchRegexp Uses

func (p *Pipe) MatchRegexp(re *regexp.Regexp) *Pipe

MatchRegexp reads from the pipe, and returns a new pipe containing only lines which match the specified compiled regular expression. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Read Uses

func (p *Pipe) Read(b []byte) (int, error)

Read reads up to len(b) bytes from the data source into b. It returns the number of bytes read and any error encountered. At end of file, or on a nil pipe, Read returns 0, io.EOF.

func (*Pipe) Reject Uses

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) RejectRegexp Uses

func (p *Pipe) RejectRegexp(re *regexp.Regexp) *Pipe

RejectRegexp reads from the pipe, and returns a new pipe containing only lines which don't match the specified compiled regular expression. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) Replace Uses

func (p *Pipe) Replace(search, replace string) *Pipe

Replace filters its input by replacing all occurrences of the string `search` with the string `replace`. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) ReplaceRegexp Uses

func (p *Pipe) ReplaceRegexp(re *regexp.Regexp, replace string) *Pipe

ReplaceRegexp filters its input by replacing all matches of the compiled regular expression `re` with the replacement string `replace`. Inside `replace`, $ signs are interpreted as in regexp.Expand, so for instance "$1" represents the text of the first submatch. If there is an error reading the pipe, the pipe's error status is also set.

func (*Pipe) SetError Uses

func (p *Pipe) SetError(err error)

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

func (*Pipe) Stdout Uses

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

Stdout writes the contents of the pipe to the program's standard output. It returns the number of bytes successfully written, plus a non-nil error if the write failed or if there was an error reading from the pipe. If the pipe has error status, Stdout returns zero plus the existing error.

func (*Pipe) String Uses

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) WithError Uses

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 Uses

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

WithReader takes an io.Reader, and associates the pipe with that reader. If necessary, the reader will be automatically closed once it has been completely read.

func (*Pipe) WriteFile Uses

func (p *Pipe) WriteFile(fileName string) (int64, error)

WriteFile writes the contents of the Pipe to the specified file, and closes the pipe after reading. It returns the number of bytes successfully written, or an error. If there is an error reading or writing, the pipe's error status is also set.

type ReadAutoCloser Uses

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

ReadAutoCloser wraps an io.Reader, and closes it automatically, if closable, once it has been completely read.

func NewReadAutoCloser Uses

func NewReadAutoCloser(r io.Reader) ReadAutoCloser

NewReadAutoCloser returns an ReadAutoCloser wrapping the supplied Reader. If the Reader is not a Closer, it will be wrapped in a NopCloser to make it closable.

func (ReadAutoCloser) Close Uses

func (a ReadAutoCloser) Close() error

Close closes the data source associated with a, and returns the result of that close operation.

func (ReadAutoCloser) Read Uses

func (a ReadAutoCloser) Read(b []byte) (n int, err error)

Read reads up to len(p) bytes from the data source into p. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF. In the EOF case, the data source will be closed.

Package script imports 13 packages (graph) and is imported by 1 packages. Updated 2019-09-29. Refresh now. Tools for package owners.