Skip to content

Commit

Permalink
core/commands: pin ls: Accept 'type' option to specify type of pinned…
Browse files Browse the repository at this point in the history
… keys to list
  • Loading branch information
mappum committed Nov 19, 2014
1 parent e6091be commit 9de0100
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
)

var pinCmd = &cmds.Command{
Expand Down Expand Up @@ -109,15 +110,39 @@ or recursively pinned are not included in the list.
`,
},

Options: []cmds.Option{
cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\""),
},
Run: func(req cmds.Request) (interface{}, error) {
n, err := req.Context().GetNode()
if err != nil {
return nil, err
}

return &KeyList{
Keys: n.Pinning.Set().GetKeys(),
}, nil
typeStr, found, err := req.Option("type").String()
if err != nil {
return nil, err
}
if !found {
typeStr = "all"
}

if typeStr != "all" && typeStr != "direct" && typeStr != "indirect" && typeStr != "recursive" {
return nil, cmds.ClientError("Invalid type '" + typeStr + "', must be \"direct\", \"indirect\", \"recursive\", or \"all\"")
}

keys := make([]u.Key, 0)
if typeStr == "direct" || typeStr == "all" {
keys = append(keys, n.Pinning.DirectKeys()...)
}
if typeStr == "indirect" || typeStr == "all" {
keys = append(keys, n.Pinning.IndirectKeys()...)
}
if typeStr == "recursive" || typeStr == "all" {
keys = append(keys, n.Pinning.RecursiveKeys()...)
}

return &KeyList{Keys: keys}, nil
},
Type: &KeyList{},
Marshalers: cmds.MarshalerMap{
Expand Down

0 comments on commit 9de0100

Please sign in to comment.