1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 02:21:48 +08:00

core/commands: Made 'refs' output u.Keys instead of strings

This commit is contained in:
Matt Bell
2014-11-18 23:20:20 -08:00
parent 477c246c6c
commit db53de806c

View File

@ -2,7 +2,6 @@ package commands
import ( import (
"fmt" "fmt"
"strings"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
@ -13,13 +12,16 @@ import (
// KeyList is a general type for outputting lists of keys // KeyList is a general type for outputting lists of keys
type KeyList struct { type KeyList struct {
Keys []string Keys []u.Key
} }
// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line // KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
func KeyListTextMarshaler(res cmds.Response) ([]byte, error) { func KeyListTextMarshaler(res cmds.Response) ([]byte, error) {
output := res.Output().(*KeyList) output := res.Output().(*KeyList)
s := strings.Join(output.Keys, "\n") s := ""
for _, key := range output.Keys {
s += key.B58String() + "\n"
}
return []byte(s), nil return []byte(s), nil
} }
@ -79,7 +81,7 @@ func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*KeyList
refsSeen = make(map[u.Key]bool) refsSeen = make(map[u.Key]bool)
} }
refs := make([]string, 0) refs := make([]u.Key, 0)
for _, path := range paths { for _, path := range paths {
object, err := n.Resolver.ResolvePath(path) object, err := n.Resolver.ResolvePath(path)
@ -96,7 +98,7 @@ func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*KeyList
return &KeyList{refs}, nil return &KeyList{refs}, nil
} }
func addRefs(n *core.IpfsNode, object *dag.Node, refs []string, refsSeen map[u.Key]bool, recursive bool) ([]string, error) { func addRefs(n *core.IpfsNode, object *dag.Node, refs []u.Key, refsSeen map[u.Key]bool, recursive bool) ([]u.Key, error) {
for _, link := range object.Links { for _, link := range object.Links {
var found bool var found bool
found, refs = addRef(link.Hash, refs, refsSeen) found, refs = addRef(link.Hash, refs, refsSeen)
@ -117,15 +119,16 @@ func addRefs(n *core.IpfsNode, object *dag.Node, refs []string, refsSeen map[u.K
return refs, nil return refs, nil
} }
func addRef(h mh.Multihash, refs []string, refsSeen map[u.Key]bool) (bool, []string) { func addRef(h mh.Multihash, refs []u.Key, refsSeen map[u.Key]bool) (bool, []u.Key) {
key := u.Key(h)
if refsSeen != nil { if refsSeen != nil {
_, found := refsSeen[u.Key(h)] _, found := refsSeen[key]
if found { if found {
return true, refs return true, refs
} }
refsSeen[u.Key(h)] = true refsSeen[key] = true
} }
refs = append(refs, h.B58String()) refs = append(refs, key)
return false, refs return false, refs
} }