Documentation ¶
Overview ¶
package pipe implements type-safe unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go native types which can be connected together like unix allows.
Example ¶
ctx := context.Background() cat := Lines{ cat: []string{"three", "small", "pigs"}, } wc := CountLines{} out := &StringBuffer{} stdio := NewStdio[string]( nil, out, os.Stderr, ) // an equivalent of cat | wc -l // just using a native Go types and channels err := NewLine[string]().Run(ctx, stdio, cat, wc) if err != nil { log.Fatal(err) } fmt.Println(out.String())
Output: 3
Index ¶
Examples ¶
Constants ¶
const ( // NotExecutable is from POSIX and indicates tool was found, but not executable NotExecutable = 126 // NotFound is from POSIX and indicate a tool was not found NotFound = 127 // UnknownError is a code used for unpacking other than pipe.Error UnknownError = 250 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
Error is a common error type returned by pipeline. It has a Code for unix compatibility and an error.
func FromError ¶
FromError unpacks error into Error. If it can't be unpacked, it assigns code 250 error fs.ErrPermission will get code NotExecutable (126) error exec.ErrNotFound will get code NotFound (127) *exec.ExitError will be converted to Error with Code: ExitCode Error will be returned unchanged all other error (including nil) will be returned with code UnknownError
type Line ¶
type Line[T any] struct { // contains filtered or unexported fields }
pipe.Line implements the unix-like pipeline for command | command2 | command3 by connecting the filters via gio.Pipe
Example (Nopipefail) ¶
ctx := context.Background() cat := Lines{ cat: []string{"three", "small", "pigs"}, } fail := Fail{err: io.EOF} wc := CountLines{} out := &StringBuffer{} stdio := NewStdio[string]( nil, out, os.Stderr, ) // an equivalent of false | cat | wc -l err := NewLine[string]().Pipefail(false).Run(ctx, stdio, fail, cat, wc) if err != nil { log.Fatal(err) } fmt.Println("OK")
Output: OK
Example (Pipefail) ¶
ctx := context.Background() cat := Lines{ cat: []string{"three", "small", "pigs"}, } fail := Fail{err: io.EOF} wc := CountLines{} out := &StringBuffer{} stdio := NewStdio[string]( nil, out, os.Stderr, ) // an equivalent of set -o pipefail; false | cat | wc -l err := NewLine[string]().Pipefail(true).Run(ctx, stdio, fail, cat, wc) if err == nil { log.Fatal("expected err, got nil") } fmt.Println(err)
Output: Error{Code: 1, Err: EOF}
func (Line[T]) Pipefail ¶
Pipefail - true (the default) is an equivalent of set -o pipefail, so pipe is canceled on a first error and this is returned upper. To simulate default shell behavior use Pipefail(false). Which does not cancel the pipe and returns the last error.
func (Line[T]) Run ¶
Run joins all filters via gio.Pipe with a stdio. Each filter runs in own goroutine and function returns on all. The returned error depends on Pipefail value
true (the default) - returns nil if none of commands fail, otherwise returns all errors in a pipe in a slice. If the first failure is Error, then it's Code is returned. otherwise code 1 is used.
false - returns error of a last command. If the error is nil, then result of the call is nil. for non nil errors, it returns a slice of all errors and a code or 1 depending on a type of last error.
type StandardIO ¶
type StandardIO[T any] interface { Stdin() gio.Reader[T] Stdout() gio.Writer[T] Stderr() io.Writer }
pipe.StandardIO is a standard unix-like type safe input and output defines three streams
stdin - from which can be read stdout - to which results should be written stderr - standard io.Writer for errors, debugs and so