cat.go 971 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package main

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"fmt"
5 6
	"io"
	"os"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9
	"github.com/gonuts/flag"
	"github.com/jbenet/commander"
10
	dag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	u "github.com/jbenet/go-ipfs/util"
)

var cmdIpfsCat = &commander.Command{
	UsageLine: "cat",
	Short:     "Show ipfs object data.",
	Long: `ipfs cat <ipfs-path> - Show ipfs object data.

    Retrieves the object named by <ipfs-path> and displays the Data
    it contains.
`,
	Run:  catCmd,
	Flag: *flag.NewFlagSet("ipfs-cat", flag.ExitOnError),
}

func catCmd(c *commander.Command, inp []string) error {
	if len(inp) < 1 {
		u.POut(c.Long)
		return nil
	}

32
	n, err := localNode(false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
	for _, fn := range inp {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
		nd, err := n.Resolver.ResolvePath(fn)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42
		if err != nil {
			return err
		}

43
		read, err := dag.NewDagReader(nd, n.DAG)
44 45 46 47 48 49 50 51 52 53 54 55 56
		if err != nil {
			fmt.Println(err)
			continue
		}

		_, err = io.Copy(os.Stdout, read)
		if err != nil {
			fmt.Println(err)
			continue
		}
	}
	return nil
}