-
Notifications
You must be signed in to change notification settings - Fork 290
/
executor_filter.go
60 lines (48 loc) · 1.38 KB
/
executor_filter.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
package execute
import (
"bufio"
"bytes"
"strings"
"github.com/kubeshop/botkube/pkg/bot/interactive"
)
var _ executorFilter = &executorTextFilter{}
// executorTextFilter filters executor text results by a given text value.
type executorTextFilter struct {
value []byte
}
// IsActive whether this filter will actually mutate the output or not.
func (f *executorTextFilter) IsActive() bool {
return len(f.value) > 0
}
// newExecutorTextFilter creates a new executorTextFilter.
func newExecutorTextFilter(val string) *executorTextFilter {
return &executorTextFilter{
value: []byte(val),
}
}
// Apply implements executorFilter to apply filtering.
func (f *executorTextFilter) Apply(text string) string {
if !f.IsActive() {
return text
}
var out strings.Builder
scanner := bufio.NewScanner(strings.NewReader(text))
for scanner.Scan() {
scanned := scanner.Bytes()
if bytes.Contains(scanned, f.value) {
out.Write(scanned)
out.WriteString("\n")
}
}
return strings.TrimSuffix(out.String(), "\n")
}
func appendInteractiveFilterIfNeeded(body string, msg interactive.CoreMessage, cmdCtx CommandContext) interactive.CoreMessage {
if !cmdCtx.Platform.IsInteractive() {
return msg
}
if len(strings.SplitN(body, "\n", lineLimitToShowFilter)) < lineLimitToShowFilter {
return msg
}
msg.PlaintextInputs = append(msg.PlaintextInputs, filterInput(cmdCtx.CleanCmd))
return msg
}