Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
pin: add a IsPinned method
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Mar 2, 2020
1 parent 292d906 commit c82db2e
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 46 deletions.
184 changes: 147 additions & 37 deletions options/pin.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package options

import "fmt"

type PinAddSettings struct {
Recursive bool
}

type TypeSettings struct {
Type string
}

type PinLsSettings struct {
Type string
}

type PinIsPinnedSettings struct {
WithType string
}

// PinRmSettings represents the settings of pin rm command
type PinRmSettings struct {
Recursive bool
Expand All @@ -17,13 +27,19 @@ type PinUpdateSettings struct {
Unpin bool
}

// PinAddOption pin add option func
type PinAddOption func(*PinAddSettings) error

// PinLsOption pin ls option func
type PinLsOption func(*PinLsSettings) error

// PinIsPinnedOption pin isPinned option func
type PinIsPinnedOption func(*PinIsPinnedSettings) error

// PinRmOption pin rm option func
type PinRmOption func(*PinRmSettings) error

// PinLsOption pin ls option func
type PinLsOption func(*PinLsSettings) error
// PinUpdateOption pin update option func
type PinUpdateOption func(*PinUpdateSettings) error

func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
Expand All @@ -41,24 +57,24 @@ func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
return options, nil
}

// PinRmOptions pin rm options
func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
options := &PinRmSettings{
Recursive: true,
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
options := &PinLsSettings{
Type: "all",
}

for _, opt := range opts {
if err := opt(options); err != nil {
err := opt(options)
if err != nil {
return nil, err
}
}

return options, nil
}

func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
options := &PinLsSettings{
Type: "all",
func PinIsPinnedOptions(opts ...PinIsPinnedOption) (*PinIsPinnedSettings, error) {
options := &PinIsPinnedSettings{
WithType: "all",
}

for _, opt := range opts {
Expand All @@ -71,6 +87,21 @@ func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
return options, nil
}

// PinRmOptions pin rm options
func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
options := &PinRmSettings{
Recursive: true,
}

for _, opt := range opts {
if err := opt(options); err != nil {
return nil, err
}
}

return options, nil
}

func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
options := &PinUpdateSettings{
Unpin: true,
Expand All @@ -86,36 +117,131 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
return options, nil
}

type pinType struct{}

type pinOpts struct {
Type pinType
Ls pinLsOpts
IsPinned pinIsPinnedOpts
}

var Pin pinOpts

type pinLsOpts struct{}

// All is an option for Pin.Ls which will make it return all pins. It is
// the default
func (pinType) All() PinLsOption {
return Pin.pinType("all")
func (pinLsOpts) All() PinLsOption {
return Pin.Ls.pinType("all")
}

// Recursive is an option for Pin.Ls which will make it only return recursive
// pins
func (pinType) Recursive() PinLsOption {
return Pin.pinType("recursive")
func (pinLsOpts) Recursive() PinLsOption {
return Pin.Ls.pinType("recursive")
}

// Direct is an option for Pin.Ls which will make it only return direct (non
// recursive) pins
func (pinType) Direct() PinLsOption {
return Pin.pinType("direct")
func (pinLsOpts) Direct() PinLsOption {
return Pin.Ls.pinType("direct")
}

// Indirect is an option for Pin.Ls which will make it only return indirect pins
// (objects referenced by other recursively pinned objects)
func (pinType) Indirect() PinLsOption {
return Pin.pinType("indirect")
func (pinLsOpts) Indirect() PinLsOption {
return Pin.Ls.pinType("indirect")
}

// Type is an option for Pin.Ls which will make it only return pins of the given
// type.
//
// 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)
func (pinLsOpts) Type(typeStr string) (PinLsOption, error) {
switch typeStr {
case "all", "direct", "indirect", "recursive":
return Pin.Ls.pinType(typeStr), nil
default:
return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
}
}

// pinType is an option for Pin.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)
func (pinLsOpts) pinType(t string) PinLsOption {
return func(settings *PinLsSettings) error {
settings.Type = t
return nil
}
}

type pinIsPinnedOpts struct{}

// All is an option for Pin.IsPinned which will make it search in all type of pins.
// It is the default
func (pinIsPinnedOpts) All() PinIsPinnedOption {
return Pin.IsPinned.pinType("all")
}

// Recursive is an option for Pin.IsPinned which will make it only search in
// recursive pins
func (pinIsPinnedOpts) Recursive() PinIsPinnedOption {
return Pin.IsPinned.pinType("recursive")
}

// Direct is an option for Pin.IsPinned which will make it only search in direct
// (non recursive) pins
func (pinIsPinnedOpts) Direct() PinIsPinnedOption {
return Pin.IsPinned.pinType("direct")
}

// Indirect is an option for Pin.IsPinned which will make it only search indirect
// pins (objects referenced by other recursively pinned objects)
func (pinIsPinnedOpts) Indirect() PinIsPinnedOption {
return Pin.IsPinned.pinType("indirect")
}

// Type is an option for Pin.IsPinned which will make it only search pins of the given
// type.
//
// 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)
func (pinIsPinnedOpts) Type(typeStr string) (PinIsPinnedOption, error) {
switch typeStr {
case "all", "direct", "indirect", "recursive":
return Pin.IsPinned.pinType(typeStr), nil
default:
return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
}
}

// pinType is an option for Pin.IsPinned which allows to specify which pin type the given
// pin is expected to be, speeding up the research.
//
// 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)
func (pinIsPinnedOpts) pinType(t string) PinIsPinnedOption {
return func(settings *PinIsPinnedSettings) error {
settings.WithType = t
return nil
}
}

// Recursive is an option for Pin.Add which specifies whether to pin an entire
Expand All @@ -137,22 +263,6 @@ func (pinOpts) RmRecursive(recursive bool) PinRmOption {
}
}

// Type is an option for Pin.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)
func (pinOpts) pinType(t string) PinLsOption {
return func(settings *PinLsSettings) error {
settings.Type = t
return nil
}
}

// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
// Default is true.
func (pinOpts) Unpin(unpin bool) PinUpdateOption {
Expand Down
4 changes: 4 additions & 0 deletions pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type PinAPI interface {
// Ls returns list of pinned objects on this node
Ls(context.Context, ...options.PinLsOption) (<-chan Pin, error)

// IsPinned returns whether or not the given cid is pinned
// and an explanation of why its pinned
IsPinned(context.Context, path.Path, ...options.PinIsPinnedOption) (string, bool, error)

// Rm removes pin for object specified by the path
Rm(context.Context, path.Path, ...options.PinRmOption) error

Expand Down
Loading

0 comments on commit c82db2e

Please sign in to comment.