core/commands2: Added 'ls' command
Matt Bell committed
Oct 30, 2014 at 18:29 UTC
f2271356f5c879270535303f9587350496329679
2 files changed
+61
core/commands2/ls.go
new
+60
@@ -0,0 +1,60 @@
1
+package commands
2
+
3
+import (
4
+ "fmt"
5
+
6
+ cmds "github.com/jbenet/go-ipfs/commands"
7
+)
8
+
9
+type Link struct {
10
+ Name, Hash string
11
+ Size uint64
12
+}
13
+
14
+var ls = &cmds.Command{
15
+ Help: "TODO",
16
+ Run: func(res cmds.Response, req cmds.Request) {
17
+ node := req.Context().Node
18
+ output := make(map[string][]Link, len(req.Arguments()))
19
+
20
+ for _, path := range req.Arguments() {
21
+ dagnode, err := node.Resolver.ResolvePath(path)
22
+ if err != nil {
23
+ res.SetError(err, cmds.ErrNormal)
24
+ return
25
+ }
26
+
27
+ output[path] = make([]Link, len(dagnode.Links))
28
+ for i, link := range dagnode.Links {
29
+ output[path][i] = Link{
30
+ Name: link.Name,
31
+ Hash: link.Hash.B58String(),
32
+ Size: link.Size,
33
+ }
34
+ }
35
+ }
36
+
37
+ res.SetValue(output)
38
+ },
39
+ Format: func(res cmds.Response) (string, error) {
40
+ s := ""
41
+ output := res.Value().(*map[string][]Link)
42
+
43
+ for path, links := range *output {
44
+ if len(*output) > 1 {
45
+ s += fmt.Sprintf("%s:\n", path)
46
+ }
47
+
48
+ for _, link := range links {
49
+ s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
50
+ }
51
+
52
+ if len(*output) > 1 {
53
+ s += "\n"
54
+ }
55
+ }
56
+
57
+ return s, nil
58
+ },
59
+ Type: &map[string][]Link{},
60
+}
core/commands2/root.go
+1
@@ -52,6 +52,7 @@ Use "ipfs help <command>" for more information about a command.
52
`,
53
Subcommands: map[string]*cmds.Command{
54
"cat": cat,
55
+ "ls": ls,
56
57
// test subcommands
58
// TODO: remove these when we don't need them anymore