-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.go
52 lines (43 loc) · 1002 Bytes
/
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
package pgcommands
import (
"bufio"
"errors"
"fmt"
"io"
"os/exec"
)
type ExecOptions struct {
StreamPrint bool
StreamDestination io.Writer
}
func streamExecOutput(out io.ReadCloser, options ExecOptions) (string, error) {
output := ""
reader := bufio.NewReader(out)
for {
line, err := reader.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
return output, nil
}
return output, fmt.Errorf("error reading output: %w", err)
}
if options.StreamPrint {
_, err = fmt.Fprint(options.StreamDestination, line)
if err != nil {
return output, fmt.Errorf("error writing output: %w", err)
}
}
output += line
}
}
func streamOutput(stderrIn io.ReadCloser, opts ExecOptions, result *Result) {
output, err := streamExecOutput(stderrIn, opts)
if err != nil {
result.Error = &ResultError{Err: err, CmdOutput: output}
}
result.Output = output
}
func CommandExist(command string) bool {
_, err := exec.LookPath(command)
return err == nil
}