ipfs cat
Juan Batiz-Benet committed
Jul 5, 2014 at 17:29 UTC
3b241486a969ae717d585980774e915882713022
4 files changed
+63
-1
cli/cat.go
new
+46
@@ -0,0 +1,46 @@
1
+package main
2
+
3
+import (
4
+ "github.com/gonuts/flag"
5
+ "github.com/jbenet/commander"
6
+ u "github.com/jbenet/go-ipfs/util"
7
+ mh "github.com/jbenet/go-multihash"
8
+)
9
+
10
+var cmdIpfsCat = &commander.Command{
11
+ UsageLine: "cat",
12
+ Short: "Show ipfs object data.",
13
+ Long: `ipfs cat <ipfs-path> - Show ipfs object data.
14
+
15
+ Retrieves the object named by <ipfs-path> and displays the Data
16
+ it contains.
17
+`,
18
+ Run: catCmd,
19
+ Flag: *flag.NewFlagSet("ipfs-cat", flag.ExitOnError),
20
+}
21
+
22
+func catCmd(c *commander.Command, inp []string) error {
23
+ if len(inp) < 1 {
24
+ u.POut(c.Long)
25
+ return nil
26
+ }
27
+
28
+ // for now only hashes, no path resolution
29
+ h, err := mh.FromB58String(inp[0])
30
+ if err != nil {
31
+ return err
32
+ }
33
+
34
+ n, err := localNode()
35
+ if err != nil {
36
+ return err
37
+ }
38
+
39
+ nd, err := n.GetDagNode(u.Key(h))
40
+ if err != nil {
41
+ return err
42
+ }
43
+
44
+ u.POut("%s", nd.Data)
45
+ return nil
46
+}
cli/ipfs.go
+2
-1
@@ -37,6 +37,7 @@ Use "ipfs help <command>" for more information about a command.
37
Run: ipfsCmd,
38
Subcommands: []*commander.Command{
39
cmdIpfsAdd,
40
+ cmdIpfsCat,
41
cmdIpfsVersion,
42
cmdIpfsCommands,
43
},
@@ -52,7 +53,7 @@ func main() {
53
err := CmdIpfs.Dispatch(os.Args[1:])
54
if err != nil {
55
if len(err.Error()) > 0 {
55
- fmt.Fprintf(os.Stderr, "%v\n", err)
56
+ fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
57
}
58
os.Exit(1)
59
}
core/core.go
+9
@@ -83,3 +83,12 @@ func (n *IpfsNode) AddDagNode(nd *merkledag.Node) (u.Key, error) {
83
84
return n.Blocks.AddBlock(b)
85
}
86
+
87
+func (n *IpfsNode) GetDagNode(k u.Key) (*merkledag.Node, error) {
88
+ b, err := n.Blocks.GetBlock(k)
89
+ if err != nil {
90
+ return nil, err
91
+ }
92
+
93
+ return merkledag.Decoded(b.Data)
94
+}
merkledag/coding.go
+6
@@ -71,3 +71,9 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
71
72
return n.encoded, nil
73
}
74
+
75
+func Decoded(encoded []byte) (*Node, error) {
76
+ n := &Node{}
77
+ err := n.Unmarshal(encoded)
78
+ return n, err
79
+}