-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_std_runner_test.go
More file actions
87 lines (79 loc) · 2.05 KB
/
example_std_runner_test.go
File metadata and controls
87 lines (79 loc) · 2.05 KB
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
package run_test
import (
"fmt"
"os"
"github.com/apatters/go-run"
)
func ExampleRun() {
// There is no need for a constructor when running local
// commands when using the standard runner.
fmt.Println("Run ls command.")
stdout, stderr, code, err := run.Run(
"/bin/ls",
"-1",
"/bin/true",
"/bin/false")
if err != nil {
fmt.Printf("Internal error executing ls: %s.\n", err)
os.Exit(1)
}
fmt.Printf("stdout = %q\n", stdout)
fmt.Printf("stderr = %q\n", stderr)
fmt.Printf("exit code = %d\n", code)
fmt.Println()
fmt.Println("Run id command.")
stdout, stderr, code, err = run.Run("/bin/id", "root")
if err != nil {
fmt.Printf("Internal error executing ls: %s.\n", err)
os.Exit(1)
}
fmt.Printf("stdout = %q\n", stdout)
fmt.Printf("stderr = %q\n", stderr)
fmt.Printf("exit code = %d\n", code)
fmt.Println()
// Output:
// Run ls command.
// stdout = "/bin/false\n/bin/true\n"
// stderr = ""
// exit code = 0
//
// Run id command.
// stdout = "uid=0(root) gid=0(root) groups=0(root)\n"
// stderr = ""
// exit code = 0
}
func ExampleShell() {
// There is no need for a constructor when running local
// commands when using the standard runner.
fmt.Println("Run ls command using shell.")
stdout, stderr, code, err := run.Shell("/bin/ls -1 /bin/true /bin/false")
if err != nil {
fmt.Printf("Internal error executing ls: %s.\n", err)
os.Exit(1)
}
fmt.Printf("stdout = %q\n", stdout)
fmt.Printf("stderr = %q\n", stderr)
fmt.Printf("exit code = %d\n", code)
fmt.Println()
// Print out the command to run, then run it.
fmt.Println("Run id command.")
stdout, stderr, code, err = run.Shell("/bin/id root | cut -f1 -d ' '")
if err != nil {
fmt.Printf("Internal error executing ls: %s.\n", err)
os.Exit(1)
}
fmt.Printf("stdout = %q\n", stdout)
fmt.Printf("stderr = %q\n", stderr)
fmt.Printf("exit code = %d\n", code)
fmt.Println()
// Output:
// Run ls command using shell.
// stdout = "/bin/false\n/bin/true\n"
// stderr = ""
// exit code = 0
//
// Run id command.
// stdout = "uid=0(root)\n"
// stderr = ""
// exit code = 0
}