-
Notifications
You must be signed in to change notification settings - Fork 0
/
processio.go
56 lines (45 loc) · 872 Bytes
/
processio.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
package main
import (
"io"
"os"
"os/exec"
)
type ProcessIO struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout io.ReadCloser
}
func CreateProcessFromCommand(command string) *ProcessIO {
cmd := exec.Command("bash", "-c", command)
cmd.Stderr = os.Stderr
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
processIO := &ProcessIO{
cmd: cmd,
stdin: stdin,
stdout: stdout,
}
go func(p *ProcessIO) {
defer p.Close()
checkerror(cmd.Start())
checkerror(cmd.Wait())
}(processIO)
return processIO
}
func (p *ProcessIO) Read(data []byte) (int, error) {
return p.stdout.Read(data)
}
func (p *ProcessIO) Write(data []byte) (int, error) {
return p.stdin.Write(data)
}
func (p *ProcessIO) Close() error {
err := p.stdin.Close()
if err != nil {
return err
}
err = p.stdout.Close()
if err != nil {
return err
}
return nil
}