-
Notifications
You must be signed in to change notification settings - Fork 402
/
file_source.go
73 lines (59 loc) · 1.71 KB
/
file_source.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package trust
import (
"context"
"os"
"github.com/zeebo/errs"
)
var (
// ErrFileSource is an error class for file source errors.
ErrFileSource = errs.Class("file source")
)
// FileSource represents a trust source contained in a file on disk.
type FileSource struct {
path string
}
// NewFileSource creates a new FileSource that loads a trust list from the
// given path.
func NewFileSource(path string) *FileSource {
return &FileSource{
path: path,
}
}
// String implements the Source interface and returns the FileSource URL.
func (source *FileSource) String() string {
return source.path
}
// Static implements the Source interface. It returns true.
func (source *FileSource) Static() bool { return true }
// FetchEntries implements the Source interface and returns entries from a
// the file source on disk. The entries returned are authoritative.
func (source *FileSource) FetchEntries(ctx context.Context) (_ []Entry, err error) {
urls, err := LoadSatelliteURLList(ctx, source.path)
if err != nil {
return nil, err
}
var entries []Entry
for _, url := range urls {
entries = append(entries, Entry{
SatelliteURL: url,
Authoritative: true,
})
}
return entries, nil
}
// LoadSatelliteURLList loads a list of Satellite URLs from a path on disk.
func LoadSatelliteURLList(ctx context.Context, path string) (_ []SatelliteURL, err error) {
defer mon.Task()(&ctx)(&err)
f, err := os.Open(path)
if err != nil {
return nil, ErrFileSource.Wrap(err)
}
defer func() { err = errs.Combine(err, f.Close()) }()
urls, err := ParseSatelliteURLList(ctx, f)
if err != nil {
return nil, ErrFileSource.Wrap(err)
}
return urls, nil
}