github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands/refs.go (about) 1 package commands 2 3 import ( 4 "io" 5 6 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" 7 "github.com/jbenet/go-ipfs/core" 8 mdag "github.com/jbenet/go-ipfs/merkledag" 9 u "github.com/jbenet/go-ipfs/util" 10 ) 11 12 func Refs(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { 13 unique, ok := opts["u"].(bool) 14 if !ok { 15 unique = false 16 } 17 18 recursive, ok := opts["r"].(bool) 19 if !ok { 20 recursive = false 21 } 22 23 var refsSeen map[u.Key]bool 24 if unique { 25 refsSeen = make(map[u.Key]bool) 26 } 27 28 for _, fn := range args { 29 nd, err := n.Resolver.ResolvePath(fn) 30 if err != nil { 31 return err 32 } 33 34 printRefs(n, nd, refsSeen, recursive) 35 } 36 return nil 37 } 38 39 func printRefs(n *core.IpfsNode, nd *mdag.Node, refSeen map[u.Key]bool, recursive bool) { 40 for _, link := range nd.Links { 41 printRef(link.Hash, refSeen) 42 43 if recursive { 44 nd, err := n.DAG.Get(u.Key(link.Hash)) 45 if err != nil { 46 log.Errorf("error: cannot retrieve %s (%s)", link.Hash.B58String(), err) 47 return 48 } 49 50 printRefs(n, nd, refSeen, recursive) 51 } 52 } 53 } 54 55 func printRef(h mh.Multihash, refsSeen map[u.Key]bool) { 56 if refsSeen != nil { 57 _, found := refsSeen[u.Key(h)] 58 if found { 59 return 60 } 61 refsSeen[u.Key(h)] = true 62 } 63 64 u.POut("%s\n", h.B58String()) 65 }