@cryptotaxi247 / kubo / commits / ef4545a26

refs

Juan Batiz-Benet committed Jul 5, 2014 at 22:19 UTC ef4545a26dc1d7105ff43dfa9f2a5dcc0351a61a
2 files changed +86
cli/ipfs.go
+1
@@ -39,6 +39,7 @@ Use "ipfs help <command>" for more information about a command.
39 cmdIpfsAdd,
40 cmdIpfsCat,
41 cmdIpfsLs,
42 + cmdIpfsRefs,
43 cmdIpfsVersion,
44 cmdIpfsCommands,
45 },
cli/refs.go new
+85
@@ -0,0 +1,85 @@
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 cmdIpfsRefs = &commander.Command{
11 + UsageLine: "refs",
12 + Short: "List link hashes from an object.",
13 + Long: `ipfs refs <ipfs-path> - List link hashes from an object..
14 +
15 + Retrieves the object named by <ipfs-path> and displays the link
16 + hashes it contains, with the following format:
17 +
18 + <link base58 hash>
19 +
20 + Note: list all refs recursively with -r.
21 +
22 +`,
23 + Run: refCmd,
24 + Flag: *flag.NewFlagSet("ipfs-refs", flag.ExitOnError),
25 +}
26 +
27 +func init() {
28 + cmdIpfsRefs.Flag.Bool("r", false, "recursive: list refs recursively")
29 + cmdIpfsRefs.Flag.Bool("u", false, "unique: list each ref only once")
30 +}
31 +
32 +func refCmd(c *commander.Command, inp []string) error {
33 + if len(inp) < 1 {
34 + u.POut(c.Long)
35 + return nil
36 + }
37 +
38 + n, err := localNode()
39 + if err != nil {
40 + return err
41 + }
42 +
43 + recursive := c.Flag.Lookup("r").Value.Get().(bool)
44 + unique := c.Flag.Lookup("u").Value.Get().(bool)
45 + refsSeen := map[u.Key]bool{}
46 +
47 + printRef := func(h mh.Multihash) {
48 + if unique {
49 + _, found := refsSeen[u.Key(h)]
50 + if found {
51 + return
52 + }
53 + refsSeen[u.Key(h)] = true
54 + }
55 +
56 + u.POut("%s\n", h.B58String())
57 + }
58 +
59 + var printRefs func(h mh.Multihash, recursive bool)
60 + printRefs = func(h mh.Multihash, recursive bool) {
61 + nd, err := n.GetDagNode(u.Key(h))
62 + if err != nil {
63 + u.PErr("error: cannot retrieve %s\n", h.B58String())
64 + return
65 + }
66 +
67 + for _, link := range nd.Links {
68 + printRef(link.Hash)
69 + if recursive {
70 + printRefs(link.Hash, recursive)
71 + }
72 + }
73 + }
74 +
75 + for _, fn := range inp {
76 + // for now only hashes, no path resolution
77 + h, err := mh.FromB58String(fn)
78 + if err != nil {
79 + return err
80 + }
81 +
82 + printRefs(h, recursive)
83 + }
84 + return nil
85 +}