deputy

package module
v0.0.0-...-5e10e7d Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2016 License: MIT Imports: 5 Imported by: 7

README

deputy GoDoc Build Status

deputy is a go package that adds smarts on top of os/exec

deputy-sm

image: creative commons, © MatsuRD

Example

// Make a new deputy that'll return the data written to stderr as the error
// message, log everything written to stdout to this application's log,  and
// timeout after 30 seconds.
cancel := make(chan struct{})
go func() {
    <-time.After(time.Second * 30)
    close(cancel)
}()

d := deputy.Deputy{
    Errors:    deputy.FromStderr,
    StdoutLog: func(b []byte) { log.Print(string(b)) },
    Cancel:    cancel,
}
if err := d.Run(exec.Command("foo")); err != nil {
    log.Print(err)
}

type Deputy

type Deputy struct {
	// Cancel, when closed, will cause the command to close.
	Cancel <-chan struct{}
    // Errors describes how errors should be handled.
    Errors ErrorHandling
    // StdoutLog takes a function that will receive lines written to stdout from
    // the command (with the newline elided).
    StdoutLog func([]byte)
    // StdoutLog takes a function that will receive lines written to stderr from
    // the command (with the newline elided).
    StderrLog func([]byte)
    // contains filtered or unexported fields
}

Deputy is a type that runs Commands with advanced options not available from os/exec. See the comments on field values for details.

func (Deputy) Run
func (d Deputy) Run(cmd *exec.Cmd) error

Run starts the specified command and waits for it to complete. Its behavior conforms to the Options passed to it at construction time.

Note that, like cmd.Run, Deputy.Run should not be used with StdoutPipe or StderrPipe.

type ErrorHandling

type ErrorHandling int

ErrorHandling is a flag that tells Deputy how to handle errors running a command. See the values below for the different modes.

const (
    // DefaultErrs represents the default handling of command errors - this
    // simply returns the error from Cmd.Run()
    DefaultErrs ErrorHandling = iota

    // FromStderr tells Deputy to convert the stderr output of a command into
    // the text of an error, if the command exits with an error.
    FromStderr

    // FromStdout tells Deputy to convert the stdout output of a command into
    // the text of an error, if the command exits with an error.
    FromStdout
)

Documentation

Overview

Package deputy provides more advanced options for running commands.

Example
package main

import (
	"log"
	"os/exec"
	"time"

	"npf.io/deputy"
)

func main() {
	cancel := make(chan struct{})
	go func() {
		<-time.After(time.Second * 30)
		close(cancel)
	}()

	// Make a new deputy that'll return the data written to stderr as the error
	// message, log everything written to stdout to this application's log,  and
	// timeout after 30 seconds.
	d := deputy.Deputy{
		Errors:    deputy.FromStderr,
		StdoutLog: func(b []byte) { log.Print(string(b)) },
		Cancel:    cancel,
	}
	if err := d.Run(exec.Command("foo")); err != nil {
		log.Print(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deputy

type Deputy struct {
	// Cancel, when closed, will cause the command to close.
	Cancel <-chan struct{}
	// Errors describes how errors should be handled.
	Errors ErrorHandling
	// StdoutLog takes a function that will receive lines written to stdout from
	// the command (with the newline elided).
	StdoutLog func([]byte)
	// StdoutLog takes a function that will receive lines written to stderr from
	// the command (with the newline elided).
	StderrLog func([]byte)
	// contains filtered or unexported fields
}

Deputy is a type that runs Commands with advanced options not available from os/exec. See the comments on field values for details.

func (Deputy) Run

func (d Deputy) Run(cmd *exec.Cmd) error

Run starts the specified command and waits for it to complete. Its behavior conforms to the Options passed to it at construction time.

Note that, like cmd.Run, Deputy.Run should not be used with StdoutPipe or StderrPipe.

type ErrorHandling

type ErrorHandling int

ErrorHandling is a flag that tells Deputy how to handle errors running a command. See the values below for the different modes.

const (
	// DefaultErrs represents the default handling of command errors - this
	// simply returns the error from Cmd.Run()
	DefaultErrs ErrorHandling = iota

	// FromStderr tells Deputy to convert the stderr output of a command into
	// the text of an error, if the command exits with an error.
	FromStderr

	// FromStdout tells Deputy to convert the stdout output of a command into
	// the text of an error, if the command exits with an error.
	FromStdout
)

Jump to

Keyboard shortcuts

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