2017-09-01 14:54:03 -04:00
|
|
|
package icmd
|
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
import (
|
|
|
|
"io"
|
2020-02-22 12:12:14 -05:00
|
|
|
"os"
|
2018-11-14 06:28:16 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-09-01 14:54:03 -04:00
|
|
|
// CmdOp is an operation which modified a Cmd structure used to execute commands
|
|
|
|
type CmdOp func(*Cmd)
|
2018-11-14 06:28:16 -05:00
|
|
|
|
|
|
|
// WithTimeout sets the timeout duration of the command
|
|
|
|
func WithTimeout(timeout time.Duration) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Timeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEnv sets the environment variable of the command.
|
|
|
|
// Each arguments are in the form of KEY=VALUE
|
|
|
|
func WithEnv(env ...string) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Env = env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dir sets the working directory of the command
|
|
|
|
func Dir(path string) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Dir = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStdin sets the standard input of the command to the specified reader
|
|
|
|
func WithStdin(r io.Reader) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Stdin = r
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
|
2023-07-29 15:01:34 -04:00
|
|
|
// WithStdout sets the standard output of the command to the specified writer
|
|
|
|
func WithStdout(w io.Writer) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Stdout = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStderr sets the standard error of the command to the specified writer
|
|
|
|
func WithStderr(w io.Writer) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.Stderr = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
// WithExtraFile adds a file descriptor to the command
|
|
|
|
func WithExtraFile(f *os.File) CmdOp {
|
|
|
|
return func(c *Cmd) {
|
|
|
|
c.ExtraFiles = append(c.ExtraFiles, f)
|
|
|
|
}
|
|
|
|
}
|