/
example_test.go
66 lines (61 loc) · 2.09 KB
/
example_test.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 cmdplx_test
import (
"fmt"
"io"
"os/exec"
"github.com/v2e4lisp/cmdplx"
)
func Example() {
var output [2]string
cmds := []*exec.Cmd{
exec.Command("sh", "-c", "echo stderr 1>&2"),
exec.Command("sh", "-c", "echo stdout"),
}
closed := 0
lines, started, exited := cmdplx.Start(cmds)
for {
select {
case line := <-lines:
if line == nil {
lines = nil
if closed++; closed == 3 {
goto DONE
}
break
}
if err := line.Err(); err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
output[line.From()-1] = line.Text()
case status := <-started:
if status == nil {
started = nil
if closed++; closed == 3 {
goto DONE
}
break
}
if err := status.Err(); err != nil {
fmt.Println(err)
}
case status := <-exited:
if status == nil {
exited = nil
if closed++; closed == 3 {
goto DONE
}
break
}
if err := status.Err(); err != nil {
fmt.Println(err)
}
}
}
DONE:
fmt.Println(output)
// Output:
// [stdout stderr]
}