-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
remotepin.go
770 lines (665 loc) · 22.6 KB
/
remotepin.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package pin
import (
"context"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"time"
neturl "net/url"
gopath "path"
"golang.org/x/sync/errgroup"
cid "github.com/ipfs/go-cid"
cmds "github.com/ipfs/go-ipfs-cmds"
config "github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
logging "github.com/ipfs/go-log"
pinclient "github.com/ipfs/go-pinning-service-http-client"
path "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
)
var log = logging.Logger("core/commands/cmdenv")
var remotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Pin (and unpin) objects to remote pinning service.",
},
Subcommands: map[string]*cmds.Command{
"add": addRemotePinCmd,
"ls": listRemotePinCmd,
"rm": rmRemotePinCmd,
"service": remotePinServiceCmd,
},
}
var remotePinServiceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Configure remote pinning services.",
},
Subcommands: map[string]*cmds.Command{
"add": addRemotePinServiceCmd,
"ls": lsRemotePinServiceCmd,
"rm": rmRemotePinServiceCmd,
},
}
const pinNameOptionName = "name"
const pinCIDsOptionName = "cid"
const pinStatusOptionName = "status"
const pinServiceNameOptionName = "service"
const pinServiceNameArgName = pinServiceNameOptionName
const pinServiceEndpointArgName = "endpoint"
const pinServiceKeyArgName = "key"
const pinServiceStatOptionName = "stat"
const pinBackgroundOptionName = "background"
const pinForceOptionName = "force"
type RemotePinOutput struct {
Status string
Cid string
Name string
}
func toRemotePinOutput(ps pinclient.PinStatusGetter) RemotePinOutput {
return RemotePinOutput{
Name: ps.GetPin().GetName(),
Status: ps.GetStatus().String(),
Cid: ps.GetPin().GetCid().String(),
}
}
func printRemotePinDetails(w io.Writer, out *RemotePinOutput) {
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
defer tw.Flush()
fw := func(k string, v string) {
fmt.Fprintf(tw, "%s:\t%s\n", k, v)
}
fw("CID", out.Cid)
fw("Name", out.Name)
fw("Status", out.Status)
}
// remote pin commands
var pinServiceNameOption = cmds.StringOption(pinServiceNameOptionName, "Name of the remote pinning service to use (mandatory).")
var addRemotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Pin object to remote pinning service.",
ShortDescription: "Asks remote pinning service to pin an IPFS object from a given path.",
LongDescription: `
Asks remote pinning service to pin an IPFS object from a given path or a CID.
To pin CID 'bafkqaaa' to service named 'mysrv' under a pin named 'mypin':
$ ipfs pin remote add --service=mysrv --name=mypin bafkqaaa
The above command will block until remote service returns 'pinned' status,
which may take time depending on the size and available providers of the pinned
data.
If you prefer to not wait for pinning confirmation and return immediately
after remote service confirms 'queued' status, add the '--background' flag:
$ ipfs pin remote add --service=mysrv --name=mypin --background bafkqaaa
Status of background pin requests can be inspected with the 'ls' command.
To list all pins for the CID across all statuses:
$ ipfs pin remote ls --service=mysrv --cid=bafkqaaa --status=queued \
--status=pinning --status=pinned --status=failed
NOTE: a comma-separated notation is supported in CLI for convenience:
$ ipfs pin remote ls --service=mysrv --cid=bafkqaaa --status=queued,pinning,pinned,failed
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("ipfs-path", true, false, "Path to object(s) to be pinned."),
},
Options: []cmds.Option{
pinServiceNameOption,
cmds.StringOption(pinNameOptionName, "An optional name for the pin."),
cmds.BoolOption(pinBackgroundOptionName, "Add to the queue on the remote service and return immediately (does not wait for pinned status).").WithDefault(false),
},
Type: RemotePinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx, cancel := context.WithCancel(req.Context)
defer cancel()
// Get remote service
c, err := getRemotePinServiceFromRequest(req, env)
if err != nil {
return err
}
// Prepare value for Pin.cid
if len(req.Arguments) != 1 {
return fmt.Errorf("expecting one CID argument")
}
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
rp, err := api.ResolvePath(ctx, path.New(req.Arguments[0]))
if err != nil {
return err
}
// Prepare Pin.name
opts := []pinclient.AddOption{}
if name, nameFound := req.Options[pinNameOptionName]; nameFound {
nameStr := name.(string)
opts = append(opts, pinclient.PinOpts.WithName(nameStr))
}
// Prepare Pin.origins
// Add own multiaddrs to the 'origins' array, so Pinning Service can
// use that as a hint and connect back to us (if possible)
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
if node.PeerHost != nil {
addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(node.PeerHost))
if err != nil {
return err
}
opts = append(opts, pinclient.PinOpts.WithOrigins(addrs...))
}
// Execute remote pin request
// TODO: fix panic when pinning service is down
ps, err := c.Add(ctx, rp.Cid(), opts...)
if err != nil {
return err
}
// Act on PinStatus.delegates
// If Pinning Service returned any delegates, proactively try to
// connect to them to facilitate data exchange without waiting for DHT
// lookup
for _, d := range ps.GetDelegates() {
// TODO: confirm this works as expected
p, err := peer.AddrInfoFromP2pAddr(d)
if err != nil {
return err
}
if err := api.Swarm().Connect(ctx, *p); err != nil {
log.Infof("error connecting to remote pin delegate %v : %w", d, err)
}
}
// Block unless --background=true is passed
if !req.Options[pinBackgroundOptionName].(bool) {
requestId := ps.GetRequestId()
for {
ps, err = c.GetStatusByID(ctx, requestId)
if err != nil {
return fmt.Errorf("failed to check pin status for requestid=%q due to error: %v", requestId, err)
}
if ps.GetRequestId() != requestId {
return fmt.Errorf("failed to check pin status for requestid=%q, remote service sent unexpected requestid=%q", requestId, ps.GetRequestId())
}
s := ps.GetStatus()
if s == pinclient.StatusPinned {
break
}
if s == pinclient.StatusFailed {
return fmt.Errorf("remote service failed to pin requestid=%q", requestId)
}
tmr := time.NewTimer(time.Second / 2)
select {
case <-tmr.C:
case <-ctx.Done():
return fmt.Errorf("waiting for pin interrupted, requestid=%q remains on remote service", requestId)
}
}
}
return res.Emit(toRemotePinOutput(ps))
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RemotePinOutput) error {
printRemotePinDetails(w, out)
return nil
}),
},
}
var listRemotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List objects pinned to remote pinning service.",
ShortDescription: `
Returns a list of objects that are pinned to a remote pinning service.
`,
LongDescription: `
Returns a list of objects that are pinned to a remote pinning service.
NOTE: By default, it will only show matching objects in 'pinned' state.
Pass '--status=queued,pinning,pinned,failed' to list pins in all states.
`,
},
Arguments: []cmds.Argument{},
Options: []cmds.Option{
pinServiceNameOption,
cmds.StringOption(pinNameOptionName, "Return pins with names that contain the value provided (case-sensitive, exact match)."),
cmds.DelimitedStringsOption(",", pinCIDsOptionName, "Return pins for the specified CIDs (comma-separated)."),
cmds.DelimitedStringsOption(",", pinStatusOptionName, "Return pins with the specified statuses (queued,pinning,pinned,failed).").WithDefault([]string{"pinned"}),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx, cancel := context.WithCancel(req.Context)
defer cancel()
c, err := getRemotePinServiceFromRequest(req, env)
if err != nil {
return err
}
psCh, errCh, err := lsRemote(ctx, req, c)
if err != nil {
return err
}
for ps := range psCh {
if err := res.Emit(toRemotePinOutput(ps)); err != nil {
return err
}
}
return <-errCh
},
Type: RemotePinOutput{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RemotePinOutput) error {
// pin remote ls produces a flat output similar to legacy pin ls
fmt.Fprintf(w, "%s\t%s\t%s\n", out.Cid, out.Status, cmdenv.EscNonPrint(out.Name))
return nil
}),
},
}
// Executes GET /pins/?query-with-filters
func lsRemote(ctx context.Context, req *cmds.Request, c *pinclient.Client) (chan pinclient.PinStatusGetter, chan error, error) {
opts := []pinclient.LsOption{}
if name, nameFound := req.Options[pinNameOptionName]; nameFound {
nameStr := name.(string)
opts = append(opts, pinclient.PinOpts.FilterName(nameStr))
}
if cidsRaw, cidsFound := req.Options[pinCIDsOptionName]; cidsFound {
cidsRawArr := cidsRaw.([]string)
var parsedCIDs []cid.Cid
for _, rawCID := range cidsRawArr {
parsedCID, err := cid.Decode(rawCID)
if err != nil {
return nil, nil, fmt.Errorf("CID %q cannot be parsed: %v", rawCID, err)
}
parsedCIDs = append(parsedCIDs, parsedCID)
}
opts = append(opts, pinclient.PinOpts.FilterCIDs(parsedCIDs...))
}
if statusRaw, statusFound := req.Options[pinStatusOptionName]; statusFound {
statusRawArr := statusRaw.([]string)
var parsedStatuses []pinclient.Status
for _, rawStatus := range statusRawArr {
s := pinclient.Status(rawStatus)
if s.String() == string(pinclient.StatusUnknown) {
return nil, nil, fmt.Errorf("status %q is not valid", rawStatus)
}
parsedStatuses = append(parsedStatuses, s)
}
opts = append(opts, pinclient.PinOpts.FilterStatus(parsedStatuses...))
}
psCh, errCh := c.Ls(ctx, opts...)
return psCh, errCh, nil
}
var rmRemotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove pins from remote pinning service.",
ShortDescription: "Removes the remote pin, allowing it to be garbage-collected if needed.",
LongDescription: `
Removes remote pins, allowing them to be garbage-collected if needed.
This command accepts the same search query parameters as 'ls', and it is good
practice to execute 'ls' before 'rm' to confirm the list of pins to be removed.
To remove a single pin for a specific CID:
$ ipfs pin remote ls --service=mysrv --cid=bafkqaaa
$ ipfs pin remote rm --service=mysrv --cid=bafkqaaa
When more than one pin matches the query on the remote service, an error is
returned. To confirm the removal of multiple pins, pass '--force':
$ ipfs pin remote ls --service=mysrv --name=popular-name
$ ipfs pin remote rm --service=mysrv --name=popular-name --force
NOTE: When no '--status' is passed, implicit '--status=pinned' is used.
To list and then remove all pending pin requests, pass an explicit status list:
$ ipfs pin remote ls --service=mysrv --status=queued,pinning,failed
$ ipfs pin remote rm --service=mysrv --status=queued,pinning,failed --force
`,
},
Arguments: []cmds.Argument{},
Options: []cmds.Option{
pinServiceNameOption,
cmds.StringOption(pinNameOptionName, "Remove pins with names that contain provided value (case-sensitive, exact match)."),
cmds.DelimitedStringsOption(",", pinCIDsOptionName, "Remove pins for the specified CIDs."),
cmds.DelimitedStringsOption(",", pinStatusOptionName, "Remove pins with the specified statuses (queued,pinning,pinned,failed).").WithDefault([]string{"pinned"}),
cmds.BoolOption(pinForceOptionName, "Allow removal of multiple pins matching the query without additional confirmation.").WithDefault(false),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx, cancel := context.WithCancel(req.Context)
defer cancel()
c, err := getRemotePinServiceFromRequest(req, env)
if err != nil {
return err
}
rmIDs := []string{}
if len(req.Arguments) == 0 {
psCh, errCh, err := lsRemote(ctx, req, c)
if err != nil {
return err
}
for ps := range psCh {
rmIDs = append(rmIDs, ps.GetRequestId())
}
if err = <-errCh; err != nil {
return fmt.Errorf("error while listing remote pins: %v", err)
}
if len(rmIDs) > 1 && !req.Options[pinForceOptionName].(bool) {
return fmt.Errorf("multiple remote pins are matching this query, add --force to confirm the bulk removal")
}
} else {
return fmt.Errorf("unexpected argument %q", req.Arguments[0])
}
for _, rmID := range rmIDs {
if err := c.DeleteByID(ctx, rmID); err != nil {
return fmt.Errorf("removing pin identified by requestid=%q failed: %v", rmID, err)
}
}
return nil
},
}
// remote service commands
var addRemotePinServiceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Add remote pinning service.",
ShortDescription: "Add credentials for access to a remote pinning service.",
LongDescription: `
Add credentials for access to a remote pinning service and store them in the
config under Pinning.RemoteServices map.
TIP:
To add services and test them by fetching pin count stats:
$ ipfs pin remote service add goodsrv https://pin-api.example.com secret-key
$ ipfs pin remote service add badsrv https://bad-api.example.com invalid-key
$ ipfs pin remote service ls --stat
goodsrv https://pin-api.example.com 0/0/0/0
badsrv https://bad-api.example.com invalid
`,
},
Arguments: []cmds.Argument{
cmds.StringArg(pinServiceNameArgName, true, false, "Service name."),
cmds.StringArg(pinServiceEndpointArgName, true, false, "Service endpoint."),
cmds.StringArg(pinServiceKeyArgName, true, false, "Service key."),
},
Type: nil,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
repo, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
defer repo.Close()
if len(req.Arguments) < 3 {
return fmt.Errorf("expecting three arguments: service name, endpoint and key")
}
name := req.Arguments[0]
endpoint, err := normalizeEndpoint(req.Arguments[1])
if err != nil {
return err
}
key := req.Arguments[2]
cfg, err := repo.Config()
if err != nil {
return err
}
if cfg.Pinning.RemoteServices != nil {
if _, present := cfg.Pinning.RemoteServices[name]; present {
return fmt.Errorf("service already present")
}
} else {
cfg.Pinning.RemoteServices = map[string]config.RemotePinningService{}
}
cfg.Pinning.RemoteServices[name] = config.RemotePinningService{
API: config.RemotePinningServiceAPI{
Endpoint: endpoint,
Key: key,
},
Policies: config.RemotePinningServicePolicies{},
}
return repo.SetConfig(cfg)
},
}
var rmRemotePinServiceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove remote pinning service.",
ShortDescription: "Remove credentials for access to a remote pinning service.",
},
Arguments: []cmds.Argument{
cmds.StringArg(pinServiceNameOptionName, true, false, "Name of remote pinning service to remove."),
},
Options: []cmds.Option{},
Type: nil,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
repo, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
defer repo.Close()
if len(req.Arguments) != 1 {
return fmt.Errorf("expecting one argument: name")
}
name := req.Arguments[0]
cfg, err := repo.Config()
if err != nil {
return err
}
if cfg.Pinning.RemoteServices != nil {
delete(cfg.Pinning.RemoteServices, name)
}
return repo.SetConfig(cfg)
},
}
var lsRemotePinServiceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List remote pinning services.",
ShortDescription: "List remote pinning services.",
LongDescription: `
List remote pinning services.
By default, only a name and an endpoint are listed; however, one can pass
'--stat' to test each endpoint by fetching pin counts for each state:
$ ipfs pin remote service ls --stat
goodsrv https://pin-api.example.com 0/0/0/0
badsrv https://bad-api.example.com invalid
TIP: pass '--enc=json' for more useful JSON output.
`,
},
Arguments: []cmds.Argument{},
Options: []cmds.Option{
cmds.BoolOption(pinServiceStatOptionName, "Try to fetch and display current pin count on remote service (queued/pinning/pinned/failed).").WithDefault(false),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx, cancel := context.WithCancel(req.Context)
defer cancel()
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
repo, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
defer repo.Close()
cfg, err := repo.Config()
if err != nil {
return err
}
if cfg.Pinning.RemoteServices == nil {
return cmds.EmitOnce(res, &PinServicesList{make([]ServiceDetails, 0)})
}
services := cfg.Pinning.RemoteServices
result := PinServicesList{make([]ServiceDetails, 0, len(services))}
for svcName, svcConfig := range services {
svcDetails := ServiceDetails{svcName, svcConfig.API.Endpoint, nil}
// if --pin-count is passed, we try to fetch pin numbers from remote service
if req.Options[pinServiceStatOptionName].(bool) {
lsRemotePinCount := func(ctx context.Context, env cmds.Environment, svcName string) (*PinCount, error) {
c, err := getRemotePinService(env, svcName)
if err != nil {
return nil, err
}
// we only care about total count, so requesting smallest batch
batch := pinclient.PinOpts.Limit(1)
fs := pinclient.PinOpts.FilterStatus
statuses := []pinclient.Status{
pinclient.StatusQueued,
pinclient.StatusPinning,
pinclient.StatusPinned,
pinclient.StatusFailed,
}
g, ctx := errgroup.WithContext(ctx)
pc := &PinCount{}
for _, s := range statuses {
status := s // lol https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
_, n, err := c.LsBatchSync(ctx, batch, fs(status))
if err != nil {
return err
}
switch status {
case pinclient.StatusQueued:
pc.Queued = n
case pinclient.StatusPinning:
pc.Pinning = n
case pinclient.StatusPinned:
pc.Pinned = n
case pinclient.StatusFailed:
pc.Failed = n
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return pc, nil
}
pinCount, err := lsRemotePinCount(ctx, env, svcName)
// PinCount is present only if we were able to fetch counts.
// We don't want to break listing of services so this is best-effort.
// (verbose err is returned by 'pin remote ls', if needed)
svcDetails.Stat = &Stat{}
if err == nil {
svcDetails.Stat.Status = "valid"
svcDetails.Stat.PinCount = pinCount
} else {
svcDetails.Stat.Status = "invalid"
}
}
result.RemoteServices = append(result.RemoteServices, svcDetails)
}
sort.Sort(result)
return cmds.EmitOnce(res, &result)
},
Type: PinServicesList{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, list *PinServicesList) error {
tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
withStat := req.Options[pinServiceStatOptionName].(bool)
for _, s := range list.RemoteServices {
if withStat {
stat := s.Stat.Status
pc := s.Stat.PinCount
if s.Stat.PinCount != nil {
stat = fmt.Sprintf("%d/%d/%d/%d", pc.Queued, pc.Pinning, pc.Pinned, pc.Failed)
}
fmt.Fprintf(tw, "%s\t%s\t%s\n", s.Service, s.ApiEndpoint, stat)
} else {
fmt.Fprintf(tw, "%s\t%s\n", s.Service, s.ApiEndpoint)
}
}
tw.Flush()
return nil
}),
},
}
type ServiceDetails struct {
Service string
ApiEndpoint string
Stat *Stat `json:",omitempty"` // present only when --stat not passed
}
type Stat struct {
Status string
PinCount *PinCount `json:",omitempty"` // missing when --stat is passed but the service is offline
}
type PinCount struct {
Queued int
Pinning int
Pinned int
Failed int
}
// Struct returned by ipfs pin remote service ls --enc=json | jq
type PinServicesList struct {
RemoteServices []ServiceDetails
}
func (l PinServicesList) Len() int {
return len(l.RemoteServices)
}
func (l PinServicesList) Swap(i, j int) {
s := l.RemoteServices
s[i], s[j] = s[j], s[i]
}
func (l PinServicesList) Less(i, j int) bool {
s := l.RemoteServices
return s[i].Service < s[j].Service
}
func getRemotePinServiceFromRequest(req *cmds.Request, env cmds.Environment) (*pinclient.Client, error) {
service, serviceFound := req.Options[pinServiceNameOptionName]
if !serviceFound {
return nil, fmt.Errorf("a service name must be passed")
}
serviceStr := service.(string)
var err error
c, err := getRemotePinService(env, serviceStr)
if err != nil {
return nil, err
}
return c, nil
}
func getRemotePinService(env cmds.Environment, name string) (*pinclient.Client, error) {
if name == "" {
return nil, fmt.Errorf("remote pinning service name not specified")
}
endpoint, key, err := getRemotePinServiceInfo(env, name)
if err != nil {
return nil, err
}
return pinclient.NewClient(endpoint, key), nil
}
func getRemotePinServiceInfo(env cmds.Environment, name string) (endpoint, key string, err error) {
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return "", "", err
}
repo, err := fsrepo.Open(cfgRoot)
if err != nil {
return "", "", err
}
defer repo.Close()
cfg, err := repo.Config()
if err != nil {
return "", "", err
}
if cfg.Pinning.RemoteServices == nil {
return "", "", fmt.Errorf("service not known")
}
service, present := cfg.Pinning.RemoteServices[name]
if !present {
return "", "", fmt.Errorf("service not known")
}
endpoint, err = normalizeEndpoint(service.API.Endpoint)
if err != nil {
return "", "", err
}
return endpoint, service.API.Key, nil
}
func normalizeEndpoint(endpoint string) (string, error) {
uri, err := neturl.ParseRequestURI(endpoint)
if err != nil || !(uri.Scheme == "http" || uri.Scheme == "https") {
return "", fmt.Errorf("service endpoint must be a valid HTTP URL")
}
// cleanup trailing and duplicate slashes (https://github.com/ipfs/go-ipfs/issues/7826)
uri.Path = gopath.Clean(uri.Path)
uri.Path = strings.TrimSuffix(uri.Path, ".")
uri.Path = strings.TrimSuffix(uri.Path, "/")
// remove any query params
if uri.RawQuery != "" {
return "", fmt.Errorf("service endpoint should be provided without any query parameters")
}
if strings.HasSuffix(uri.Path, "/pins") {
return "", fmt.Errorf("service endpoint should be provided without the /pins suffix")
}
return uri.String(), nil
}