1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package process
import (
"io"
"os"
"os/exec"
"time"
)
type Commander interface {
Start() error
Wait() error
Process() *os.Process
}
type CommandOptions struct {
Dir string
Env []string
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
Logger Logger
GracefulKillTimeout time.Duration
ForceKillTimeout time.Duration
UseWindowsLegacyProcessStrategy bool
}
type osCmd struct {
internal *exec.Cmd
options CommandOptions
}
// NewOSCmd creates a new implementation of Commander using the os.Cmd from
// os/exec.
func NewOSCmd(executable string, args []string, options CommandOptions) Commander {
c := exec.Command(executable, args...)
c.Dir = options.Dir
c.Env = options.Env
c.Stdin = options.Stdin
c.Stdout = options.Stdout
c.Stderr = options.Stderr
return &osCmd{
internal: c,
options: options,
}
}
func (c *osCmd) Start() error {
setProcessGroup(c.internal, c.options.UseWindowsLegacyProcessStrategy)
return c.internal.Start()
}
func (c *osCmd) Wait() error {
return c.internal.Wait()
}
func (c *osCmd) Process() *os.Process {
return c.internal.Process
}
|