-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
88 lines (78 loc) · 1.7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// +build !windows,!plan9
package exec_test
import (
"log"
"os"
"github.com/pkg/exec"
)
func ExampleCmd_Run() {
cmd := exec.Command("git", "status")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func ExampleCmd_Run_dir() {
cmd := exec.Command("git", "status")
// change working directory to /tmp and run git status.
err := cmd.Run(exec.Dir("/tmp"))
if err != nil {
log.Fatal(err)
}
}
func ExampleCmd_Run_stdout() {
cmd := exec.Command("git", "status")
// change working directory to /tmp, pass os.Stdout to the child
// and run git status.
err := cmd.Run(
exec.Dir("/tmp"),
exec.Stdout(os.Stdout),
)
if err != nil {
log.Fatal(err)
}
}
func ExampleCmd_Run_before() {
cmd := exec.Command("/usr/sleep", "60s")
// set a before and after function
err := cmd.Run(
exec.BeforeFunc(func(c *exec.Cmd) error {
log.Println("About to call:", c.Args)
return nil
}),
exec.AfterFunc(func(c *exec.Cmd) error {
log.Println("Finised calling:", c.Args)
return nil
}),
)
if err != nil {
log.Fatal(err)
}
}
func ExampleCmd_Output_dir() {
cmd := exec.Command("git", "status")
// change working directory to /tmp, run git status and
// capture the stdout of the child.
out, err := cmd.Output(exec.Dir("/tmp"))
if err != nil {
log.Fatal(err)
}
log.Printf("%s", out)
}
func ExampleCmd_Start() {
// start httpd server and redirect the childs stderr
// to the parent's stdout.
cmd := exec.Command("httpd")
err := cmd.Start(exec.Stderr(os.Stdout))
if err != nil {
log.Fatal(err)
}
}
func ExampleCmd_Start_Setenv() {
// set or overwrite an environment variable.
cmd := exec.Command("go")
err := cmd.Start(exec.Setenv("GOPATH", "/foo"))
if err != nil {
log.Fatal(err)
}
}