unix

package
v0.0.0-...-28cf358 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 5 Imported by: 6

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

func NewCmd(cmd *exec.Cmd) Cmd

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

type Filter

type Filter interface {
	Run(context.Context, StandardIO) error
}

type Line

type Line struct {
	pipe.Line[byte]
}

func NewLine

func NewLine() Line

func (Line) Pipefail

func (p Line) Pipefail(b bool) Line

func (Line) Run

func (p Line) Run(ctx context.Context, stdio StandardIO, filters ...Filter) error

type StandardIO

type StandardIO interface {
	Stdin() io.Reader
	Stdout() io.Writer
	Stderr() io.Writer
}

type Stdio

type Stdio struct {
	// contains filtered or unexported fields
}

func NewStdio

func NewStdio(stdin io.Reader, stdout io.Writer, stderr io.Writer) Stdio

func (Stdio) Stderr

func (s Stdio) Stderr() io.Writer

func (Stdio) Stdin

func (s Stdio) Stdin() io.Reader

func (Stdio) Stdout

func (s Stdio) Stdout() io.Writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL