-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
43 lines (34 loc) · 925 Bytes
/
search.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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/fatih/color"
"github.com/opensearch-project/opensearch-go/v2"
)
func search(client *opensearch.Client, clibanaConfig ClibanaConfig) {
tailer := NewTailer(client, clibanaConfig)
for hit := range tailer.Tail() {
var output string
if clibanaConfig.Search.Fields != nil {
var values []string
for _, field := range clibanaConfig.Search.Fields {
if value, ok := getNestedField(hit.Source, field.Name); ok {
if colorCode, ok := Colors[field.Color]; ok {
colorer := color.New(colorCode)
value = colorer.Sprint(value)
}
values = append(values, value)
}
}
output = strings.Join(values, " ")
} else {
buf, err := json.Marshal(hit.Source)
if err != nil {
FatalError(fmt.Errorf("failed to marshal JSON: %w", err))
}
output = string(buf)
}
fmt.Println(output) //nolint:forbidigo
}
}