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 ¶
- Variables
- type Env
- type Pipe
- func Args() *Pipe
- func Echo(s string) *Pipe
- func Exec(s ...string) *Pipe
- func Execf(s string, a ...interface{}) *Pipe
- func File(name string) *Pipe
- func FindFiles(path string) *Pipe
- func IfExists(filename string) *Pipe
- func ListFiles(path string) *Pipe
- func NewPipe() *Pipe
- func SetEnv(env Env) *Pipe
- func Slice(s []string) *Pipe
- func Stdin() *Pipe
- func (p *Pipe) AppendFile(fileName string) (int64, error)
- func (p *Pipe) Basename() *Pipe
- func (p *Pipe) Bytes() ([]byte, error)
- func (p *Pipe) Close() error
- func (p *Pipe) Column(col int) *Pipe
- func (p *Pipe) Concat() *Pipe
- func (p *Pipe) CountLines() (int, error)
- func (p *Pipe) Dirname() *Pipe
- func (p *Pipe) EachLine(process func(string, *strings.Builder)) *Pipe
- func (p *Pipe) End() (stdout, stderr string, err error)
- func (p *Pipe) Error() error
- func (p *Pipe) Exec(cmdLine ...string) *Pipe
- func (p *Pipe) ExecForEach(cmdTpl string) *Pipe
- func (p *Pipe) Execf(s string, a ...interface{}) *Pipe
- func (p *Pipe) ExitStatus() int
- func (p *Pipe) First(lines int) *Pipe
- func (p *Pipe) Freq() *Pipe
- func (p *Pipe) JSON(obj interface{}) error
- func (p *Pipe) Join() *Pipe
- func (p *Pipe) Last(lines int) *Pipe
- func (p *Pipe) Match(s string) *Pipe
- func (p *Pipe) MatchRegexp(re *regexp.Regexp) *Pipe
- func (p *Pipe) Read(b []byte) (int, error)
- func (p *Pipe) Reject(s string) *Pipe
- func (p *Pipe) RejectRegexp(re *regexp.Regexp) *Pipe
- func (p *Pipe) Replace(search, replace string) *Pipe
- func (p *Pipe) ReplaceRegexp(re *regexp.Regexp, replace string) *Pipe
- func (p *Pipe) SHA256Sum() (string, error)
- func (p *Pipe) SHA256Sums() *Pipe
- func (p *Pipe) SetEnv(env Env) *Pipe
- func (p *Pipe) SetError(err error)
- func (p *Pipe) Slice() ([]string, error)
- func (p *Pipe) SliceOfColumns() ([][]string, error)
- func (p *Pipe) Stdout() (int, error)
- func (p *Pipe) String() (string, error)
- func (p *Pipe) Template(data interface{}) *Pipe
- func (p *Pipe) WithError(err error) *Pipe
- func (p *Pipe) WithReader(r io.Reader) *Pipe
- func (p *Pipe) WriteFile(fileName string) (int64, error)
- type ReadAutoCloser
Constants ¶
This section is empty.
Variables ¶
var DebugExec = false
Functions ¶
This section is empty.
Types ¶
type Pipe ¶
type Pipe struct { Reader ReadAutoCloser Env Env // contains filtered or unexported fields }
Pipe represents a pipe object with an associated ReadAutoCloser.
func Args ¶
func Args() *Pipe
Args creates a pipe containing the program's command-line arguments, one per line.
func Exec ¶
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 ¶
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 FindFiles ¶
FindFiles takes a directory path and returns a pipe listing all the files in the directory and its subdirectories recursively, one per line, like Unix `find -type f`. If the path doesn't exist or can't be read, the pipe's error status will be set.
func IfExists ¶
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 ¶
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 Slice ¶
Slice returns a pipe containing each element of the supplied slice of strings, one per line.
func (*Pipe) AppendFile ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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), these will be ignored, execution will continue, and the pipe's error status will not be set.
func (*Pipe) CountLines ¶
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 ¶
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 ¶
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) Exec ¶
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) ExecForEach ¶
ExecForEach runs the supplied command once for each line of input, and returns a pipe containing the output. The command string is interpreted as a Go template, so `{{.}}` will be replaced with the input value, for example. If any command resulted in 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) SHA256Sum ¶
SHA256Sum calculates the SHA-256 of the file from the pipe's reader, and returns the string result, or an error. If there is an error reading the pipe, the pipe's error status is also set.
func (*Pipe) SHA256Sums ¶
SHA256Sums reads a list of file paths from the pipe, one per line, and returns a pipe which contains the SHA-256 checksum of each pathname. 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) Slice ¶
Slice returns the contents of the pipe as a slice of strings, one element per line, or an error. If there is an error reading the pipe, the pipe's error status is also set.
func (*Pipe) SliceOfColumns ¶
func (*Pipe) Stdout ¶
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 ¶
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 ¶
WithError sets the pipe's error status to the specified error and returns the modified pipe.
func (*Pipe) WithReader ¶
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.
type ReadAutoCloser ¶
type ReadAutoCloser struct {
// contains filtered or unexported fields
}
ReadAutoCloser represents a pipe source which will be automatically closed once it has been fully read.
func NewReadAutoCloser ¶
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 ¶
func (a ReadAutoCloser) Close() error
Close closes the data source associated with a, and returns the result of that close operation.
func (ReadAutoCloser) Read ¶
func (a ReadAutoCloser) Read(buf []byte) (n int, err error)
Read reads up to len(buf) bytes from the data source into buf. 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.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
visitors
This program reads an Apache logfile in Common Log Format, like this: 212.205.21.11 - - [30/Jun/2019:17:06:15 +0000] "GET / HTTP/1.1" 200 2028 "https://example.com/ "Mozilla/5.0 (Linux; Android 8.0.0; FIG-LX1 Build/HUAWEIFIG-LX1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.156 Mobile Safari/537.36" It extracts the first column of each line (the visitor IP address), counts the frequency of each unique IP address in the log, and outputs the 10 most frequent visitors in the log.
|
This program reads an Apache logfile in Common Log Format, like this: 212.205.21.11 - - [30/Jun/2019:17:06:15 +0000] "GET / HTTP/1.1" 200 2028 "https://example.com/ "Mozilla/5.0 (Linux; Android 8.0.0; FIG-LX1 Build/HUAWEIFIG-LX1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.156 Mobile Safari/537.36" It extracts the first column of each line (the visitor IP address), counts the frequency of each unique IP address in the log, and outputs the 10 most frequent visitors in the log. |