cat.go 1.07 KB
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 5
	"fmt"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	"github.com/gonuts/flag"
	"github.com/jbenet/commander"
	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
	}

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

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

40
		fmt.Println("Printing Data!")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44
		_, err = fmt.Printf("%s", nd.Data)
		if err != nil {
			return err
		}
45 46 47 48 49 50 51 52 53 54 55

		fmt.Println("Printing child nodes:")
		for _, subn := range nd.Links {
			k := u.Key(subn.Hash)
			blk, err := n.Blocks.GetBlock(k)
			fmt.Printf("Getting link: %s\n", k.Pretty())
			if err != nil {
				return err
			}
			fmt.Println(string(blk.Data))
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58
	return nil
}