-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
144 lines (119 loc) · 2.59 KB
/
run.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package run
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os/exec"
)
func RunExe(exe string, parms []string) ([]byte, []byte, error) {
// fmt.Printf("======== parms: %v\n\n", parms)
cmd := exec.Command(exe, parms...)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("StdoutPipe: " + err.Error())
return nil, nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("StderrPipe: ", err.Error())
return nil, nil, err
}
if err := cmd.Start(); err != nil {
fmt.Println("Start: ", err.Error())
return nil, nil, err
}
bytesErr, err := ioutil.ReadAll(stderr)
if err != nil {
fmt.Println("ReadAll stderr: ", err.Error())
return nil, nil, err
}
output, err := ioutil.ReadAll(stdout)
if err != nil {
fmt.Println("ReadAll stdout: ", err.Error())
return nil, nil, err
}
if err := cmd.Wait(); err != nil {
// fmt.Println("Wait: ", err.Error())
return output, bytesErr, err
}
// fmt.Printf("stdout: %s", bytes)
return output, bytesErr, nil
}
func RunExe2(name string, arg ...string) {
// fmt.Printf("run, name=%v\n", name)
cmd := exec.Command(name, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("StdoutPipe: " + err.Error())
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("StderrPipe: ", err.Error())
return
}
if err := cmd.Start(); err != nil {
fmt.Println("Start: ", err.Error())
return
}
bytesErr, err := ioutil.ReadAll(stderr)
if err != nil {
fmt.Println("ReadAll stderr: ", err.Error())
return
}
if len(bytesErr) != 0 {
fmt.Printf("stderr is not nil: %s", bytesErr)
return
}
_, err = ioutil.ReadAll(stdout)
if err != nil {
fmt.Println("ReadAll stdout: ", err.Error())
return
}
if err := cmd.Wait(); err != nil {
fmt.Println("Wait: ", err.Error())
return
}
// fmt.Printf("stdout: %s", bytes)
}
func RunAndChangeDir(stdOutput chan string, errOutput chan string, dir string, exe string, params ...string) error {
cmd := exec.Command(exe, params...)
o, err := cmd.StdoutPipe()
if err != nil {
return err
}
e, err := cmd.StderrPipe()
if err != nil {
return err
}
if len(dir) > 0 {
cmd.Dir = dir
}
err = cmd.Start()
if err != nil {
return err
}
go func(o io.ReadCloser) {
readerO := bufio.NewReader(o)
for {
line, err2 := readerO.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
stdOutput <- line
}
}(o)
go func(e io.ReadCloser) {
readerE := bufio.NewReader(e)
for {
line, err2 := readerE.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
errOutput <- line
}
}(e)
err = cmd.Wait()
return err
}