-
Notifications
You must be signed in to change notification settings - Fork 85
/
utils.go
93 lines (76 loc) · 2.28 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
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
package system
import (
"bufio"
"fmt"
"io"
"net/url"
"os"
"regexp"
"strings"
"github.com/c2h5oh/datasize"
)
// Making these variable to allow for testing
// MaxStdoutFileLength sets the max size for stdout file during container execution (needed to prevent DoS)
var MaxStdoutFileLength = 1 * datasize.GB
// MaxStderrFileLength sets the max size for stderr file during container execution (needed to prevent DoS)
var MaxStderrFileLength = 1 * datasize.GB
// MaxStdoutReturnLength sets the max size for stdout string return into RunOutput (with truncation)
// from container execution (needed to prevent DoS)
var MaxStdoutReturnLength = 2 * datasize.KB
// MaxStderrReturnLength sets the max size for stderr string return into RunOutput (with truncation)
// from container execution (needed to prevent DoS)
var MaxStderrReturnLength = 2 * datasize.KB
// TODO: #282 we need these to avoid stream based deadlocks
// https://go-review.googlesource.com/c/go/+/42271/3/misc/android/go_android_exec.go#37
var Stdout = struct{ io.Writer }{os.Stdout}
var Stderr = struct{ io.Writer }{os.Stderr}
// PathExists returns whether the given file or directory exists
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func ReverseList(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func SplitLines(s string) []string {
var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
func FindJobIDInTestOutput(testOutput string) string {
// Build a regex starting with Job ID and ending with a UUID
r := regexp.MustCompile(`Job ID: (j-[a-f0-9-]{36})`)
b := r.FindStringSubmatch(testOutput)
if len(b) > 1 {
return b[1]
}
return ""
}
func FindJobIDInTestOutputLegacy(testOutput string) string {
// Build a regex starting with Job ID and ending with a UUID
r := regexp.MustCompile(`Job ID: ([a-f0-9-]{36})`)
b := r.FindStringSubmatch(testOutput)
if len(b) > 1 {
return b[1]
}
return ""
}
func MustParseURL(uri string) *url.URL {
url, err := url.Parse(uri)
if err != nil {
panic(fmt.Sprintf("url does not parse: %s", uri))
}
return url
}