mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-23 05:35:58 +08:00
core/commands: Created a general key list output type and plaintext marshaler
This commit is contained in:
@ -2,6 +2,7 @@ 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"
|
||||||
@ -10,8 +11,16 @@ import (
|
|||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RefsOutput struct {
|
// KeyList is a general type for outputting lists of keys
|
||||||
Refs []string
|
type KeyList struct {
|
||||||
|
Keys []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
|
||||||
|
func KeyListTextMarshaler(res cmds.Response) ([]byte, error) {
|
||||||
|
output := res.Output().(*KeyList)
|
||||||
|
s := strings.Join(output.Keys, "\n")
|
||||||
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var refsCmd = &cmds.Command{
|
var refsCmd = &cmds.Command{
|
||||||
@ -58,20 +67,13 @@ Note: list all refs recursively with -r.
|
|||||||
|
|
||||||
return getRefs(n, req.Arguments(), unique, recursive)
|
return getRefs(n, req.Arguments(), unique, recursive)
|
||||||
},
|
},
|
||||||
Type: &RefsOutput{},
|
Type: &KeyList{},
|
||||||
Marshalers: cmds.MarshalerMap{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: KeyListTextMarshaler,
|
||||||
output := res.Output().(*RefsOutput)
|
|
||||||
s := ""
|
|
||||||
for _, ref := range output.Refs {
|
|
||||||
s += fmt.Sprintln(ref)
|
|
||||||
}
|
|
||||||
return []byte(s), nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*RefsOutput, error) {
|
func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*KeyList, error) {
|
||||||
var refsSeen map[u.Key]bool
|
var refsSeen map[u.Key]bool
|
||||||
if unique {
|
if unique {
|
||||||
refsSeen = make(map[u.Key]bool)
|
refsSeen = make(map[u.Key]bool)
|
||||||
@ -91,7 +93,7 @@ func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*RefsOut
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RefsOutput{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 []string, refsSeen map[u.Key]bool, recursive bool) ([]string, error) {
|
||||||
|
Reference in New Issue
Block a user