@cryptotaxi247 / kubo / commits / 180b9b32d

refs: added --format option

Juan Batiz-Benet committed Jan 7, 2015 at 13:42 UTC 180b9b32d3ec76be43b2806fdecb4afa0fcd4a4e
1 file changed +23 -6
core/commands/refs.go
+23 -6
@@ -3,6 +3,7 @@ package commands
3 import (
4 "bytes"
5 "io"
6 + "strings"
7 "sync"
8
9 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
@@ -45,7 +46,8 @@ Note: list all refs recursively with -r.
46 cmds.StringArg("ipfs-path", true, true, "Path to the object(s) to list refs from"),
47 },
48 Options: []cmds.Option{
48 - cmds.BoolOption("edges", "e", "Emit edge format: <from> -> <to>"),
49 + cmds.StringOption("format", "Emit edges with given format. tokens: <src> <dst> <linkname>"),
50 + cmds.BoolOption("edges", "e", "Emit edge format: `<from> -> <to>`"),
51 cmds.BoolOption("unique", "u", "Omit duplicate refs from output"),
52 cmds.BoolOption("recursive", "r", "Recursively list links of child nodes"),
53 },
@@ -70,6 +72,11 @@ Note: list all refs recursively with -r.
72 return nil, err
73 }
74
75 + format, _, err := req.Option("format").String()
76 + if err != nil {
77 + return nil, err
78 + }
79 +
80 objs, err := objectsForPaths(n, req.Arguments())
81 if err != nil {
82 return nil, err
@@ -87,6 +94,7 @@ Note: list all refs recursively with -r.
94 Ctx: n.Context(),
95 Unique: unique,
96 PrintEdge: edges,
97 + PrintFmt: format,
98 Recursive: recursive,
99 }
100
@@ -159,6 +167,7 @@ type RefWriter struct {
167 Unique bool
168 Recursive bool
169 PrintEdge bool
170 + PrintFmt string
171
172 seen map[u.Key]struct{}
173 }
@@ -182,7 +191,7 @@ func (rw *RefWriter) WriteRefs(n *dag.Node) (int, error) {
191 continue
192 }
193
185 - if err := rw.WriteEdge(nkey, lk); err != nil {
194 + if err := rw.WriteEdge(nkey, lk, l.Name); err != nil {
195 return count, err
196 }
197 count++
@@ -223,7 +232,7 @@ func (rw *RefWriter) skip(k u.Key) bool {
232 }
233
234 // Write one edge
226 -func (rw *RefWriter) WriteEdge(from, to u.Key) error {
235 +func (rw *RefWriter) WriteEdge(from, to u.Key, linkname string) error {
236 if rw.Ctx != nil {
237 select {
238 case <-rw.Ctx.Done(): // just in case.
@@ -233,10 +242,18 @@ func (rw *RefWriter) WriteEdge(from, to u.Key) error {
242 }
243
244 var s string
236 - if rw.PrintEdge {
237 - s = from.Pretty() + " -> "
245 + switch {
246 + case rw.PrintFmt != "":
247 + s = rw.PrintFmt
248 + s = strings.Replace(s, "<src>", from.Pretty(), -1)
249 + s = strings.Replace(s, "<dst>", to.Pretty(), -1)
250 + s = strings.Replace(s, "<linkname>", linkname, -1)
251 + case rw.PrintEdge:
252 + s = from.Pretty() + " -> " + to.Pretty()
253 + default:
254 + s += to.Pretty()
255 }
239 - s += to.Pretty() + "\n"
256 + s += "\n"
257
258 if _, err := rw.W.Write([]byte(s)); err != nil {
259 return err