-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_transformer.go
56 lines (44 loc) · 1.01 KB
/
command_transformer.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
package main
import (
"strings"
)
var commandList = []string{
"nodedisabled",
}
func isCommandInList(cmd string) bool {
for _, command := range commandList {
if cmd == command {
return true
}
}
return false
}
func transformCommand(command string) string {
segments := strings.Split(command, "&&")
transformedSegments := make([]string, len(segments))
for i, segment := range segments {
trimmedSegment := strings.TrimSpace(segment)
cmd := strings.Fields(trimmedSegment)[0]
if isCommandInList(cmd) {
transformedSegments[i] = "timeout 5 " + trimmedSegment
} else {
transformedSegments[i] = trimmedSegment
}
}
return join(transformedSegments)
}
func join(segments []string) string {
var result strings.Builder
for i, segment := range segments {
isLastElement := i == len(segments)-1
result.WriteString(segment)
if !isLastElement {
if strings.HasPrefix(segment, "timeout") {
result.WriteString(" & ")
} else {
result.WriteString(" && ")
}
}
}
return result.String()
}