core/commands/pin: make 'ipfs pin ls' accept path args
License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Christian Couder committed
Jan 12, 2016 at 22:36 UTC
4244f71dcd48e7f82f5a9e1fee19f7f3c6e41627
1 file changed
+70
-8
core/commands/pin.go
+70
-8
@@ -7,8 +7,11 @@ import (
7
8
key "github.com/ipfs/go-ipfs/blocks/key"
9
cmds "github.com/ipfs/go-ipfs/commands"
10
+ context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11
+ core "github.com/ipfs/go-ipfs/core"
12
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
13
dag "github.com/ipfs/go-ipfs/merkledag"
14
+ path "github.com/ipfs/go-ipfs/path"
15
u "github.com/ipfs/go-ipfs/util"
16
)
17
@@ -183,6 +186,9 @@ Example:
186
`,
187
},
188
189
+ Arguments: []cmds.Argument{
190
+ cmds.StringArg("ipfs-path", false, true, "Path to object(s) to be listed"),
191
+ },
192
Options: []cmds.Option{
193
cmds.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\". Defaults to \"recursive\""),
194
cmds.BoolOption("count", "n", "Show refcount when listing indirect pins"),
@@ -195,20 +201,40 @@ Example:
201
return
202
}
203
198
- typeStr, found, err := req.Option("type").String()
204
+ typeStr, typeStrFound, err := req.Option("type").String()
205
if err != nil {
206
res.SetError(err, cmds.ErrNormal)
207
return
208
}
203
- if !found {
204
- typeStr = "recursive"
209
+
210
+ if typeStrFound {
211
+ switch typeStr {
212
+ case "all", "direct", "indirect", "recursive":
213
+ default:
214
+ err = fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
215
+ res.SetError(err, cmds.ErrClient)
216
+ return
217
+ }
218
}
219
207
- switch typeStr {
208
- case "all", "direct", "indirect", "recursive":
209
- default:
210
- err = fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
211
- res.SetError(err, cmds.ErrClient)
220
+ if len(req.Arguments()) > 0 {
221
+ if !typeStrFound {
222
+ typeStr = "all"
223
+ }
224
+
225
+ keys, err := pinLsKeys(req.Arguments(), typeStr, req.Context(), n)
226
+
227
+ if err != nil {
228
+ res.SetError(err, cmds.ErrNormal)
229
+ } else {
230
+ res.SetOutput(&RefKeyList{Keys: keys})
231
+ }
232
+
233
+ return
234
+ }
235
+
236
+ if !typeStrFound {
237
+ typeStr = "recursive"
238
}
239
240
keys := make(map[string]RefKeyObject)
@@ -278,3 +304,39 @@ type RefKeyObject struct {
304
type RefKeyList struct {
305
Keys map[string]RefKeyObject
306
}
307
+
308
+func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsNode) (map[string]RefKeyObject, error) {
309
+ keys := make(map[string]RefKeyObject)
310
+
311
+ for _, p := range args {
312
+ dagNode, err := core.Resolve(ctx, n, path.Path(p))
313
+ if err != nil {
314
+ return nil, err
315
+ }
316
+
317
+ k, err := dagNode.Key()
318
+ if err != nil {
319
+ return nil, err
320
+ }
321
+
322
+ pinType, pinned, err := n.Pinning.IsPinnedWithType(k, typeStr)
323
+ if err != nil {
324
+ return nil, err
325
+ }
326
+
327
+ if !pinned {
328
+ return nil, fmt.Errorf("Path '%s' is not pinned", p)
329
+ }
330
+
331
+ switch pinType {
332
+ case "direct", "indirect", "recursive", "internal":
333
+ default:
334
+ pinType = "indirect through " + pinType
335
+ }
336
+ keys[k.B58String()] = RefKeyObject{
337
+ Type: pinType,
338
+ }
339
+ }
340
+
341
+ return keys, nil
342
+}