cmd

package
v0.0.0-...-3970965 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2018 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package cmd provides helper functions for working with the os/exec library.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Output

func Output(cmd *exec.Cmd) ([]string, error)

Output executes the command & returns stdout. Stderr and error exit codes are reported as the returned error.

Example
tempdir, _ := ioutil.TempDir("", "example-output")
defer os.RemoveAll(tempdir)

os.MkdirAll(filepath.Join(tempdir, "sub1"), 0744)
os.MkdirAll(filepath.Join(tempdir, "sub2"), 0744)

out, _ := Output(exec.Command("ls", tempdir))

for _, k := range out {
	fmt.Println(k)
}
Output:

sub1
sub2

func PipeWith

func PipeWith(cmd *exec.Cmd, action func(io.Reader) error) error

PipeWith executes the command & sends the provided event handler the stdout io.Reader. Errors returned from the event handler are forwarded on. Stderr and error exit codes are reported as the returned error.

Example
tempdir, _ := ioutil.TempDir("", "example-pipewith")
defer os.RemoveAll(tempdir)

os.MkdirAll(filepath.Join(tempdir, "sub1"), 0744)
os.MkdirAll(filepath.Join(tempdir, "sub2"), 0744)

PipeWith(exec.Command("ls", tempdir), func(stdout io.Reader) error {
	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}

	return nil
})
Output:

sub1
sub2

func Run

func Run(cmd *exec.Cmd) error

Run executes the command & throws away stdout. Stderr and error exit codes are reported as the returned error.

Example
tempdir, _ := ioutil.TempDir("", "example-run")
defer os.RemoveAll(tempdir)

os.MkdirAll(filepath.Join(tempdir, "sub1"), 0744)
os.MkdirAll(filepath.Join(tempdir, "sub2"), 0744)

if runErr := Run(exec.Command("ls", tempdir)); runErr != nil {
	fmt.Printf("Got error %v", runErr)
}
Output:

Types

type Error

type Error struct {
	Exit   *exec.ExitError
	StdErr []string
}

Error is returned when an exit error is encountered. It also captures the stderr lines from the command.

func (*Error) Error

func (s *Error) Error() string

Jump to

Keyboard shortcuts

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