forked from fujiwara/kinesis-tailf
/
cat.go
63 lines (59 loc) · 1.23 KB
/
cat.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
package ktail
import (
"bufio"
"context"
"crypto/sha256"
"fmt"
"io"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/kinesis"
)
func (app *App) Cat(ctx context.Context, partitionKey string, src io.Reader) error {
scanner := bufio.NewScanner(src)
for scanner.Scan() {
b := scanner.Bytes()
if app.AppendLF {
b = append(b, LF...)
}
in := &kinesis.PutRecordInput{
Data: b,
StreamName: &app.StreamName,
}
if partitionKey == "" {
pk := fmt.Sprintf("%x", sha256.Sum256(b))
in.PartitionKey = &pk
} else {
in.PartitionKey = &partitionKey
}
if _, err := app.kinesis.PutRecord(in); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func (app *App) CatFirehose(ctx context.Context, _ string, src io.Reader) error {
fh := firehose.New(app.sess)
scanner := bufio.NewScanner(src)
for scanner.Scan() {
b := scanner.Bytes()
if app.AppendLF {
b = append(b, LF...)
}
in := &firehose.PutRecordInput{
Record: &firehose.Record{
Data: b,
},
DeliveryStreamName: &app.StreamName,
}
if _, err := fh.PutRecord(in); err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}