提交 afd8fadb 作者: Matt Bell 提交者: Juan Batiz-Benet

core/commands: Added 'cat' command

上级 aea52132
...@@ -4,26 +4,34 @@ import ( ...@@ -4,26 +4,34 @@ import (
"fmt" "fmt"
"io" "io"
"github.com/jbenet/go-ipfs/core" cmds "github.com/jbenet/go-ipfs/commands"
uio "github.com/jbenet/go-ipfs/unixfs/io" uio "github.com/jbenet/go-ipfs/unixfs/io"
) )
func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { var cat = &cmds.Command{
for _, fn := range args { Help: "TODO",
dagnode, err := n.Resolver.ResolvePath(fn) Run: func(req cmds.Request, res cmds.Response) {
if err != nil { node := req.Context().Node
return fmt.Errorf("catFile error: %v", err) fmt.Println(node.Resolver)
} readers := make([]io.Reader, 0, len(req.Arguments()))
read, err := uio.NewDagReader(dagnode, n.DAG) for _, path := range req.Arguments() {
dagnode, err := node.Resolver.ResolvePath(path)
if err != nil { if err != nil {
return fmt.Errorf("cat error: %v", err) res.SetError(err, cmds.ErrNormal)
return
} }
_, err = io.Copy(out, read) read, err := uio.NewDagReader(dagnode, node.DAG)
if err != nil { if err != nil {
return fmt.Errorf("cat error: %v", err) res.SetError(err, cmds.ErrNormal)
return
} }
readers = append(readers, read)
} }
return nil
reader := io.MultiReader(readers...)
res.SetValue(reader)
},
} }
...@@ -43,6 +43,10 @@ Advanced Commands: ...@@ -43,6 +43,10 @@ Advanced Commands:
Use "ipfs help <command>" for more information about a command. Use "ipfs help <command>" for more information about a command.
`, `,
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"cat": cat,
// test subcommands
// TODO: remove these when we don't need them anymore
"beep": &cmds.Command{ "beep": &cmds.Command{
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
v := &TestOutput{"hello, world", 1337} v := &TestOutput{"hello, world", 1337}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论