Commit 3b241486 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

ipfs cat

parent 62822145
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
}
// for now only hashes, no path resolution
h, err := mh.FromB58String(inp[0])
if err != nil {
return err
}
n, err := localNode()
if err != nil {
return err
}
nd, err := n.GetDagNode(u.Key(h))
if err != nil {
return err
}
u.POut("%s", nd.Data)
return nil
}
......@@ -37,6 +37,7 @@ Use "ipfs help <command>" for more information about a command.
Run: ipfsCmd,
Subcommands: []*commander.Command{
cmdIpfsAdd,
cmdIpfsCat,
cmdIpfsVersion,
cmdIpfsCommands,
},
......@@ -52,7 +53,7 @@ func main() {
err := CmdIpfs.Dispatch(os.Args[1:])
if err != nil {
if len(err.Error()) > 0 {
fmt.Fprintf(os.Stderr, "%v\n", err)
fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
}
os.Exit(1)
}
......
......@@ -83,3 +83,12 @@ func (n *IpfsNode) AddDagNode(nd *merkledag.Node) (u.Key, error) {
return n.Blocks.AddBlock(b)
}
func (n *IpfsNode) GetDagNode(k u.Key) (*merkledag.Node, error) {
b, err := n.Blocks.GetBlock(k)
if err != nil {
return nil, err
}
return merkledag.Decoded(b.Data)
}
......@@ -71,3 +71,9 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
return n.encoded, nil
}
func Decoded(encoded []byte) (*Node, error) {
n := &Node{}
err := n.Unmarshal(encoded)
return n, err
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment