-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
50 lines (44 loc) · 1.27 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
package shell_test
import (
"bytes"
"fmt"
"log"
"strings"
"github.com/keegancsmith/shell"
)
func ExampleSprintf() {
// Generates shell command to find number of go files on a remote machine
host := "foo.com"
findCmd := shell.Sprintf("find . -iname %s | wc -l", "*.go")
remoteCmd := shell.Sprintf("ssh ubuntu@%s %s", host, findCmd)
fmt.Println(remoteCmd)
// Output: ssh [email protected] 'find . -iname '\''*.go'\'' | wc -l'
}
func ExampleSprintf_StringSlice() {
// Support for passing in a slice of arguments
gitcmd := shell.Sprintf("git add %S", []string{"foo.go", "bar.go", "test data"})
fmt.Println(gitcmd)
// Output: git add foo.go bar.go 'test data'
}
func ExampleCommandf() {
out, err := shell.Commandf("echo %s", "hello world").Output()
if err != nil {
log.Fatal(err)
}
fmt.Print(string(out))
// Output: hello world
}
func ExampleCommandf_redirect() {
var stdout, stderr bytes.Buffer
cmd := shell.Commandf("echo %s; echo %s 1>&2", "hello from stdout", "hello from stderr")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println("stdout:", strings.TrimSpace(stdout.String()))
fmt.Println("stderr:", strings.TrimSpace(stderr.String()))
// Output: stdout: hello from stdout
// stderr: hello from stderr
}