1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 01:12:24 +08:00
This commit is contained in:
Juan Batiz-Benet
2014-07-05 22:19:54 -07:00
parent e4ee16fac1
commit ef4545a26d
2 changed files with 86 additions and 0 deletions

View File

@ -39,6 +39,7 @@ Use "ipfs help <command>" for more information about a command.
cmdIpfsAdd,
cmdIpfsCat,
cmdIpfsLs,
cmdIpfsRefs,
cmdIpfsVersion,
cmdIpfsCommands,
},

85
cli/refs.go Normal file
View File

@ -0,0 +1,85 @@
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 cmdIpfsRefs = &commander.Command{
UsageLine: "refs",
Short: "List link hashes from an object.",
Long: `ipfs refs <ipfs-path> - List link hashes from an object..
Retrieves the object named by <ipfs-path> and displays the link
hashes it contains, with the following format:
<link base58 hash>
Note: list all refs recursively with -r.
`,
Run: refCmd,
Flag: *flag.NewFlagSet("ipfs-refs", flag.ExitOnError),
}
func init() {
cmdIpfsRefs.Flag.Bool("r", false, "recursive: list refs recursively")
cmdIpfsRefs.Flag.Bool("u", false, "unique: list each ref only once")
}
func refCmd(c *commander.Command, inp []string) error {
if len(inp) < 1 {
u.POut(c.Long)
return nil
}
n, err := localNode()
if err != nil {
return err
}
recursive := c.Flag.Lookup("r").Value.Get().(bool)
unique := c.Flag.Lookup("u").Value.Get().(bool)
refsSeen := map[u.Key]bool{}
printRef := func(h mh.Multihash) {
if unique {
_, found := refsSeen[u.Key(h)]
if found {
return
}
refsSeen[u.Key(h)] = true
}
u.POut("%s\n", h.B58String())
}
var printRefs func(h mh.Multihash, recursive bool)
printRefs = func(h mh.Multihash, recursive bool) {
nd, err := n.GetDagNode(u.Key(h))
if err != nil {
u.PErr("error: cannot retrieve %s\n", h.B58String())
return
}
for _, link := range nd.Links {
printRef(link.Hash)
if recursive {
printRefs(link.Hash, recursive)
}
}
}
for _, fn := range inp {
// for now only hashes, no path resolution
h, err := mh.FromB58String(fn)
if err != nil {
return err
}
printRefs(h, recursive)
}
return nil
}