-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
61 lines (48 loc) · 1.13 KB
/
utils.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
package main
import (
"bufio"
"strings"
"bytes"
"os"
"os/exec"
)
func preExecCmd(command []string, buffer *string) *exec.Cmd {
cmd := exec.Command(command[0], command[1:]...)
if buffer != nil {
cmd.Stdin = bytes.NewReader([]byte(*buffer))
} else {
cmd.Stdin = os.Stdin
}
cmd.Stderr = os.Stderr
return cmd
}
func postExecCmd(capture bool, cmd *exec.Cmd) (string, error) {
var stdout bytes.Buffer
if capture {
cmd.Stdout = &stdout
err := cmd.Run()
return strings.TrimSpace(stdout.String()), err
} else {
cmd.Stdout = os.Stdout
err := cmd.Run()
return "", err
}
}
func ExecCmdWithBuffer(command []string, buffer string, capture bool) (string, error) {
cmd := preExecCmd(command, &buffer)
return postExecCmd(capture, cmd)
}
func ExecCmd(command []string, capture bool) (string, error) {
cmd := preExecCmd(command, nil)
return postExecCmd(capture, cmd)
}
func GetResults(out string) ([]string, error) {
reader := strings.NewReader(string(out))
scanner := bufio.NewScanner(reader)
var lines []string
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
return lines, scanner.Err()
}