-
Notifications
You must be signed in to change notification settings - Fork 63
/
provider.go
78 lines (70 loc) · 2.06 KB
/
provider.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
67
68
69
70
71
72
73
74
75
76
77
78
package http
import (
"bytes"
"io"
"strings"
"github.com/spf13/afero"
"github.com/yandex/pandora/components/providers/base"
"github.com/yandex/pandora/components/providers/http/config"
"github.com/yandex/pandora/components/providers/http/decoders"
"github.com/yandex/pandora/components/providers/http/provider"
"github.com/yandex/pandora/core"
"golang.org/x/xerrors"
)
func NewProvider(fs afero.Fs, conf config.Config) (core.Provider, error) {
if !conf.Decoder.IsValid() {
return nil, xerrors.Errorf("unknown decoder type faced")
}
var (
readSeeker io.ReadSeeker
closer io.Closer
err error
)
if len(conf.Uris) > 0 {
readSeeker, closer, err = uriReadSeekCloser(conf)
} else {
readSeeker, closer, err = fileReadSeekCloser(fs, conf.File)
}
if err != nil {
return nil, xerrors.Errorf("cant create ReadSeekCloser: %w", err)
}
decoder, err := decoders.NewDecoder(conf, readSeeker)
if err != nil {
return nil, xerrors.Errorf("decoder init error: %w", err)
}
return &provider.Provider{
ProviderBase: base.ProviderBase{
FS: fs,
},
Config: conf,
Decoder: decoder,
Close: closer.Close,
Sink: make(chan decoders.DecodedAmmo),
}, nil
}
func fileReadSeekCloser(fs afero.Fs, path string) (io.ReadSeeker, io.Closer, error) {
if path == "" {
return nil, nil, xerrors.Errorf("one should specify either 'file' or 'uris'")
}
file, err := fs.Open(path)
if err != nil {
return nil, nil, xerrors.Errorf("open file error: %w", err)
}
return file, file, nil
}
type fakeCloser struct {
}
func (fc *fakeCloser) Close() error {
return nil
}
func uriReadSeekCloser(conf config.Config) (io.ReadSeeker, io.Closer, error) {
if conf.Decoder != config.DecoderURI {
return nil, nil, xerrors.Errorf("'uris' expect setted only for 'uri' decoder, but faced with '%s'", conf.Decoder)
}
if conf.File != "" {
return nil, nil, xerrors.Errorf("one should specify either 'file' or 'uris', but not both of them")
}
reader := bytes.NewReader([]byte(strings.Join(conf.Uris, "\n")))
readSeeker := io.ReadSeeker(reader)
return readSeeker, &fakeCloser{}, nil
}