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 ¶
- type BaseProgram
- func (b *BaseProgram) Error() error
- func (b *BaseProgram) Exit(err error) error
- func (b *BaseProgram) Fprint(a ...any) error
- func (b *BaseProgram) FprintStderr(a ...any)
- func (b *BaseProgram) SetError(err error) error
- func (b *BaseProgram) SetStderr(stderr io.Writer)
- func (b *BaseProgram) SetStdin(stdin io.Reader)
- func (b *BaseProgram) SetStdout(stdout io.Writer)
- func (b *BaseProgram) Start() error
- type ExitError
- type Pipe
- type Pipeline
- func (p *Pipeline) Add(programs ...Program) *Pipeline
- func (p *Pipeline) Bytes() ([]byte, error)
- func (p *Pipeline) Close() error
- func (p *Pipeline) Error() error
- func (p *Pipeline) ExitStatus() int
- func (p *Pipeline) Int() (int, error)
- func (p *Pipeline) Int64() (int64, error)
- func (p *Pipeline) IsClosed() bool
- func (p *Pipeline) Pipe(program Program) *Pipeline
- func (p *Pipeline) Read(b []byte) (int, error)
- func (p *Pipeline) Run(programs ...Program) (int64, error)
- func (p *Pipeline) Scanner(filter func(string, io.Writer)) *Pipeline
- func (p *Pipeline) SetCombinedOutput(v bool) *Pipeline
- func (p *Pipeline) SetError(err error) *Pipeline
- func (p *Pipeline) SetExitOnError(v bool) *Pipeline
- func (p *Pipeline) Slice() ([]string, error)
- func (p *Pipeline) String() (string, error)
- func (p *Pipeline) Wait() *Pipeline
- func (p *Pipeline) WithError(err error) *Pipeline
- func (p *Pipeline) WithReader(r io.Reader) *Pipeline
- func (p *Pipeline) WithStderr(w io.Writer) *Pipeline
- func (p *Pipeline) WithStdout(w io.Writer) *Pipeline
- type Program
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseProgram ¶ added in v0.0.3
type BaseProgram struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer StartFn func() error // contains filtered or unexported fields }
BaseProgram is for convenience to easily create new programs that only require a custom StartFn function.
func NewBaseProgram ¶ added in v0.0.3
func NewBaseProgram() *BaseProgram
func (*BaseProgram) Error ¶ added in v0.0.3
func (b *BaseProgram) Error() error
func (*BaseProgram) Exit ¶ added in v0.0.3
func (b *BaseProgram) Exit(err error) error
func (*BaseProgram) Fprint ¶ added in v0.0.3
func (b *BaseProgram) Fprint(a ...any) error
func (*BaseProgram) FprintStderr ¶ added in v0.0.3
func (b *BaseProgram) FprintStderr(a ...any)
func (*BaseProgram) SetError ¶ added in v0.0.3
func (b *BaseProgram) SetError(err error) error
func (*BaseProgram) SetStderr ¶ added in v0.0.3
func (b *BaseProgram) SetStderr(stderr io.Writer)
func (*BaseProgram) SetStdin ¶ added in v0.0.3
func (b *BaseProgram) SetStdin(stdin io.Reader)
func (*BaseProgram) SetStdout ¶ added in v0.0.3
func (b *BaseProgram) SetStdout(stdout io.Writer)
func (*BaseProgram) Start ¶ added in v0.0.3
func (b *BaseProgram) Start() error
type Pipe ¶
type Pipe struct {
// contains filtered or unexported fields
}
Pipe provides a pipe that streams out what was streamed into it.
func NewReadOnlyPipe ¶
NewReaderPipe initializes a read-only pipe with the provided stream.
type Pipeline ¶
type Pipeline struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer // contains filtered or unexported fields }
Pipeline represents a pipeline object with an associated [ReadAutoCloser].
func NewPipeline ¶
func NewPipeline() *Pipeline
NewPipeline creates a new pipe with an empty reader (use [Pipe.WithReader] to attach another reader to it).
func (*Pipeline) Close ¶
Close closes the pipe's associated reader. This is a no-op if the reader is not an io.Closer.
func (*Pipeline) ExitStatus ¶
ExitStatus returns the integer exit status of a previous command (for example run by [Pipe.Exec]). This will be zero unless the pipe's error status is set and the error matches the pattern “exit status %d”.
func (*Pipeline) Pipe ¶
Pipe (Filter) adds a new program to the pipeline and ties the output stream io.Reader of the previous program into the input stream io.Reader of the next program. It then reads the output stream io.Writer of the next program to be used for further processing within the pipeline.
Pipe runs concurrently, so its goroutine will not exit until the pipe has been fully read. Use [Pipe.Wait] to wait for all concurrent pipes to complete.
func (*Pipeline) Read ¶
Read reads up to len(b) bytes from the pipe 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 (*Pipeline) Run ¶
Run adds one or more programs to the pipeline and/or runs the pipeline with all programs added to it.
func (*Pipeline) Scanner ¶
Scanner (FilterScan) sends the contents of the pipe to the function filter, a line at a time, and produces the result. filter takes each line as a string and an io.Writer to write its output to. See [Pipe.Filter] for concurrency handling.
func (*Pipeline) SetCombinedOutput ¶ added in v0.0.3
SetCombinedOutput configures the pipeline to combine stderr with stdout
func (*Pipeline) SetExitOnError ¶
SetExitOnError configures the pipeline to exit when an error has occured
func (*Pipeline) Slice ¶
Slice returns the pipe's contents as a slice of strings, one element per line, or an error.
An empty pipe will produce an empty slice. A pipe containing a single empty line (that is, a single \n character) will produce a slice containing the empty string as its single element.
func (*Pipeline) Wait ¶
Wait reads the pipe to completion and discards the result. This is mostly useful for waiting until concurrent filters have completed (see [Pipe.Filter]).
func (*Pipeline) WithReader ¶
WithReader sets the pipe's input reader to r. Once r has been completely read, it will be closed if necessary.
func (*Pipeline) WithStderr ¶
WithStderr redirects the standard error output for commands run via [Pipe.Exec] or [Pipe.ExecForEach] to the writer w, instead of going to the pipe as it normally would.