-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
53 lines (43 loc) · 964 Bytes
/
scan.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
package hec
import (
"bufio"
"encoding/json"
"fmt"
"math"
"os"
"time"
)
// Scan reads input from stdin and publishes it to a specified Splunk HTTP Event Collector (HEC) and also optionally to stdout.
// See EventManager Client
func Scan(opt ...ScanOpt) error {
opts := defaultScanOpts
opts.apply(opt)
em := opts.manager
if err := em.Start(); err != nil {
return err
}
// Defer stopping the manager after a specified drain time.
defer func() {
time.Sleep(time.Duration(math.Max(10, opts.drainTime.Seconds())))
em.Stop()
}()
publishFn := func(e string) {}
if opts.stdoutEnabled {
publishFn = func(e string) {
fmt.Println(e)
}
}
publishFn1 := publishFn
publishFn = func(e string) {
publishFn1(e)
ee := make(map[string]any)
if err := json.Unmarshal([]byte(e), &ee); err == nil {
em.Publish(ee)
}
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
publishFn(scanner.Text())
}
return scanner.Err()
}