@cryptotaxi247 / kubo / commits / edb8eeaec

core/commands: Created a general key list output type and plaintext marshaler

Matt Bell committed Nov 18, 2014 at 23:08 UTC edb8eeaecf68fe081dad2c22fb34d7eefe55c4a7
1 file changed +15 -13
core/commands/refs.go
+15 -13
@@ -2,6 +2,7 @@ package commands
2
3 import (
4 "fmt"
5 + "strings"
6
7 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 cmds "github.com/jbenet/go-ipfs/commands"
@@ -10,8 +11,16 @@ import (
11 u "github.com/jbenet/go-ipfs/util"
12 )
13
13 -type RefsOutput struct {
14 - Refs []string
14 +// KeyList is a general type for outputting lists of keys
15 +type KeyList struct {
16 + Keys []string
17 +}
18 +
19 +// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
20 +func KeyListTextMarshaler(res cmds.Response) ([]byte, error) {
21 + output := res.Output().(*KeyList)
22 + s := strings.Join(output.Keys, "\n")
23 + return []byte(s), nil
24 }
25
26 var refsCmd = &cmds.Command{
@@ -58,20 +67,13 @@ Note: list all refs recursively with -r.
67
68 return getRefs(n, req.Arguments(), unique, recursive)
69 },
61 - Type: &RefsOutput{},
70 + Type: &KeyList{},
71 Marshalers: cmds.MarshalerMap{
63 - cmds.Text: func(res cmds.Response) ([]byte, error) {
64 - output := res.Output().(*RefsOutput)
65 - s := ""
66 - for _, ref := range output.Refs {
67 - s += fmt.Sprintln(ref)
68 - }
69 - return []byte(s), nil
70 - },
72 + cmds.Text: KeyListTextMarshaler,
73 },
74 }
75
74 -func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*RefsOutput, error) {
76 +func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*KeyList, error) {
77 var refsSeen map[u.Key]bool
78 if unique {
79 refsSeen = make(map[u.Key]bool)
@@ -91,7 +93,7 @@ func getRefs(n *core.IpfsNode, paths []string, unique, recursive bool) (*RefsOut
93 }
94 }
95
94 - return &RefsOutput{refs}, nil
96 + return &KeyList{refs}, nil
97 }
98
99 func addRefs(n *core.IpfsNode, object *dag.Node, refs []string, refsSeen map[u.Key]bool, recursive bool) ([]string, error) {