exechelper

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2020 License: Apache-2.0 Imports: 8 Imported by: 16

README

exechelper is a simple wrapper around standard go 'exec' that tries to follow the spirit of its API closely

Its main features are:

  1. It allows you to pass strings containing the command you want executed instead of an array of args.
  2. It's Start() returns an errCh that will get zero or one errors, and be closed after the command has finished running
  3. You can use the WithXYZ pattern to do things like customize things like Stdout, Stdin, StdError, Dir, and Env variables

Run Examples:

if err := exechelper.Run("go list -m");err != nil {...}

if err := exechelper.Run("go list -m",WithEnv(os.Environs()));err != nil {...}

if err := exechelper.Run("go list -m",WithDir(dir)); err != nil {...}

outputBuffer := bytes.NewBuffer([]byte{})
errBuffer := bytes.NewBuffer([]bytes{})
if err := exechelper.Run("go list -m",WithStdout(outputBuffer),WithStderr(errBuffer));err != nil {...}

Start Examples

ctx,cancel := context.WithCancel(context.Background())
errCh := exechelper.Start(startContext,"spire-server run",WithContext(ctx))

// cancel() - will stop the running cmd if its still running
// <-errCh - provides any error from the Start.  Will always have zero or one err, so <-errCh will block until Start has finished
//           For non-zero Exit code will return a exec.ExitError
//           Note: cancel() will almost always result in a non-zero exit code

Similarly, exechelper.Output(...), exechelper.CombinedOutput(...) are provided.

Documentation

Overview

Package exechelper provides a wrapper around cmd.Exec that makes it easier to use

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombinedOutput

func CombinedOutput(cmdStr string, options ...*Option) ([]byte, error)

CombinedOutput - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.CombinedOutput and returns the resulting output as []byte and error

func Output

func Output(cmdStr string, options ...*Option) ([]byte, error)

Output - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.Output and returns the resulting output as []byte and error

func Run

func Run(cmdStr string, options ...*Option) error

Run - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.Run and returns the resulting error

func Start

func Start(cmdStr string, options ...*Option) <-chan error

Start - Creates an exec.Cmd cmdStr. Runs exec.Cmd.Start.

Types

type CmdFunc

type CmdFunc func(cmd *exec.Cmd) error

CmdFunc function for setting exec.Cmd parameters.

type Option

type Option struct {
	// Context - context (if any) for running the exec.Cmd
	Context context.Context
	// CmdFunc to be applied to the exec.Cmd
	CmdOption CmdFunc
}

Option - expresses optional behavior for exec.Cmd

func CmdOption

func CmdOption(cmdFunc CmdFunc) *Option

CmdOption - convenience function for producing an Option that only has an Option.CmdOption

func WithArgs

func WithArgs(args ...string) *Option

WithArgs - appends additional args to cmdStr

useful for ensuring correctness when you start from
args []string rather than from a cmdStr to be parsed

func WithContext

func WithContext(ctx context.Context) *Option

WithContext - option for setting the context.Context for running the exec.Cmd

func WithDir

func WithDir(dir string) *Option

WithDir - Option that will create the requested dir if it does not exist and set exec.Cmd.Dir = dir

func WithEnvKV

func WithEnvKV(envs ...string) *Option

WithEnvKV - add entries to exec.Cmd as a series key,value pairs in a list of strings Existing instances of 'key' will be overwritten Example: WithEnvKV(key1,value2,key2,value2...)

func WithEnvMap

func WithEnvMap(envMap map[string]string) *Option

WithEnvMap - add entries to exec.Cmd from envMap Existing instances of 'key' will be overwritten Example: WithEnvKV(map[string]string{key1:value1,key2:value2})

func WithEnvirons

func WithEnvirons(environs ...string) *Option

WithEnvirons - add entries to exec.Cmd.Env as a series of "key=value" strings Example: WithEnvirons("key1=value1","key2=value2",...)

func WithStderr

func WithStderr(writer io.Writer) *Option

WithStderr - option to provide a writer to receive exec.Cmd.Stderr

if multiple WithStderr options are received, they are combined
with an io.Multiwriter

func WithStdin

func WithStdin(reader io.Reader) *Option

WithStdin - option to set exec.Cmd.Stdout

func WithStdout

func WithStdout(writer io.Writer) *Option

WithStdout - option to provide a writer to receive exec.Cmd.Stdout

if multiple WithStdout options are received, they are combined
with an io.Multiwriter

Jump to

Keyboard shortcuts

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