-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl.go
280 lines (266 loc) · 5.6 KB
/
gl.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gl
import (
"bufio"
"bytes"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"unicode"
)
type RunPrinter func(string)
type RunArg struct {
Exe string
Args []string
Dir string
Env []string
Stdin []byte
Timeout int
Stdout RunPrinter
Stderr RunPrinter
}
type RunOut struct {
Stdout string
Stderr string
Error string
}
// Interface to execute the given `RunArg` through `exec.Command`.
// The first return value is a boolean, true indicates success, false otherwise.
// Second value is the standard output of the command.
// Third value is the standard error of the command.
// Fourth value is error string from Run.
func (a RunArg) Run() (bool, RunOut) {
var r bool
/* #nosec G204 */
cmd := exec.Command(a.Exe, a.Args...)
cmd.SysProcAttr = setPgid()
if a.Dir != "" {
cmd.Dir = a.Dir
}
if a.Env != nil || len(a.Env) > 0 {
cmd.Env = append(os.Environ(), a.Env...)
}
if a.Stdin != nil || len(a.Stdin) > 0 {
cmd.Stdin = bytes.NewBuffer(a.Stdin)
}
var errorStr string
var err error
var stdout string
var stderr string
done := make(chan struct{})
soPipe, soErr := cmd.StdoutPipe()
if soErr != nil {
return false, RunOut{Stdout: "", Stderr: "", Error: soErr.Error()}
}
sePipe, seErr := cmd.StderrPipe()
if seErr != nil {
return false, RunOut{Stdout: "", Stderr: "", Error: soErr.Error()}
}
var soStr strings.Builder
var seStr strings.Builder
soBuf := bufio.NewReader(soPipe)
seBuf := bufio.NewReader(sePipe)
go func() {
soLast := false
seLast := false
for {
line, err := soBuf.ReadString('\n')
if err == io.EOF {
soLast = true
}
soStr.WriteString(line)
if a.Stdout != nil {
a.Stdout(line)
}
if soLast {
break
}
if err != nil {
continue
}
}
for {
line, err := seBuf.ReadString('\n')
if err == io.EOF {
seLast = true
}
seStr.WriteString(line)
if a.Stderr != nil {
a.Stderr(line)
}
if seLast {
break
}
if err != nil {
continue
}
}
done <- struct{}{}
}()
err = cmd.Start()
if err != nil {
r = false
errorStr = err.Error()
} else {
var timer *time.Timer
if (a.Timeout != 0) && (a.Timeout > 0) {
timer = time.AfterFunc(time.Duration(a.Timeout)*time.Second, func() {
pid := cmd.Process.Pid
switch runtime.GOOS {
case "windows":
_ = cmd.Process.Kill()
default:
sKill(pid)
}
})
}
<-done
err = cmd.Wait()
stdout = soStr.String()
stderr = seStr.String()
if err != nil {
r = false
errorStr = err.Error()
} else {
r = true
}
if (a.Timeout != 0) && (a.Timeout > 0) {
timer.Stop()
}
}
return r, RunOut{Stdout: stdout, Stderr: stderr, Error: errorStr}
}
func IsFile(p string) bool {
info, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func IsDir(p string) bool {
if fi, err := os.Stat(p); err == nil {
if fi.IsDir() {
return true
}
}
return false
}
// Returns a function for walking a path for files.
// Files are read and then contents are written to a strings.Builder pointer.
func PathWalker(sh *strings.Builder) func(string, fs.DirEntry, error) error {
return func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
/* #nosec G304 */
if IsFile(path) {
var file *os.File
var str []byte
file, oerr := os.Open(path)
if err != nil {
return oerr
}
defer file.Close()
str, rerr := io.ReadAll(file)
if err != nil {
return rerr
}
sh.WriteString(string(str)) // length of string and nil err ignored
return file.Close()
}
return nil
}
}
// Reads a file `path` then returns the contents as a string.
// Always returns a string value.
// An empty string "" is returned for errors or nonexistent/unreadable files.
func FileRead(path string) string {
/* #nosec G304 */
if IsFile(path) {
var file *os.File
var str []byte
file, oerr := os.Open(path)
if oerr != nil {
return ""
}
defer file.Close()
str, rerr := io.ReadAll(file)
if rerr != nil {
return ""
}
_ = file.Close()
if len(str) > 0 {
return string(str)
} else {
return ""
}
} else {
return ""
}
}
func FileLines(path string) []string {
var text []string
/* #nosec G304 */
if IsFile(path) {
var file *os.File
file, err := os.Open(path)
if err != nil {
return text
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
text = append(text, scanner.Text())
}
_ = file.Close()
return text
} else {
return text
}
}
func FileGlob(path string) ([]string, error) {
p := ""
for _, r := range path {
if unicode.IsLetter(r) {
p += fmt.Sprintf("[%c%c]", unicode.ToLower(r), unicode.ToUpper(r))
} else {
p += string(r)
}
}
return filepath.Glob(p)
}
// Insert string argument #2 into index `i` of first argument `a`.
func InsertStr(a []string, b string, i int) []string {
a = append(a, "")
copy(a[i+1:], a[i:]) // number of elements copied ignored
a[i] = b
return a
}
// Prefix string `s` with pipes "│".
// Used to "prettify" command line output.
// Returns new string.
func PipeStr(prefix string, str string) string {
replacement := fmt.Sprintf("\n %s │ ", prefix)
str = strings.Replace(str, "\n", replacement, -1)
return fmt.Sprintf(" %s │\n %s │ %s\n %s │", prefix, prefix, str, prefix)
}
// Writes the string `s` to the file `path`.
// It returns any error encountered, nil otherwise.
func StringToFile(path string, s string) error {
fo, err := os.Create(path)
if err != nil {
return err
}
defer fo.Close()
_, err = io.Copy(fo, strings.NewReader(s))
if err != nil {
return err
}
return fo.Close()
}