diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index 95351e7d0..40fa4131e 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -58,6 +58,15 @@ type BlockStat interface { Path() Path } +// Pin holds information about pinned resource +type Pin interface { + // Path to the pinned object + Path() Path + + // Type of the pin + Type() string +} + // CoreAPI defines an unified interface to IPFS for Go programs. type CoreAPI interface { // Unixfs returns an implementation of Unixfs API. @@ -322,5 +331,40 @@ type ObjectStat struct { CumulativeSize int } +// PinAPI specifies the interface to pining +type PinAPI interface { + // Add creates new pin, be default recursive - pinning the whole referenced + // tree + Add(context.Context, Path, ...options.PinAddOption) error + + // WithRecursive is an option for Add which specifies whether to pin an entire + // object tree or just one object. Default: true + WithRecursive(bool) options.PinAddOption + + // Ls returns list of pinned objects on this node + Ls(context.Context) ([]Pin, error) + + // WithType is an option for Ls which allows to specify which pin types should + // be returned + // + // Supported values: + // * "direct" - directly pinned objects + // * "recursive" - roots of recursive pins + // * "indirect" - indirectly pinned objects (referenced by recursively pinned + // objects) + // * "all" - all pinned objects (default) + WithType(string) options.PinLsOption + + // Rm removes pin for object specified by the path + Rm(context.Context, Path) error + + // Update changes one pin to another, skipping checks for matching paths in + // the old tree + Update(ctx context.Context, from Path, to Path) error + + // Verify verifies the integrity of pinned objects + Verify(context.Context) error +} + var ErrIsDir = errors.New("object is a directory") var ErrOffline = errors.New("can't resolve, ipfs node is offline") diff --git a/core/coreapi/interface/options/pin.go b/core/coreapi/interface/options/pin.go new file mode 100644 index 000000000..4ad16d555 --- /dev/null +++ b/core/coreapi/interface/options/pin.go @@ -0,0 +1,58 @@ +package options + +type PinAddSettings struct { + Recursive bool +} + +type PinLsSettings struct { + Type string +} + +type PinAddOption func(*PinAddSettings) error +type PinLsOption func(settings *PinLsSettings) error + +func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) { + options := &PinAddSettings{ + Recursive: true, + } + + for _, opt := range opts { + err := opt(options) + if err != nil { + return nil, err + } + } + + return options, nil +} + +func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) { + options := &PinLsSettings{ + Type: "all", + } + + for _, opt := range opts { + err := opt(options) + if err != nil { + return nil, err + } + } + + return options, nil +} + +type PinOptions struct{} + +func (api *PinOptions) WithRecursive(recucsive bool) PinAddOption { + return func(settings *PinAddSettings) error { + settings.Recursive = recucsive + return nil + } +} + +func (api *PinOptions) WithType(t string) PinLsOption { + return func(settings *PinLsSettings) error { + settings.Type = t + return nil + } +} diff --git a/core/coreapi/pin.go b/core/coreapi/pin.go new file mode 100644 index 000000000..ef7f98c24 --- /dev/null +++ b/core/coreapi/pin.go @@ -0,0 +1,34 @@ +package coreapi + +import ( + "context" + + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" + "github.com/pkg/errors" +) + +type PinAPI struct { + *CoreAPI + *caopts.PinOptions +} + +func (api *PinAPI) Add(context.Context, coreiface.Path, ...caopts.PinAddOption) error { + return errors.New("TODO") +} + +func (api *PinAPI) Ls(context.Context) ([]coreiface.Pin, error) { + return nil, errors.New("TODO") +} + +func (api *PinAPI) Rm(context.Context, coreiface.Path) error { + return errors.New("TODO") +} + +func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface.Path) error { + return errors.New("TODO") +} + +func (api *PinAPI) Verify(context.Context) error { + return errors.New("TODO") +}