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

Commit

Permalink
Merge pull request #240 from ani1311/feat/type-for-pin-ls
Browse files Browse the repository at this point in the history
Added Type field for Pin ls command
  • Loading branch information
Stebalien committed Apr 22, 2021
2 parents 009d1b1 + 7f18df8 commit eaf54e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
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

0 comments on commit eaf54e8

Please sign in to comment.