forked from theairkit/runcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.go
91 lines (73 loc) · 2.07 KB
/
local.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package runcmd
import (
"io"
"os/exec"
"syscall"
)
// LocalCmd is implementation of CmdWorker interface for local commands
type LocalCmd struct {
cmd *exec.Cmd
}
// Local is implementation of Runner interface for local commands
type Local struct{}
// NewLocalRunner creates instance of local runner
func NewLocalRunner() (*Local, error) {
return &Local{}, nil
}
// Command creates worker for current command execution
func (local *Local) Command(name string, arg ...string) CmdWorker {
return &LocalCmd{
cmd: exec.Command(name, arg...),
}
}
// stub to suite interface
func (local *LocalCmd) CmdError() error {
return nil
}
// Run executes current command and retuns output splitted by newline
func (cmd *LocalCmd) Run() error {
return run(cmd)
}
func (cmd *LocalCmd) Output() ([]byte, []byte, error) {
return output(cmd)
}
// Start begins current command execution
func (cmd *LocalCmd) Start() error {
return cmd.cmd.Start()
}
// Wait returns error after command execution if current command return nonzero
// exit code
func (cmd *LocalCmd) Wait() error {
return cmd.cmd.Wait()
}
// StdinPipe returns stdin of current worker
func (cmd *LocalCmd) StdinPipe() (io.WriteCloser, error) {
return cmd.cmd.StdinPipe()
}
// StdoutPipe returns stdout of current worker
func (cmd *LocalCmd) StdoutPipe() (io.Reader, error) {
return cmd.cmd.StdoutPipe()
}
// StderrPipe returns stderr of current worker
func (cmd *LocalCmd) StderrPipe() (io.Reader, error) {
return cmd.cmd.StderrPipe()
}
// SetStdout is for binding your own writer to worker stdout
func (cmd *LocalCmd) SetStdout(buffer io.Writer) {
cmd.cmd.Stdout = buffer
}
// SetStderr is for binding your own writer to worker stderr
func (cmd *LocalCmd) SetStderr(buffer io.Writer) {
cmd.cmd.Stderr = buffer
}
func (cmd *LocalCmd) SetStdin(reader io.Reader) {
cmd.cmd.Stdin = reader
}
// GetArgs returns cmdline for current worker
func (cmd *LocalCmd) GetArgs() []string {
return cmd.cmd.Args
}
// Signal sends a signal to the Process.
func (cmd *LocalCmd) Signal(signal syscall.Signal) error {
return cmd.cmd.Process.Signal(signal)
}