core/commands: Added 'cat' command
Matt Bell committed
Oct 28, 2014 at 23:15 UTC
afd8fadba8f53b8f08f8cbb583f4db064b3e1f31
2 files changed
+28
-16
core/commands/cat.go
+24
-16
@@ -4,26 +4,34 @@ import (
4
"fmt"
5
"io"
6
7
- "github.com/jbenet/go-ipfs/core"
7
+ cmds "github.com/jbenet/go-ipfs/commands"
8
uio "github.com/jbenet/go-ipfs/unixfs/io"
9
)
10
11
-func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
12
- for _, fn := range args {
13
- dagnode, err := n.Resolver.ResolvePath(fn)
14
- if err != nil {
15
- return fmt.Errorf("catFile error: %v", err)
16
- }
11
+var cat = &cmds.Command{
12
+ Help: "TODO",
13
+ Run: func(req cmds.Request, res cmds.Response) {
14
+ node := req.Context().Node
15
+ fmt.Println(node.Resolver)
16
+ readers := make([]io.Reader, 0, len(req.Arguments()))
17
18
- read, err := uio.NewDagReader(dagnode, n.DAG)
19
- if err != nil {
20
- return fmt.Errorf("cat error: %v", err)
21
- }
18
+ for _, path := range req.Arguments() {
19
+ dagnode, err := node.Resolver.ResolvePath(path)
20
+ if err != nil {
21
+ res.SetError(err, cmds.ErrNormal)
22
+ return
23
+ }
24
23
- _, err = io.Copy(out, read)
24
- if err != nil {
25
- return fmt.Errorf("cat error: %v", err)
25
+ read, err := uio.NewDagReader(dagnode, node.DAG)
26
+ if err != nil {
27
+ res.SetError(err, cmds.ErrNormal)
28
+ return
29
+ }
30
+
31
+ readers = append(readers, read)
32
}
27
- }
28
- return nil
33
+
34
+ reader := io.MultiReader(readers...)
35
+ res.SetValue(reader)
36
+ },
37
}
core/commands/root.go
+4
@@ -43,6 +43,10 @@ Advanced Commands:
43
Use "ipfs help <command>" for more information about a command.
44
`,
45
Subcommands: map[string]*cmds.Command{
46
+ "cat": cat,
47
+
48
+ // test subcommands
49
+ // TODO: remove these when we don't need them anymore
50
"beep": &cmds.Command{
51
Run: func(req cmds.Request, res cmds.Response) {
52
v := &TestOutput{"hello, world", 1337}