Skip to content

Commit

Permalink
Merge pull request #2 from edwarnicke/multiwriter
Browse files Browse the repository at this point in the history
Enable WithStdout and WithStderr multiplexing
  • Loading branch information
edwarnicke authored May 6, 2020
2 parents 7dc61be + 65d8938 commit a22a20b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
8 changes: 4 additions & 4 deletions exechelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"github.com/google/shlex"
)

// Run - Creates a exec.Prepare using cmdStr. Runs exec.Prepare.Run and returns the resulting error
// Run - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.Run and returns the resulting error
func Run(cmdStr string, options ...*Option) error {
return <-Start(cmdStr, options...)
}

// Start - Creates an exec.Prepare cmdStr. Runs exec.Prepare.Start.
// Start - Creates an exec.Cmd cmdStr. Runs exec.Cmd.Start.
func Start(cmdStr string, options ...*Option) <-chan error {
errCh := make(chan error, 1)

Expand Down Expand Up @@ -87,7 +87,7 @@ func Start(cmdStr string, options ...*Option) <-chan error {
return errCh
}

// Output - Creates a exec.Prepare using cmdStr. Runs exec.Prepare.Output and returns the resulting output as []byte and error
// Output - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.Output and returns the resulting output as []byte and error
func Output(cmdStr string, options ...*Option) ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
options = append(options, WithStdout(buffer))
Expand All @@ -97,7 +97,7 @@ func Output(cmdStr string, options ...*Option) ([]byte, error) {
return buffer.Bytes(), nil
}

// CombinedOutput - Creates a exec.Prepare using cmdStr. Runs exec.Prepare.CombinedOutput and returns the resulting output as []byte and error
// CombinedOutput - Creates a exec.Cmd using cmdStr. Runs exec.Cmd.CombinedOutput and returns the resulting output as []byte and error
func CombinedOutput(cmdStr string, options ...*Option) ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
options = append(options, WithStdout(buffer), WithStderr(buffer))
Expand Down
19 changes: 19 additions & 0 deletions exechelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ func TestWithStdin(t *testing.T) {
assert.Equal(t, testStr, strings.TrimSpace(string(b)))
}

func TestWithStdout(t *testing.T) {
buffer := bytes.NewBuffer([]byte{})
output, err := exechelper.Output("ls", exechelper.WithStdout(buffer))
assert.NoError(t, err)
assert.Equal(t, string(output), buffer.String())
}

func TestWithStderr(t *testing.T) {
buffer1 := bytes.NewBuffer([]byte{})
buffer2 := bytes.NewBuffer([]byte{})
err := exechelper.Run("ls fdhdhdhahdr",
exechelper.WithStderr(buffer1),
exechelper.WithStderr(buffer2),
)
assert.Error(t, err)
assert.True(t, buffer1.Len() > 0)
assert.Equal(t, buffer1.String(), buffer2.String())
}

func TestWithEnvMap(t *testing.T) {
// Try one
key1 := "key1"
Expand Down
30 changes: 21 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func WithArgs(args ...string) *Option {
})
}

// WithDir - Option that will create the requested dir if it does not exist and set exec.Prepare.Dir = dir
// WithDir - Option that will create the requested dir if it does not exist and set exec.Cmd.Dir = dir
func WithDir(dir string) *Option {
return CmdOption(func(cmd *exec.Cmd) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
Expand All @@ -70,31 +70,43 @@ func WithDir(dir string) *Option {
})
}

// WithStdin - option to set exec.Prepare.Stdout
// WithStdin - option to set exec.Cmd.Stdout
func WithStdin(reader io.Reader) *Option {
return CmdOption(func(cmd *exec.Cmd) error {
cmd.Stdin = reader
return nil
})
}

// WithStdout - option to set exec.Prepare.Stdout
// WithStdout - option to provide a writer to receive exec.Cmd.Stdout
// if multiple WithStdout options are received, they are combined
// with an io.Multiwriter
func WithStdout(writer io.Writer) *Option {
return CmdOption(func(cmd *exec.Cmd) error {
cmd.Stdout = writer
if cmd.Stdout == nil {
cmd.Stdout = writer
return nil
}
cmd.Stdout = io.MultiWriter(cmd.Stdout, writer)
return nil
})
}

// WithStderr - option to set exec.Prepare.Stdout
// 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 WithStderr(writer io.Writer) *Option {
return CmdOption(func(cmd *exec.Cmd) error {
cmd.Stderr = writer
if cmd.Stderr == nil {
cmd.Stderr = writer
return nil
}
cmd.Stderr = io.MultiWriter(cmd.Stderr, writer)
return nil
})
}

// WithEnvirons - add entries to exec.Prepare.Env as a series of "key=value" strings
// WithEnvirons - add entries to exec.Cmd.Env as a series of "key=value" strings
// Example: WithEnvirons("key1=value1","key2=value2",...)
func WithEnvirons(environs ...string) *Option {
var envs []string
Expand All @@ -110,7 +122,7 @@ func WithEnvirons(environs ...string) *Option {
return WithEnvKV(envs...)
}

// WithEnvKV - add entries to exec.Prepare as a series key,value pairs in a list of strings
// 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 WithEnvKV(envs ...string) *Option {
Expand All @@ -133,7 +145,7 @@ func WithEnvKV(envs ...string) *Option {
})
}

// WithEnvMap - add entries to exec.Prepare from envMap
// 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 WithEnvMap(envMap map[string]string) *Option {
Expand Down

0 comments on commit a22a20b

Please sign in to comment.