-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask.go
66 lines (54 loc) · 909 Bytes
/
task.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
package tweed
import (
"bytes"
"github.com/tweedproject/tweed/random"
)
type task struct {
id string
stdout bytes.Buffer
stderr bytes.Buffer
done bool
exited bool
rc int
}
func (t *task) Done() bool {
return true
}
func (t *task) ExitCode() int {
return 0
}
func (t *task) OK() bool {
return true
}
func (t *task) Stdout() string {
return t.stdout.String()
}
func (t *task) Stderr() string {
return t.stderr.String()
}
func background(e Exec, fn func()) *task {
e.Stdout = make(chan string, 0)
e.Stderr = make(chan string, 0)
e.Done = make(chan int, 1)
t := &task{id: random.ID("t")}
go func() {
for s := range e.Stdout {
t.stdout.Write([]byte(s))
}
}()
go func() {
for s := range e.Stderr {
t.stderr.Write([]byte(s))
}
}()
go func() {
for rc := range e.Done {
t.exited = true
t.rc = rc
}
t.done = true
fn()
}()
go e.run()
return t
}