coreapi: pin draft
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jan 9, 2018 at 01:10 UTC
064c194b4f30a9834c1a3fca49495a514b68c58b
3 files changed
+136
core/coreapi/interface/interface.go
+44
@@ -58,6 +58,15 @@ type BlockStat interface {
58
Path() Path
59
}
60
61
+// Pin holds information about pinned resource
62
+type Pin interface {
63
+ // Path to the pinned object
64
+ Path() Path
65
+
66
+ // Type of the pin
67
+ Type() string
68
+}
69
+
70
// CoreAPI defines an unified interface to IPFS for Go programs.
71
type CoreAPI interface {
72
// Unixfs returns an implementation of Unixfs API.
@@ -322,5 +331,40 @@ type ObjectStat struct {
331
CumulativeSize int
332
}
333
334
+// PinAPI specifies the interface to pining
335
+type PinAPI interface {
336
+ // Add creates new pin, be default recursive - pinning the whole referenced
337
+ // tree
338
+ Add(context.Context, Path, ...options.PinAddOption) error
339
+
340
+ // WithRecursive is an option for Add which specifies whether to pin an entire
341
+ // object tree or just one object. Default: true
342
+ WithRecursive(bool) options.PinAddOption
343
+
344
+ // Ls returns list of pinned objects on this node
345
+ Ls(context.Context) ([]Pin, error)
346
+
347
+ // WithType is an option for Ls which allows to specify which pin types should
348
+ // be returned
349
+ //
350
+ // Supported values:
351
+ // * "direct" - directly pinned objects
352
+ // * "recursive" - roots of recursive pins
353
+ // * "indirect" - indirectly pinned objects (referenced by recursively pinned
354
+ // objects)
355
+ // * "all" - all pinned objects (default)
356
+ WithType(string) options.PinLsOption
357
+
358
+ // Rm removes pin for object specified by the path
359
+ Rm(context.Context, Path) error
360
+
361
+ // Update changes one pin to another, skipping checks for matching paths in
362
+ // the old tree
363
+ Update(ctx context.Context, from Path, to Path) error
364
+
365
+ // Verify verifies the integrity of pinned objects
366
+ Verify(context.Context) error
367
+}
368
+
369
var ErrIsDir = errors.New("object is a directory")
370
var ErrOffline = errors.New("can't resolve, ipfs node is offline")
core/coreapi/interface/options/pin.go
new
+58
@@ -0,0 +1,58 @@
1
+package options
2
+
3
+type PinAddSettings struct {
4
+ Recursive bool
5
+}
6
+
7
+type PinLsSettings struct {
8
+ Type string
9
+}
10
+
11
+type PinAddOption func(*PinAddSettings) error
12
+type PinLsOption func(settings *PinLsSettings) error
13
+
14
+func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
15
+ options := &PinAddSettings{
16
+ Recursive: true,
17
+ }
18
+
19
+ for _, opt := range opts {
20
+ err := opt(options)
21
+ if err != nil {
22
+ return nil, err
23
+ }
24
+ }
25
+
26
+ return options, nil
27
+}
28
+
29
+func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
30
+ options := &PinLsSettings{
31
+ Type: "all",
32
+ }
33
+
34
+ for _, opt := range opts {
35
+ err := opt(options)
36
+ if err != nil {
37
+ return nil, err
38
+ }
39
+ }
40
+
41
+ return options, nil
42
+}
43
+
44
+type PinOptions struct{}
45
+
46
+func (api *PinOptions) WithRecursive(recucsive bool) PinAddOption {
47
+ return func(settings *PinAddSettings) error {
48
+ settings.Recursive = recucsive
49
+ return nil
50
+ }
51
+}
52
+
53
+func (api *PinOptions) WithType(t string) PinLsOption {
54
+ return func(settings *PinLsSettings) error {
55
+ settings.Type = t
56
+ return nil
57
+ }
58
+}
core/coreapi/pin.go
new
+34
@@ -0,0 +1,34 @@
1
+package coreapi
2
+
3
+import (
4
+ "context"
5
+
6
+ coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
7
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8
+ "github.com/pkg/errors"
9
+)
10
+
11
+type PinAPI struct {
12
+ *CoreAPI
13
+ *caopts.PinOptions
14
+}
15
+
16
+func (api *PinAPI) Add(context.Context, coreiface.Path, ...caopts.PinAddOption) error {
17
+ return errors.New("TODO")
18
+}
19
+
20
+func (api *PinAPI) Ls(context.Context) ([]coreiface.Pin, error) {
21
+ return nil, errors.New("TODO")
22
+}
23
+
24
+func (api *PinAPI) Rm(context.Context, coreiface.Path) error {
25
+ return errors.New("TODO")
26
+}
27
+
28
+func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface.Path) error {
29
+ return errors.New("TODO")
30
+}
31
+
32
+func (api *PinAPI) Verify(context.Context) error {
33
+ return errors.New("TODO")
34
+}