pipeline

package module
v0.0.0-...-c5d633d Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2019 License: BSD-3-Clause Imports: 5 Imported by: 3

README

pipeline

Golang exec pipeline

Documentation

Index

Constants

View Source
const ErrCodeNil int = 1000

Variables

This section is empty.

Functions

func Run

func Run(cmds ...*exec.Cmd) (stdout, stderr *bytes.Buffer, finErr error)

Run strings together the given exec.Cmd commands in a similar fashion to the

Unix pipeline. Each command's standard output is connected to the standard
input of the next command, and the output of the final command in the
pipeline is returned, along with the collected standard error of all cmds
and the first error found (if any).

To provide input to the pipeline, assign an io.Reader to the first's Stdin.

Pipeline exit code:
 if all cmds start ok and exit zero, finErr=nil, otherwise finErr assigned
 to a pipeline.Error struct, which Code is value of the last (rightmost)
 command to exit with a non-zero status. like Bash set -o pipefail.

Examples:
 1:
     stdout, stderr, err := pipeline.Run(&os.Stderr, exec.Command("ls", "-alh"))
     if err == nil {
         fmt.Println("stdout:", stdout.String())
     } else {
         fmt.Printf("exit_code: %d\n", err.(*pipeline.Error).Code)
         fmt.Printf("error: %v", err.(*pipeline.Error).Err)
     }

 2:
     stdout, _, err := pipeline.Run(&stderr, exec.Command("ls", "-alh"),
         exec.Command("cat"))

 3:
     cmd := exec.Command("cat")
     cmd.Stdin = os.Stdin // to read input from user
     stdout, _, _ := pipeline.Run(&cmd, exec.Command("tr", "a-z", "A-Z"))

Types

type Error

type Error struct {
	Code int
	Err  error
}

func (*Error) Error

func (e *Error) Error() string

implement interface error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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