-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
66 lines (58 loc) · 1.6 KB
/
commands.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
package main
import (
"sort"
"strings"
)
// Converts a list of stream names into a list of stream ids.
func findStreamIds(opts *options, streamNames string) (results []string) {
names := strings.Split(streamNames, ",")
if len(names) > 0 {
allStreams := fetchStreams(opts)
for _, name := range names {
var id string
lowerName := strings.ToLower(name)
for _, v := range allStreams {
streamName := strings.ToLower(v["title"])
if strings.HasPrefix(streamName, lowerName) {
id = v["id"]
}
}
if len(id) > 0 {
results = append(results, id)
}
}
}
return results
}
// Print out the list of streams defined in Graylog.
func commandListStreams(streams map[string]map[string]string) {
var sts []map[string]string
for _, v := range streams {
sts = append(sts, v)
}
sort.Slice(sts, func(i, j int) bool {
iTitle := strings.ToLower(string(sts[i]["title"]))
jTitle := strings.ToLower(string(sts[j]["title"]))
return iTitle < jTitle
})
for _, stream := range sts {
description := stream["description"]
title := stream["title"]
if len(description) > 0 && title != description {
printBoldText(title + " - " + description)
} else {
printBoldText(title)
}
}
}
// Print out the log messages that match the search criteria.
func commandListMessages(opts *options) ([]logMessage, map[string]map[string]string) {
messages := fetchMessages(opts)
streams := fetchStreams(opts)
return messages, streams
}
func printMessages(messages []logMessage, opts *options, streams map[string]map[string]string) {
for _, msg := range messages {
printMessage(opts, streams, msg)
}
}