-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
48 lines (42 loc) · 1.15 KB
/
util.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
package jumpboot
import (
"bufio"
"os"
"os/exec"
)
func isDirWritable(path string) bool {
// Attempt to create a temporary file in the specified directory.
tmpFile, err := os.CreateTemp(path, "test-*")
if err != nil {
// If an error occurs, the directory is not writable.
return false
}
fileName := tmpFile.Name()
// Clean up: close and remove the temporary file.
tmpFile.Close()
os.Remove(fileName)
// If the temporary file was created successfully, the directory is writable.
return true
}
// RunReadStdout is a general function to run a binary and return the standard output.
// This can be used to run any binary, not just Python scripts.
// RunReadStdout blocks until the child process exits.
func RunReadStdout(binPath string, args ...string) (string, error) {
retv := ""
cmd := exec.Command(binPath, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
defer stdout.Close()
// continue to read the output until there is no more
// or an error occurs
if err := cmd.Start(); err != nil {
return "", err
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
retv += scanner.Text() + "\n"
}
return retv, nil
}