databricks-cli/libs/process/opts.go

79 lines
1.5 KiB
Go
Raw Normal View History

package process
import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
)
type execOption func(context.Context, *exec.Cmd) error
func WithEnv(key, value string) execOption {
return func(ctx context.Context, c *exec.Cmd) error {
v := fmt.Sprintf("%s=%s", key, value)
c.Env = append(c.Env, v)
return nil
}
}
func WithEnvs(envs map[string]string) execOption {
return func(ctx context.Context, c *exec.Cmd) error {
for k, v := range envs {
err := WithEnv(k, v)(ctx, c)
if err != nil {
return err
}
}
return nil
}
}
func WithDir(dir string) execOption {
return func(_ context.Context, c *exec.Cmd) error {
c.Dir = dir
return nil
}
}
func WithStdoutPipe(dst *io.ReadCloser) execOption {
return func(_ context.Context, c *exec.Cmd) error {
outPipe, err := c.StdoutPipe()
if err != nil {
return err
}
*dst = outPipe
return nil
}
}
func WithStdinReader(src io.Reader) execOption {
return func(_ context.Context, c *exec.Cmd) error {
c.Stdin = src
return nil
}
}
func WithStderrWriter(dst io.Writer) execOption {
return func(_ context.Context, c *exec.Cmd) error {
c.Stderr = dst
return nil
}
}
func WithStdoutWriter(dst io.Writer) execOption {
return func(_ context.Context, c *exec.Cmd) error {
c.Stdout = dst
return nil
}
}
func WithCombinedOutput(buf *bytes.Buffer) execOption {
return func(_ context.Context, c *exec.Cmd) error {
c.Stdout = io.MultiWriter(buf, c.Stdout)
c.Stderr = io.MultiWriter(buf, c.Stderr)
return nil
}
}