-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
48 lines (42 loc) · 1.08 KB
/
reader.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
package mapping
import (
"context"
"io"
"time"
"cloud.google.com/go/storage"
)
type Reader struct {
readerBucket func(ctx context.Context, fileName string) (io.ReadCloser, error)
}
func NewReader(gcreds *GcloudCreds, bucketName string) (*Reader, *storage.Client, error) {
gb, gbClient, err := NewGCSBucketGetter(gcreds, bucketName)
if err != nil {
return nil, nil, err
}
return &Reader{
readerBucket: gb.GetStorageReader,
}, gbClient, nil
}
func newReaderFromGCSClient(gbSL func(ctx context.Context, fileName string) (io.ReadCloser, error)) *Reader {
return &Reader{
readerBucket: gbSL,
}
}
func (r *Reader) Load(ctx context.Context, eventFamily string, version string, environment string) (*Mapping, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
filename := getMappingFilename(eventFamily, version, environment)
reader, err := r.readerBucket(ctx, filename)
if err != nil {
return nil, err
}
m, err := LoadMappingIO(reader)
if err != nil {
return nil, err
}
err = reader.Close()
if err != nil {
return nil, err
}
return m, nil
}