cat.go 1.19 KB
Newer Older
1 2 3 4 5
package commands

import (
	"io"

Matt Bell's avatar
Matt Bell committed
6
	cmds "github.com/jbenet/go-ipfs/commands"
7
	core "github.com/jbenet/go-ipfs/core"
8
	uio "github.com/jbenet/go-ipfs/unixfs/io"
9 10
)

11
var CatCmd = &cmds.Command{
12 13 14 15
	Helptext: cmds.HelpText{
		Tagline: "Show IPFS object data",
		ShortDescription: `
Retrieves the object named by <ipfs-path> and outputs the data
16
it contains.
17 18
`,
	},
19 20

	Arguments: []cmds.Argument{
21
		cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to be outputted"),
22
	},
23
	Run: func(req cmds.Request) (interface{}, error) {
24 25 26 27 28
		node, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}

Matt Bell's avatar
Matt Bell committed
29
		readers := make([]io.Reader, 0, len(req.Arguments()))
30

31
		readers, err = cat(node, req.Arguments())
32
		if err != nil {
33
			return nil, err
34
		}
Matt Bell's avatar
Matt Bell committed
35 36

		reader := io.MultiReader(readers...)
37
		return reader, nil
Matt Bell's avatar
Matt Bell committed
38
	},
39
}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

func cat(node *core.IpfsNode, paths []string) ([]io.Reader, error) {
	readers := make([]io.Reader, 0, len(paths))
	for _, path := range paths {
		dagnode, err := node.Resolver.ResolvePath(path)
		if err != nil {
			return nil, err
		}
		read, err := uio.NewDagReader(dagnode, node.DAG)
		if err != nil {
			return nil, err
		}
		readers = append(readers, read)
	}
	return readers, nil
}