Documentation ¶
Overview ¶
package pipe implements unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go stream of bytes which can be connected together like unix allows.
It is build as a tiny wrapper on top of gio/pipe as it uses standard io.Reader and io.Writer so it can integrate into idiomatic Go code more seamlessly.
Example ¶
package main import ( "bufio" "bytes" "context" "fmt" "io" "log" "os" "strings" . "codeberg.org/gonix/gio/unix" ) type Cat struct { cat []byte } func (c Cat) Run(ctx context.Context, stdio StandardIO) error { buf := bytes.NewBuffer(c.cat) _, err := io.Copy(stdio.Stdout(), buf) return err } // CountLines count a number of read lines type CountLines struct{} func (c CountLines) Run(ctx context.Context, stdio StandardIO) error { counter := 0 r := bufio.NewScanner(stdio.Stdin()) for r.Scan() { counter++ } if r.Err() != nil { return r.Err() } fmt.Fprintf(stdio.Stdout(), "%d\n", counter) return nil } type Fail struct { err error } func (c Fail) Run(context.Context, StandardIO) error { return c.err } func main() { ctx := context.Background() cat := Cat{ cat: []byte("three\nsmall\npigs\n"), } wc := CountLines{} var out strings.Builder stdio := NewStdio( nil, &out, os.Stderr, ) // an equivalent of cat | wc -l err := NewLine().Run(ctx, stdio, cat, wc) if err != nil { log.Fatal(err) } fmt.Println(out.String()) }
Output: 3
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
// contains filtered or unexported fields
}
Exec is a wrapper of os/exec.Cmd providing a Filter compatible interface
func NewCmd ¶
NewCmd wraps exec.Cmd. The intended usage is
cmd := NewCmd(exec.Command("go", "version"))
func (Cmd) Run ¶
func (c Cmd) Run(ctx context.Context, stdio StandardIO) error
Run implements Filter interface for Cmd wrapper. It creates a _new_ instance of exec.Command under a hood, so any previous state is ignored here.
Returns a pipe.Error if Run results in *exec.ExitError. Code is ExitCode and Err is the *exec.ExitError