-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
250 lines (210 loc) · 8.36 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"log"
"os"
"strings"
"github.com/INFURA/ipfs-pump/pump"
s3ds "github.com/ipfs/go-ds-s3"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
EnumFile = "file"
EnumAPIPin = "apipin"
EnumFlatFS = "flatfs"
EnumBadger = "badger"
EnumS3 = "s3"
)
const (
CollAPI = "api"
CollFlatFS = "flatfs"
CollBadger = "badger"
CollS3 = "s3"
)
const (
DrainAPI = "api"
DrainFlatFS = "flatfs"
DrainBadger = "badger"
DrainS3 = "s3"
)
var (
enumValues = []string{EnumFile, EnumAPIPin, EnumFlatFS, EnumBadger, EnumS3}
enumArg = kingpin.Arg("enum", "The source to enumerate the content. "+
"Possible values are ["+strings.Join(enumValues, ",")+"].").
Required().Enum(enumValues...)
collValues = []string{CollAPI, CollFlatFS, CollBadger, CollS3}
collArg = kingpin.Arg("coll", "The source to get the data blocks. "+
"Possible values are ["+strings.Join(collValues, ",")+"].").
Required().Enum(collValues...)
drainValues = []string{DrainAPI, DrainFlatFS, DrainBadger, DrainS3}
drainArg = kingpin.Arg("drain", "The destination to copy to. "+
"Possible values are ["+strings.Join(drainValues, ",")+"].").
Required().Enum(drainValues...)
worker = kingpin.Flag("worker", "The number of concurrent worker to retrieve/push content").
Default("1").Uint()
failedBlocksPath = kingpin.Flag("failed-blocks-path", "The path to a file where all the failed CIDs should be written").Default("").String()
enumFilePath = kingpin.Flag("enum-file-path", "Enumerator "+EnumFile+": Path")
enumFilePathVal = enumFilePath.String()
enumAPIPinURL = kingpin.Flag("enum-api-pin-url", "Enumerator "+EnumAPIPin+": API URL")
enumAPIPinURLVal = enumAPIPinURL.String()
enumAPIPinStream = kingpin.Flag("enum-api-pin-stream", "Enumerator "+EnumAPIPin+": Stream")
enumAPIPinStreamVal = enumAPIPinStream.Bool()
enumFlatFSPath = kingpin.Flag("enum-flatfs-path", "Enumerator "+EnumFlatFS+": Path")
enumFlatFSPathVal = enumFlatFSPath.String()
enumBadgerPath = kingpin.Flag("enum-badger-path", "Enumerator "+EnumBadger+": Path")
enumBadgerPathVal = enumBadgerPath.String()
enumS3Region = kingpin.Flag("enum-s3-region", "Enumerator "+EnumS3+": Region")
enumS3RegionVal = enumS3Region.String()
enumS3Bucket = kingpin.Flag("enum-s3-bucket", "Enumerator "+EnumS3+": Bucket name")
enumS3BucketVal = enumS3Bucket.String()
enumS3AccessKey = kingpin.Flag("enum-s3-access-key", "Enumerator "+EnumS3+": Access key")
enumS3AccessKeyVal = enumS3AccessKey.String()
enumS3SecretKey = kingpin.Flag("enum-s3-secret-key", "Enumerator "+EnumS3+": Secret key")
enumS3SecretKeyVal = enumS3SecretKey.String()
enumS3SessionToken = kingpin.Flag("enum-s3-session-token", "Enumerator "+EnumS3+": Session token")
enumS3SessionTokenVal = enumS3SessionToken.String()
collAPIURL = kingpin.Flag("coll-api-url", "Collector "+CollAPI+": API URL")
collAPIURLVal = collAPIURL.String()
collFlatFSPath = kingpin.Flag("coll-flatfs-path", "Collector "+CollFlatFS+": Path")
collFlatFSPathVal = collFlatFSPath.String()
collBadgerPath = kingpin.Flag("coll-badger-path", "Collector "+CollBadger+": Path")
collBadgerPathVal = collBadgerPath.String()
collS3Region = kingpin.Flag("coll-s3-region", "Collector "+EnumS3+": Region")
collS3RegionVal = collS3Region.String()
collS3Bucket = kingpin.Flag("coll-s3-bucket", "Collector "+CollS3+": Bucket name")
collS3BucketVal = collS3Bucket.String()
collS3AccessKey = kingpin.Flag("coll-s3-access-key", "Collector "+CollS3+": Access key")
collS3AccessKeyVal = collS3AccessKey.String()
collS3SecretKey = kingpin.Flag("coll-s3-secret-key", "Collector "+CollS3+": Secret key")
collS3SecretKeyVal = collS3SecretKey.String()
collS3SessionToken = kingpin.Flag("coll-s3-session-token", "Collector "+CollS3+": Session token")
collS3SessionTokenVal = collS3SessionToken.String()
drainAPIURL = kingpin.Flag("drain-api-url", "Drain "+DrainAPI+": API URL")
drainAPIURLVal = drainAPIURL.String()
drainFlatFSPath = kingpin.Flag("drain-flatfs-path", "Drain "+DrainFlatFS+": Path")
drainFlatFSPathVal = drainFlatFSPath.String()
drainBadgerPath = kingpin.Flag("drain-badger-path", "Drain "+DrainBadger+": Path")
drainBadgerPathVal = drainBadgerPath.String()
drainS3Region = kingpin.Flag("drain-s3-region", "Drain "+EnumS3+": Region")
drainS3RegionVal = drainS3Region.String()
drainS3Bucket = kingpin.Flag("drain-s3-bucket", "Drain "+DrainS3+": Bucket name")
drainS3BucketVal = drainS3Bucket.String()
drainS3AccessKey = kingpin.Flag("drain-s3-access-key", "Drain "+DrainS3+": Access key")
drainS3AccessKeyVal = drainS3AccessKey.String()
drainS3SecretKey = kingpin.Flag("drain-s3-secret-key", "Drain "+DrainS3+": Secret key")
drainS3SecretKeyVal = drainS3SecretKey.String()
drainS3SessionToken = kingpin.Flag("drain-s3-session-token", "Drain "+DrainS3+": Session token")
drainS3SessionTokenVal = drainS3SessionToken.String()
)
func main() {
kingpin.Parse()
var enumerator pump.Enumerator
var collector pump.Collector
var drain pump.Drain
var err error
switch *enumArg {
case EnumFile:
requiredFlag(enumFilePath, *enumFilePathVal)
var file *os.File
file, err = os.Open(*enumFilePathVal)
if err != nil {
log.Fatal(err)
}
enumerator, err = pump.NewFileEnumerator(file)
case EnumAPIPin:
requiredFlag(enumAPIPinURL, *enumAPIPinURLVal)
enumerator = pump.NewAPIPinEnumerator(*enumAPIPinURLVal, *enumAPIPinStreamVal)
case EnumFlatFS:
requiredFlag(enumFlatFSPath, *enumFlatFSPathVal)
enumerator, err = pump.NewFlatFSEnumerator(*enumFlatFSPathVal)
case EnumBadger:
requiredFlag(enumBadgerPath, *enumBadgerPathVal)
enumerator, err = pump.NewBadgerEnumerator(*enumBadgerPathVal)
case EnumS3:
requiredFlag(enumS3Region, *enumS3RegionVal)
requiredFlag(enumS3Bucket, *enumS3BucketVal)
config := s3ds.Config{
Region: *enumS3RegionVal,
Bucket: *enumS3BucketVal,
AccessKey: *enumS3AccessKeyVal,
SecretKey: *enumS3SecretKeyVal,
SessionToken: *enumS3SessionTokenVal,
}
enumerator, err = pump.NewS3Enumerator(config)
}
if err != nil {
log.Fatal(err)
}
switch *collArg {
case CollAPI:
requiredFlag(collAPIURL, *collAPIURLVal)
collector = pump.NewAPICollector(*collAPIURLVal)
case CollFlatFS:
requiredFlag(collFlatFSPath, *collFlatFSPathVal)
collector, err = pump.NewFlatFSCollector(*collFlatFSPathVal)
case CollBadger:
requiredFlag(collBadgerPath, *collBadgerPathVal)
collector, err = pump.NewBadgerCollector(*collBadgerPathVal)
case CollS3:
requiredFlag(collS3Region, *collS3RegionVal)
requiredFlag(collS3Bucket, *collS3BucketVal)
config := s3ds.Config{
Region: *collS3RegionVal,
Bucket: *collS3BucketVal,
AccessKey: *collS3AccessKeyVal,
SecretKey: *collS3SecretKeyVal,
SessionToken: *collS3SessionTokenVal,
}
collector, err = pump.NewS3Collector(config)
}
if err != nil {
log.Fatal(err)
}
switch *drainArg {
case DrainAPI:
requiredFlag(drainAPIURL, *drainAPIURLVal)
drain = pump.NewAPIDrain(*drainAPIURLVal)
case DrainFlatFS:
requiredFlag(drainFlatFSPath, *drainFlatFSPathVal)
drain, err = pump.NewFlatFSDrain(*drainFlatFSPathVal)
case DrainBadger:
requiredFlag(drainBadgerPath, *drainBadgerPathVal)
drain, err = pump.NewBadgerDrain(*drainBadgerPathVal)
case DrainS3:
requiredFlag(drainS3Region, *drainS3RegionVal)
requiredFlag(drainS3Bucket, *drainS3BucketVal)
config := s3ds.Config{
Region: *drainS3RegionVal,
Bucket: *drainS3BucketVal,
AccessKey: *drainS3AccessKeyVal,
SecretKey: *drainS3SecretKeyVal,
SessionToken: *drainS3SessionTokenVal,
}
drain, err = pump.NewS3Drain(config)
}
if err != nil {
log.Fatal(err)
}
progressWriter := pump.NewProgressWriter()
var failedBlocksWriter pump.FailedBlocksWriter
if *failedBlocksPath == "" {
failedBlocksWriter = pump.NewNullableFileEnumeratorWriter()
} else {
enumWriter, closeWriter, err := pump.NewFileEnumeratorWriter(*failedBlocksPath)
if err != nil {
log.Fatal(err)
}
failedBlocksWriter = enumWriter
defer func() {
err = closeWriter()
if err != nil {
log.Fatal(err)
}
}()
}
pump.PumpIt(enumerator, collector, drain, failedBlocksWriter, progressWriter, *worker)
}
func requiredFlag(flag *kingpin.FlagClause, val string) {
if len(val) == 0 {
log.Fatalf("flag %s is required", flag.Model().Name)
}
}