Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Added Type field for Pin ls command #240

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ func (s *Shell) Unpin(path string) error {
Exec(context.Background(), nil)
}

type PinType string

const (
DirectPin = "direct"
RecursivePin = "recursive"
IndirectPin = "indirect"
DirectPin PinType = "direct"
RecursivePin PinType = "recursive"
IndirectPin PinType = "indirect"
)

type PinInfo struct {
Expand All @@ -237,6 +239,12 @@ func (s *Shell) Pins() (map[string]PinInfo, error) {
return raw.Keys, s.Request("pin/ls").Exec(context.Background(), &raw)
}

// Pins returns a map of the pins of specified type (DirectPin, RecursivePin, or IndirectPin)
func (s *Shell) PinsOfType(ctx context.Context, pinType PinType) (map[string]PinInfo, error) {
var raw struct{ Keys map[string]PinInfo }
return raw.Keys, s.Request("pin/ls").Option("type", pinType).Exec(ctx, &raw)
}

// PinStreamInfo is the output type for PinsStream
type PinStreamInfo struct {
Cid string
Expand Down
34 changes: 34 additions & 0 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ func TestPins(t *testing.T) {
is.Equal(info.Type, RecursivePin)
}

func TestPinsOfType(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)

// Add a thing, which pins it by default
h, err := s.Add(bytes.NewBufferString("go-ipfs-api pins test 9F3D1F30-D12A-4024-9477-8F0C8E4B3A63"))
is.Nil(err)

pins, err := s.PinsOfType(context.Background(), RecursivePin)
is.Nil(err)

_, ok := pins[h]
is.True(ok)

err = s.Unpin(h)
is.Nil(err)

pins, err = s.Pins()
is.Nil(err)

_, ok = pins[h]
is.False(ok)

err = s.Pin(h)
is.Nil(err)

pins, err = s.Pins()
is.Nil(err)

info, ok := pins[h]
is.True(ok)
is.Equal(info.Type, RecursivePin)
}

func TestPinsStream(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
Expand Down