{"modulePath":"github.com/github/go-pipe","version":"v1.0.2","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package pipe\n\n## Variables\n\n```go\nvar (\n\t// IsSIGPIPE is an `ErrorMatcher` that matches `*exec.ExitError`s\n\t// that were caused by SIGPIPE. The match for `*exec.ExitError`s\n\t// uses `errors.As()`. Use like\n\t//\n\t//     p.Add(IgnoreError(someStage, IsSIGPIPE))\n\tIsSIGPIPE = IsSignal(syscall.SIGPIPE)\n\n\t// IsEPIPE is an `ErrorMatcher` that matches `syscall.EPIPE` using\n\t// `errors.Is()`. Use like\n\t//\n\t//     p.Add(IgnoreError(someStage, IsEPIPE))\n\tIsEPIPE = IsError(syscall.EPIPE)\n\n\t// IsErrClosedPipe is an `ErrorMatcher` that matches\n\t// `io.ErrClosedPipe` using `errors.Is()`. (`io.ErrClosedPipe` is\n\t// the error that results from writing to a closed\n\t// `*io.PipeWriter`.) Use like\n\t//\n\t//     p.Add(IgnoreError(someStage, IsErrClosedPipe))\n\tIsErrClosedPipe = IsError(io.ErrClosedPipe)\n\n\t// IsPipeError is an `ErrorMatcher` that matches a few different\n\t// errors that typically result if a stage writes to a subsequent\n\t// stage that has stopped reading from its stdin. Use like\n\t//\n\t//     p.Add(IgnoreError(someStage, IsPipeError))\n\tIsPipeError = AnyError(IsSIGPIPE, IsEPIPE, IsErrClosedPipe)\n)\n```\n\n```go\nvar ErrMemoryLimitExceeded = errors.New(\"memory limit exceeded\")\n```\nErrMemoryLimitExceeded is the error that will be used to kill a process, if necessary, from MemoryLimit.\n\n```go\nvar FinishEarly = errors.New(\"finish stage early\")\n```\nFinishEarly is an error that can be returned by a \\`Stage\\` to request that the iteration be ended early (possibly without reading all of its input). This \"error\" is considered a successful return, and is not reported to the caller.\n\n## Functions\n\n```go\nfunc ScanLFTerminatedLines(data []byte, atEOF bool) (advance int, token []byte, err error)\n```\nScanLFTerminatedLines is a \\`bufio.SplitFunc\\` that splits its input into lines at LF characters (not treating CR specially).\n\n## Types\n\n```go\ntype AppendVars func(context.Context, []EnvVar) []EnvVar\n```\n\n```go\ntype ContextValueFunc func(context.Context) (string, bool)\n```\n\n```go\ntype ContextValuesFunc func(context.Context) []EnvVar\n```\n\n```go\ntype Env struct {\n\t// The directory in which external commands should be executed by\n\t// default.\n\tDir string\n\n\t// Vars are extra environment variables. These will override any\n\t// environment variables that would be inherited from the current\n\t// process.\n\tVars []AppendVars\n}\n```\nEnv represents the environment that a pipeline stage should run in. It is passed to \\`Stage.Start()\\`.\n\n```go\ntype EnvVar struct {\n\t// The name of the environment variable.\n\tKey string\n\t// The value.\n\tValue string\n}\n```\nEnvVar represents an environment variable that will be provided to any child process spawned in this pipeline.\n\n```go\ntype ErrorFilter func(err error) error\n```\nErrorFilter is a function that can filter errors from \\`Stage.Wait()\\`. The original error (possibly nil) is passed in as an argument, and whatever the function returns is the error (possibly nil) that is actually emitted.\n\n```go\ntype ErrorMatcher func(err error) bool\n```\nErrorMatcher decides whether its argument matches some class of errors (e.g., errors that we want to ignore). The function will only be invoked for non-nil errors.\n\n```go\nfunc AnyError(ems ...ErrorMatcher) ErrorMatcher\n```\nAnyError returns an \\`ErrorMatcher\\` that returns true for an error that matches any of the \\`ems\\`.\n\n```go\nfunc IsError(target error) ErrorMatcher\n```\nIsError returns an ErrorIdentifier for the specified target error, matched using \\`errors.Is()\\`. Use like\n\n\tp.Add(pipe.IgnoreError(someStage, IsError(io.EOF)))\n\n```go\nfunc IsSignal(signal syscall.Signal) ErrorMatcher\n```\nIsSIGPIPE returns an \\`ErrorMatcher\\` that matches \\`\\*exec.ExitError\\`s that were caused by the specified signal. The match for \\`\\*exec.ExitError\\`s uses \\`errors.As()\\`. Note that under Windows this always returns false, because on that platform \\`WaitStatus.Signaled()\\` isn't implemented (it is hardcoded to return \\`false\\`).\n\n```go\ntype Event struct {\n\tCommand string\n\tMsg     string\n\tErr     error\n\tContext map[string]interface{}\n}\n```\nEvent represents anything that could happen during the pipeline execution\n\n```go\ntype LimitableStage interface {\n\tStage\n\n\tGetRSSAnon(context.Context) (uint64, error)\n\tKill(error)\n}\n```\nLimitableStage is the superset of Stage that must be implemented by stages passed to MemoryLimit and MemoryObserver.\n\n```go\ntype LinewiseStageFunc func(\n\tctx context.Context, env Env, line []byte, stdout *bufio.Writer,\n) error\n```\nLinewiseStageFunc is a function that can be embedded in a \\`goStage\\`. It is called once per line in the input (where \"line\" can be defined via any \\`bufio.Scanner\\`). It should process the line and may write whatever it likes to \\`stdout\\`, which is a buffered writer whose contents are forwarded to the input of the next stage of the pipeline. The function needn't write one line of output per line of input.\n\nThe function mustn't retain copies of \\`line\\`, since it may be overwritten every time the function is called.\n\nThe function needn't flush or close \\`stdout\\` (this will be done automatically when all of the input has been processed).\n\nIf there is an error parsing the input into lines, or if this function returns an error, then the whole pipeline will be aborted with that error. However, if the function returns the special error \\`pipe.FinishEarly\\`, the stage will stop processing immediately with a \\`nil\\` error value.\n\nThe function will be called in a separate goroutine, so it must be careful to synchronize any data access aside from writing to \\`stdout\\`.\n\n```go\ntype NewPipeFn func(opts ...Option) *Pipeline\n```\n\n```go\ntype NewScannerFunc func(r io.Reader) (Scanner, error)\n```\nNewScannerFunc is used to create a \\`Scanner\\` for scanning input that is coming from \\`r\\`.\n\n```go\ntype Option func(*Pipeline)\n```\nOption is a type alias for Pipeline functional options.\n\n```go\nfunc WithDir(dir string) Option\n```\nWithDir sets the default directory for running external commands.\n\n```go\nfunc WithEnvVar(key, value string) Option\n```\nWithEnvVar appends an environment variable for the pipeline.\n\n```go\nfunc WithEnvVarFunc(key string, valueFunc ContextValueFunc) Option\n```\nWithEnvVarFunc appends a context-based environment variable for the pipeline.\n\n```go\nfunc WithEnvVars(b []EnvVar) Option\n```\nWithEnvVars appends several environment variable for the pipeline.\n\n```go\nfunc WithEnvVarsFunc(valuesFunc ContextValuesFunc) Option\n```\nWithEnvVarsFunc appends several context-based environment variables for the pipeline.\n\n```go\nfunc WithEventHandler(handler func(e *Event)) Option\n```\nWithEventHandler sets a handler for the pipeline. Setting one will emit and event for each process.\n\n```go\nfunc WithStdin(stdin io.Reader) Option\n```\nWithStdin assigns stdin to the first command in the pipeline.\n\n```go\nfunc WithStdout(stdout io.Writer) Option\n```\nWithStdout assigns stdout to the last command in the pipeline.\n\n```go\nfunc WithStdoutCloser(stdout io.WriteCloser) Option\n```\nWithStdoutCloser assigns stdout to the last command in the pipeline, and closes stdout when it's done.\n\n```go\ntype Pipeline struct {\n\t// contains filtered or unexported fields\n}\n```\nPipeline represents a Unix-like pipe that can include multiple stages, including external processes but also and stages written in Go.\n\n```go\nfunc New(options ...Option) *Pipeline\n```\nNewPipeline returns a Pipeline struct with all of the \\`options\\` applied.\n\n```go\nfunc (p *Pipeline) Add(stages ...Stage)\n```\nAdd appends one or more stages to the pipeline.\n\n```go\nfunc (p *Pipeline) AddWithIgnoredError(em ErrorMatcher, stages ...Stage)\n```\nAddWithIgnoredError appends one or more stages that are ignoring the passed in error to the pipeline.\n\n```go\nfunc (p *Pipeline) Output(ctx context.Context) ([]byte, error)\n```\n\n```go\nfunc (p *Pipeline) Run(ctx context.Context) error\n```\nRun starts and waits for the commands in the pipeline.\n\n```go\nfunc (p *Pipeline) Start(ctx context.Context) error\n```\nStart starts the commands in the pipeline. If \\`Start()\\` exits without an error, \\`Wait()\\` must also be called, to allow all resources to be freed.\n\n```go\nfunc (p *Pipeline) Wait() error\n```\nWait waits for each stage in the pipeline to exit.\n\n```go\ntype Scanner interface {\n\tScan() bool\n\tBytes() []byte\n\tErr() error\n}\n```\nScanner defines the interface (which is implemented by \\`bufio.Scanner\\`) that is needed by \\`AddScannerFunction()\\`. See \\`bufio.Scanner\\` for how these methods should behave.\n\n```go\ntype Stage interface {\n\t// Name returns the name of the stage.\n\tName() string\n\n\t// Start starts the stage in the background, in the environment\n\t// described by `env`, and using `stdin` as input. (`stdin` should\n\t// be set to `nil` if the stage is to receive no input, which\n\t// might be the case for the first stage in a pipeline.) It\n\t// returns an `io.ReadCloser` from which the stage's output can be\n\t// read (or `nil` if it generates no output, which should only be\n\t// the case for the last stage in a pipeline). It is the stages'\n\t// responsibility to close `stdin` (if it is not nil) when it has\n\t// read all of the input that it needs, and to close the write end\n\t// of its output reader when it is done, as that is generally how\n\t// the subsequent stage knows that it has received all of its\n\t// input and can finish its work, too.\n\t//\n\t// If `Start()` returns without an error, `Wait()` must also be\n\t// called, to allow all resources to be freed.\n\tStart(ctx context.Context, env Env, stdin io.ReadCloser) (io.ReadCloser, error)\n\n\t// Wait waits for the stage to be done, either because it has\n\t// finished or because it has been killed due to the expiration of\n\t// the context passed to `Start()`.\n\tWait() error\n}\n```\nStage is an element of a \\`Pipeline\\`.\n\n```go\nfunc Command(command string, args ...string) Stage\n```\nCommand returns a pipeline \\`Stage\\` based on the specified external \\`command\\`, run with the given command-line \\`args\\`. Its stdin and stdout are handled as usual, and its stderr is collected and included in any \\`\\*exec.ExitError\\` that the command might emit.\n\n```go\nfunc CommandStage(name string, cmd *exec.Cmd) Stage\n```\nCommandStage returns a pipeline \\`Stage\\` with the name \\`name\\`, based on the specified \\`cmd\\`. Its stdin and stdout are handled as usual, and its stderr is collected and included in any \\`\\*exec.ExitError\\` that the command might emit.\n\n```go\nfunc FilterError(s Stage, filter ErrorFilter) Stage\n```\n\n```go\nfunc Function(name string, f StageFunc) Stage\n```\nFunction returns a pipeline \\`Stage\\` that will run a \\`StageFunc\\` in a separate goroutine to process the data. See \\`StageFunc\\` for more information.\n\n```go\nfunc IgnoreError(s Stage, em ErrorMatcher) Stage\n```\nIgnoreError creates a stage that acts like \\`s\\` except that it ignores any errors that are matched by \\`em\\`. Use like\n\n\tp.Add(pipe.IgnoreError(\n\t    someStage,\n\t    func(err error) bool {\n\t        var myError *MyErrorType\n\t        return errors.As(err, \u0026myError) \u0026\u0026 myError.foo == 42\n\t    },\n\t)\n\nThe second argument can also be one of the \\`ErrorMatcher\\`s that are provided by this package (e.g., \\`IsError(target)\\`, IsSignal(signal), \\`IsSIGPIPE\\`, \\`IsEPIPE\\`, \\`IsPipeError\\`), or one of the functions from the standard library that has the same signature (e.g., \\`os.IsTimeout\\`), or some combination of these (e.g., \\`AnyError(IsSIGPIPE, os.IsTimeout)\\`).\n\n```go\nfunc LinewiseFunction(name string, f LinewiseStageFunc) Stage\n```\nLinewiseFunction returns a function-based \\`Stage\\`. The input will be split into LF-terminated lines and passed to the function one line at a time (without the LF). The function may emit output to its \\`stdout\\` argument. See the definition of \\`LinewiseStageFunc\\` for more information.\n\nNote that the stage will emit an error if any line (including its end-of-line terminator) exceeds 64 kiB in length. If this is too short, use \\`ScannerFunction()\\` directly with your own \\`NewScannerFunc\\` as argument, or use \\`Function()\\` directly with your own \\`StageFunc\\`.\n\n```go\nfunc MemoryLimit(stage Stage, byteLimit uint64, eventHandler func(e *Event)) Stage\n```\nMemoryLimit watches the memory usage of the stage and stops it if it exceeds the given limit.\n\n```go\nfunc MemoryObserver(stage Stage, eventHandler func(e *Event)) Stage\n```\nMemoryObserver watches memory use of the stage and logs the maximum value when the stage exits.\n\n```go\nfunc Print(a ...interface{}) Stage\n```\n\n```go\nfunc Printf(format string, a ...interface{}) Stage\n```\n\n```go\nfunc Println(a ...interface{}) Stage\n```\n\n```go\nfunc ScannerFunction(\n\tname string, newScanner NewScannerFunc, f LinewiseStageFunc,\n) Stage\n```\nScannerFunction creates a function-based \\`Stage\\`. The function will be passed input, one line at a time, and may emit output. See the definition of \\`LinewiseStageFunc\\` for more information.\n\n```go\ntype StageFunc func(ctx context.Context, env Env, stdin io.Reader, stdout io.Writer) error\n```\nStageFunc is a function that can be used to power a \\`goStage\\`. It should read its input from \\`stdin\\` and write its output to \\`stdout\\`. \\`stdin\\` and \\`stdout\\` will be closed automatically (if necessary) once the function returns.\n\nNeither \\`stdin\\` nor \\`stdout\\` are necessarily buffered. If the \\`StageFunc\\` requires buffering, it needs to arrange that itself.\n\nA \\`StageFunc\\` is run in a separate goroutine, so it must be careful to synchronize any data access aside from reading and writing.\n\n","path":"github.com/github/go-pipe/pipe","name":"pipe","synopsis":"","isRedistributable":true}
