-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_test.go
111 lines (101 loc) · 1.92 KB
/
pipe_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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cmdio_test
import (
"errors"
"fmt"
"log"
"os"
"strings"
"testing/iotest"
"lesiw.io/cmdio"
"lesiw.io/cmdio/sys"
"lesiw.io/prefix"
)
func ExamplePipe_echo() {
cmdio.Trace = prefix.NewWriter("+ ", os.Stdout)
rnr := sys.Runner()
err := cmdio.Pipe(
rnr.Command("echo", "hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
if err != nil {
log.Fatal(err)
}
// Output:
// + echo 'hello world' | tr a-z A-Z
// HELLO WORLD
}
func ExamplePipe_reader() {
cmdio.Trace = prefix.NewWriter("+ ", os.Stdout)
rnr := sys.Runner()
err := cmdio.Pipe(
strings.NewReader("hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
if err != nil {
log.Fatal(err)
}
// Output:
// + <*strings.Reader> | tr a-z A-Z
// HELLO WORLD
}
func ExampleMustPipe() {
rnr := sys.Runner()
cmdio.MustPipe(
strings.NewReader("hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
// Output:
// HELLO WORLD
}
func ExampleMustPipe_panic() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
rnr := sys.Runner()
cmdio.MustPipe(
iotest.ErrReader(errors.New("some error")),
rnr.Command("tr", "a-z", "A-Z"),
)
// Output:
// some error
//
// <*iotest.errReader> | <- some error
// tr a-z A-Z
}
func ExampleMustGetPipe() {
rnr := sys.Runner()
r := cmdio.MustGetPipe(
strings.NewReader("hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
fmt.Println("out:", r.Out)
fmt.Println("code:", r.Code)
// Output:
// out: HELLO WORLD
// code: 0
}
func ExampleMustGetPipe_panic() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
rnr := sys.Runner()
_ = cmdio.MustGetPipe(
// Use busybox ls to normalize output.
rnr.Command("busybox", "ls", "/bad_directory"),
rnr.Command("tr", "a-z", "A-Z"),
)
// Output:
// exit status 1
//
// busybox ls /bad_directory | <- exit status 1
// tr a-z A-Z
//
// out: <empty>
// log:
// ls: /bad_directory: No such file or directory
// code: 0
}