-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (42 loc) · 912 Bytes
/
example_test.go
File metadata and controls
50 lines (42 loc) · 912 Bytes
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
package sh
import (
"fmt"
"time"
)
// How to use sh
// Capture a bash command's exit code
func Example_a() {
exitCode, err := WaitCode(RunBash("exit 10"))
fmt.Println(exitCode, err)
// output:
// 10 <nil>
}
// Capture a bash command's exit code
func Example_b() {
exitCode, err := WaitCode(RunBash("exit 0"))
fmt.Println(exitCode, err)
// output:
// 0 <nil>
}
// Capture a program's exit code
func Example_c() {
exitCode, err := WaitCode(RunCmd("bash", "-c", "exit 45"))
fmt.Println(exitCode, err)
// output:
// 45 <nil>
}
// Capture other errors
func Example_d() {
exitCode, _ := WaitCode(RunCmd("non-exisistent", "-c", "exit 45"))
fmt.Println(exitCode)
// output:
// -1
}
// Timeout a long-running programs
func Example_e() {
cmd, err := RunBash("sleep 5")
exitCode, err := WaitCodeTimeout(cmd, err, 1*time.Second)
fmt.Println(exitCode, err)
// output:
// -1 process timed out
}