-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
fetcher.go
107 lines (92 loc) · 2.7 KB
/
fetcher.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package migrations
import (
"context"
"fmt"
"io"
"os"
"github.com/hashicorp/go-multierror"
)
const (
// Current distribution to fetch migrations from
CurrentIpfsDist = "/ipfs/QmdaCHYBDHEhXCMoynH5UcohEay6m1XayZCcxWZzKAHNVN" // fs-repo-11-to-12 v1.0.2
// Latest distribution path. Default for fetchers.
LatestIpfsDist = "/ipns/dist.ipfs.tech"
// Distribution environ variable
envIpfsDistPath = "IPFS_DIST_PATH"
)
type Fetcher interface {
// Fetch attempts to fetch the file at the given ipfs path.
Fetch(ctx context.Context, filePath string) ([]byte, error)
// Close performs any cleanup after the fetcher is not longer needed.
Close() error
}
// MultiFetcher holds multiple Fetchers and provides a Fetch that tries each
// until one succeeds.
type MultiFetcher struct {
fetchers []Fetcher
}
type limitReadCloser struct {
io.Reader
io.Closer
}
// NewMultiFetcher creates a MultiFetcher with the given Fetchers. The
// Fetchers are tried in order, then passed to this function.
func NewMultiFetcher(f ...Fetcher) *MultiFetcher {
mf := &MultiFetcher{
fetchers: make([]Fetcher, len(f)),
}
copy(mf.fetchers, f)
return mf
}
// Fetch attempts to fetch the file at each of its fetchers until one succeeds.
func (f *MultiFetcher) Fetch(ctx context.Context, ipfsPath string) ([]byte, error) {
var errs error
for _, fetcher := range f.fetchers {
out, err := fetcher.Fetch(ctx, ipfsPath)
if err == nil {
return out, nil
}
fmt.Printf("Error fetching: %s\n", err.Error())
errs = multierror.Append(errs, err)
}
return nil, errs
}
func (f *MultiFetcher) Close() error {
var errs error
for _, fetcher := range f.fetchers {
if err := fetcher.Close(); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
func (f *MultiFetcher) Len() int {
return len(f.fetchers)
}
func (f *MultiFetcher) Fetchers() []Fetcher {
return f.fetchers
}
// NewLimitReadCloser returns a new io.ReadCloser with the reader wrappen in a
// io.LimitedReader limited to reading the amount specified.
func NewLimitReadCloser(rc io.ReadCloser, limit int64) io.ReadCloser {
return limitReadCloser{
Reader: io.LimitReader(rc, limit),
Closer: rc,
}
}
// GetDistPathEnv returns the IPFS path to the distribution site, using
// the value of environ variable specified by envIpfsDistPath. If the environ
// variable is not set, then returns the provided distPath, and if that is not set
// then returns the IPNS path.
//
// To get the IPFS path of the latest distribution, if not overriddin by the
// environ variable: GetDistPathEnv(CurrentIpfsDist)
func GetDistPathEnv(distPath string) string {
if dist := os.Getenv(envIpfsDistPath); dist != "" {
return dist
}
if distPath == "" {
return LatestIpfsDist
}
return distPath
}