-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
58 lines (45 loc) · 1.33 KB
/
helpers.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
package dira
import (
"encoding/json"
"fmt"
"io"
"regexp"
"strconv"
)
func prepareInquirePayload(e ExecutionAbstraction) (string, map[string]interface{}, string) {
url := fmt.Sprintf(InquireUrl, e.GetUrl(), e.GetContainer())
inquireOptions := map[string]interface{}{
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
"Cmd": []interface{}{"sh", "-c", e.GetCommand()}, // bash is acceptable instead of "sh"
}
inquireOptJson, _ := json.Marshal(inquireOptions)
inquireOptionJsonLen := strconv.Itoa(len(inquireOptJson))
return url, inquireOptions, inquireOptionJsonLen
}
func prepareExecPayload(e ExecutionAbstraction, id string) (string, map[string]interface{}) {
url := fmt.Sprintf(ExecUrl, e.GetUrl(), id)
execOptions := map[string]interface{}{
"Detach": false,
"Tty": false,
}
return url, execOptions
}
func removeMatching(containerStdout []byte) string {
stdout := string(containerStdout)
regex := regexp.MustCompile(RegexPattern)
return regex.ReplaceAllString(stdout, "")
}
func handleStdoutResult(matching bool, result io.ReadCloser) (res string, err error) {
containerStdOut, ioErr := io.ReadAll(result)
if err = ioErr; err != nil {
return "", ioErr
}
res = string(containerStdOut)
if matching {
res = removeMatching(containerStdOut)
}
return res, err
}