mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-13 12:18:00 +08:00
core/commands: Added 'cat' command
This commit is contained in:
parent
aea52132cb
commit
afd8fadba8
@ -4,26 +4,34 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/jbenet/go-ipfs/core"
|
||||
cmds "github.com/jbenet/go-ipfs/commands"
|
||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||
)
|
||||
|
||||
func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
|
||||
for _, fn := range args {
|
||||
dagnode, err := n.Resolver.ResolvePath(fn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("catFile error: %v", err)
|
||||
var cat = &cmds.Command{
|
||||
Help: "TODO",
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
node := req.Context().Node
|
||||
fmt.Println(node.Resolver)
|
||||
readers := make([]io.Reader, 0, len(req.Arguments()))
|
||||
|
||||
for _, path := range req.Arguments() {
|
||||
dagnode, err := node.Resolver.ResolvePath(path)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
read, err := uio.NewDagReader(dagnode, node.DAG)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
readers = append(readers, read)
|
||||
}
|
||||
|
||||
read, err := uio.NewDagReader(dagnode, n.DAG)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cat error: %v", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(out, read)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cat error: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
reader := io.MultiReader(readers...)
|
||||
res.SetValue(reader)
|
||||
},
|
||||
}
|
||||
|
||||
@ -43,6 +43,10 @@ Advanced Commands:
|
||||
Use "ipfs help <command>" for more information about a command.
|
||||
`,
|
||||
Subcommands: map[string]*cmds.Command{
|
||||
"cat": cat,
|
||||
|
||||
// test subcommands
|
||||
// TODO: remove these when we don't need them anymore
|
||||
"beep": &cmds.Command{
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
v := &TestOutput{"hello, world", 1337}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user