@cryptotaxi247 / kubo / commits / 9de01007d

core/commands: pin ls: Accept 'type' option to specify type of pinned keys to list

Matt Bell committed Nov 19, 2014 at 00:53 UTC 9de01007d02639301840f1de5ce2dfb074b21c5b
1 file changed +28 -3
core/commands/pin.go
+28 -3
@@ -6,6 +6,7 @@ import (
6 cmds "github.com/jbenet/go-ipfs/commands"
7 "github.com/jbenet/go-ipfs/core"
8 "github.com/jbenet/go-ipfs/merkledag"
9 + u "github.com/jbenet/go-ipfs/util"
10 )
11
12 var pinCmd = &cmds.Command{
@@ -109,15 +110,39 @@ or recursively pinned are not included in the list.
110 `,
111 },
112
113 + Options: []cmds.Option{
114 + cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\""),
115 + },
116 Run: func(req cmds.Request) (interface{}, error) {
117 n, err := req.Context().GetNode()
118 if err != nil {
119 return nil, err
120 }
121
118 - return &KeyList{
119 - Keys: n.Pinning.Set().GetKeys(),
120 - }, nil
122 + typeStr, found, err := req.Option("type").String()
123 + if err != nil {
124 + return nil, err
125 + }
126 + if !found {
127 + typeStr = "all"
128 + }
129 +
130 + if typeStr != "all" && typeStr != "direct" && typeStr != "indirect" && typeStr != "recursive" {
131 + return nil, cmds.ClientError("Invalid type '" + typeStr + "', must be \"direct\", \"indirect\", \"recursive\", or \"all\"")
132 + }
133 +
134 + keys := make([]u.Key, 0)
135 + if typeStr == "direct" || typeStr == "all" {
136 + keys = append(keys, n.Pinning.DirectKeys()...)
137 + }
138 + if typeStr == "indirect" || typeStr == "all" {
139 + keys = append(keys, n.Pinning.IndirectKeys()...)
140 + }
141 + if typeStr == "recursive" || typeStr == "all" {
142 + keys = append(keys, n.Pinning.RecursiveKeys()...)
143 + }
144 +
145 + return &KeyList{Keys: keys}, nil
146 },
147 Type: &KeyList{},
148 Marshalers: cmds.MarshalerMap{