cat.go 884 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
package main

import (
	"github.com/gonuts/flag"
	"github.com/jbenet/commander"
	u "github.com/jbenet/go-ipfs/util"
	mh "github.com/jbenet/go-multihash"
)

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
	}

	n, err := localNode()
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36 37 38
	for _, fn := range inp {
		// for now only hashes, no path resolution
		h, err := mh.FromB58String(fn)
		if err != nil {
			return err
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39

40
		nd, err := n.DAG.Get(u.Key(h))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46
		if err != nil {
			return err
		}

		u.POut("%s", nd.Data)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
	return nil
}