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

import (
4
	"os"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5

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

31 32 33 34 35
	expanded, err := u.ExpandPathnames(inp)
	if err != nil {
		return err
	}

36 37
	com := daemon.NewCommand()
	com.Command = "cat"
38
	com.Args = expanded
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39

40
	err = daemon.SendCommand(com, "localhost:12345")
41
	if err != nil {
Jeromy's avatar
Jeromy committed
42 43
		conf := getConfig(c.Parent)
		n, err := localNode(conf, false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47
		if err != nil {
			return err
		}

48
		return commands.Cat(n, com.Args, com.Opts, os.Stdout)
49 50 51
	}
	return nil
}