-
Notifications
You must be signed in to change notification settings - Fork 2k
/
os_exec.go
81 lines (65 loc) · 1.27 KB
/
os_exec.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
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
argNum := len(os.Args)
if argNum < 2 {
log.Printf("Usage:%s command\n", os.Args[0])
os.Exit(1)
}
arg := []string{}
if argNum > 2 {
arg = os.Args[2:]
}
mainOutput(UsePipe(os.Args[1], arg...))
}
func mainOutput(out []byte, err error) {
if err != nil {
log.Fatal(err)
}
fmt.Printf("The output of command %q is\n%s\n", os.Args[1], out)
}
// 直接给 Cmd.Stdout 赋值
func FillStd(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
var out = new(bytes.Buffer)
cmd.Stdout = out
cmd.Stderr = out
err := cmd.Run()
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func UseOutput(name string, arg ...string) ([]byte, error) {
return exec.Command(name, arg...).Output()
}
// 使用 Pipe
func UsePipe(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err = cmd.Start(); err != nil {
return nil, err
}
var out = make([]byte, 0, 1024)
for {
tmp := make([]byte, 128)
n, err := stdout.Read(tmp)
out = append(out, tmp[:n]...)
if err != nil {
break
}
}
if err = cmd.Wait(); err != nil {
return nil, err
}
return out, nil
}