-
Notifications
You must be signed in to change notification settings - Fork 6
/
exec.go
76 lines (64 loc) · 1.17 KB
/
exec.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
package tweed
import (
"fmt"
"io"
"os/exec"
)
type Exec struct {
Run string
Env []string
Stdout chan string
Stderr chan string
Done chan int
}
func (e Exec) drain(out chan string, in io.Reader, buf []byte) {
for {
n, err := in.Read(buf)
if err != nil {
if err == io.EOF {
return
}
out <- fmt.Sprintf("---\nERROR: %s\n", err)
return
}
out <- string(buf[:n])
}
}
func run1(e Exec) ([]byte, error) {
return e.run1()
}
func (e Exec) run1() ([]byte, error) {
cmd := exec.Command(e.Run)
cmd.Dir = "/"
cmd.Env = e.Env
cmd.Stderr = nil
return cmd.Output()
}
func run(e Exec) error {
return e.run()
}
// FIXME this elaborateness is not needed anymore
func (e Exec) run() error {
cmd := exec.Command(e.Run)
cmd.Dir = "/"
cmd.Env = e.Env
cmd.Stderr = nil
fd1, err := cmd.StdoutPipe()
if err != nil {
return err
}
fd2, err := cmd.StderrPipe()
if err != nil {
return err
}
go e.drain(e.Stdout, fd1, make([]byte, 8192))
go e.drain(e.Stderr, fd2, make([]byte, 8192))
fmt.Printf("running `%s'\n", e.Run)
cmd.Start()
err = cmd.Wait()
if cmd.ProcessState.Exited() {
e.Done <- cmd.ProcessState.ExitCode()
}
close(e.Done)
return err
}