package module
Version: v0.0.4 Latest Latest

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

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 10 Imported by: 0




Various useful tools for running and pipe-ing stuff outside of Go.

This library makes it very easy to execute complex sequences of executables mixing both go-specific filters and commands.

For example it is possible to run the following:

	res, err := RunPipe(input, "gzip", "-9")
	if err != nil {
		t.Errorf("failed to run test: %s", err)

	res, err = gzip.NewReader(res)
	if err != nil {
		t.Errorf("failed to run test: %s", err)

Reading from res in that example will return the exact same bytes as input, after having been compressed once, then decompressed.

If the command fails, the final Read() call will return the failure code, and allows correctly catching any problem (by default, go os/exec will only return the error when calling Wait(), which may result in errors not being catched).


There are a lot of zombie threads

This means Wait() was not called. If using a method returning a pipe, you need to read the pipe to EOF in order for resources to be cleared. Another option is to call defer pipe.Close() in order to ensure resources are freed.

Close() will return quickly and kill the process, however if you want to wait and give the process some time, defer pipe.CloseWait(ctx) can be used. If the context has a deadline the process will be killed as per the deadline.




This section is empty.


View Source
var (
	ErrCommandMissing = errors.New("command is missing")


func Reap added in v0.0.4

func Reap() error

func Run

func Run(arg ...string) error

Run is a very simple invokation of command run, with output forwarded to stdout

func RunGet

func RunGet(arg ...string) ([]byte, error)

RunGet executes the command and returns its output as a buffer

func RunJson

func RunJson(obj interface{}, arg ...string) error

RunJson executes the command and applies its output to the specified object, parsing json data

func RunWrite

func RunWrite(r io.Reader, arg ...string) error

RunWrite executed the command and passes r as its input

func Sh

func Sh(cmd string) error

Sh runs a shell command (linux only)

func ShQuote

func ShQuote(s string) string


type Pipe added in v0.0.4

type Pipe interface {
	CloseWait(ctx context.Context) error

func RunPipe

func RunPipe(r io.Reader, arg ...string) (Pipe, error)

RunPipe runs a command, connecting both ends

func RunRead

func RunRead(arg ...string) (Pipe, error)

RunRead executes and returns the command's output as a stream

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL