From 9de01007d02639301840f1de5ce2dfb074b21c5b Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Wed, 19 Nov 2014 00:53:57 -0800 Subject: [PATCH] core/commands: pin ls: Accept 'type' option to specify type of pinned keys to list --- core/commands/pin.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/core/commands/pin.go b/core/commands/pin.go index 5340177b015..656bc321d8a 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -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{ @@ -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{