2023-09-27 09:04:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 08:43:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 09:04:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|