@cryptotaxi247 / kubo / commits / faec2a328

cleanup dht cmd output and fix unrecognized events

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jan 2, 2016 at 08:27 UTC faec2a32806b6332d7ca7edc7fcdcc6f3e07fbf8
1 file changed +100 -132
core/commands/dht.go
+100 -132
@@ -103,29 +103,7 @@ var queryDhtCmd = &cmds.Command{
103 verbose, _, _ := res.Request().Option("v").Bool()
104
105 buf := new(bytes.Buffer)
106 - if verbose {
107 - fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
108 - }
109 - switch obj.Type {
110 - case notif.FinalPeer:
111 - fmt.Fprintf(buf, "%s\n", obj.ID)
112 - case notif.PeerResponse:
113 - if verbose {
114 - fmt.Fprintf(buf, "* %s says use ", obj.ID)
115 - for _, p := range obj.Responses {
116 - fmt.Fprintf(buf, "%s ", p.ID)
117 - }
118 - fmt.Fprintln(buf)
119 - }
120 - case notif.SendingQuery:
121 - if verbose {
122 - fmt.Fprintf(buf, "* querying %s\n", obj.ID)
123 - }
124 - case notif.QueryError:
125 - fmt.Fprintf(buf, "error: %s\n", obj.Extra)
126 - default:
127 - fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
128 - }
106 + printEvent(obj, buf, verbose, nil)
107 return buf, nil
108 }
109
@@ -201,50 +179,34 @@ FindProviders will return a list of peers who are able to provide the value requ
179 }
180
181 verbose, _, _ := res.Request().Option("v").Bool()
204 -
205 - marshal := func(v interface{}) (io.Reader, error) {
206 - obj, ok := v.(*notif.QueryEvent)
207 - if !ok {
208 - return nil, u.ErrCast()
209 - }
210 -
211 - buf := new(bytes.Buffer)
212 - if verbose {
213 - fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
214 - }
215 - switch obj.Type {
216 - case notif.FinalPeer:
182 + pfm := pfuncMap{
183 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
184 if verbose {
218 - fmt.Fprintf(buf, "* closest peer %s\n", obj.ID)
185 + fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
186 }
220 - case notif.Provider:
187 + },
188 + notif.Provider: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
189 prov := obj.Responses[0]
190 if verbose {
223 - fmt.Fprintf(buf, "provider: ")
191 + fmt.Fprintf(out, "provider: ")
192 }
225 - fmt.Fprintf(buf, "%s\n", prov.ID.Pretty())
193 + fmt.Fprintf(out, "%s\n", prov.ID.Pretty())
194 if verbose {
195 for _, a := range prov.Addrs {
228 - fmt.Fprintf(buf, "\t%s\n", a)
196 + fmt.Fprintf(out, "\t%s\n", a)
197 }
198 }
231 - case notif.PeerResponse:
232 - if verbose {
233 - fmt.Fprintf(buf, "* %s says use ", obj.ID)
234 - for _, p := range obj.Responses {
235 - fmt.Fprintf(buf, "%s ", p.ID)
236 - }
237 - fmt.Fprintln(buf)
238 - }
239 - case notif.SendingQuery:
240 - if verbose {
241 - fmt.Fprintf(buf, "* querying %s\n", obj.ID)
242 - }
243 - case notif.QueryError:
244 - fmt.Fprintf(buf, "error: %s\n", obj.Extra)
245 - default:
246 - fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
199 + },
200 + }
201 +
202 + marshal := func(v interface{}) (io.Reader, error) {
203 + obj, ok := v.(*notif.QueryEvent)
204 + if !ok {
205 + return nil, u.ErrCast()
206 }
207 +
208 + buf := new(bytes.Buffer)
209 + printEvent(obj, buf, verbose, pfm)
210 return buf, nil
211 }
212
@@ -323,6 +285,15 @@ var findPeerDhtCmd = &cmds.Command{
285 return nil, u.ErrCast()
286 }
287
288 + pfm := pfuncMap{
289 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
290 + pi := obj.Responses[0]
291 + fmt.Fprintf(out, "%s\n", pi.ID)
292 + for _, a := range pi.Addrs {
293 + fmt.Fprintf(out, "\t%s\n", a)
294 + }
295 + },
296 + }
297 marshal := func(v interface{}) (io.Reader, error) {
298 obj, ok := v.(*notif.QueryEvent)
299 if !ok {
@@ -330,27 +301,7 @@ var findPeerDhtCmd = &cmds.Command{
301 }
302
303 buf := new(bytes.Buffer)
333 - fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
334 - switch obj.Type {
335 - case notif.FinalPeer:
336 - pi := obj.Responses[0]
337 - fmt.Fprintf(buf, "%s\n", pi.ID)
338 - for _, a := range pi.Addrs {
339 - fmt.Fprintf(buf, "\t%s\n", a)
340 - }
341 - case notif.PeerResponse:
342 - fmt.Fprintf(buf, "* %s says use ", obj.ID)
343 - for _, p := range obj.Responses {
344 - fmt.Fprintf(buf, "%s ", p.ID)
345 - }
346 - fmt.Fprintln(buf)
347 - case notif.SendingQuery:
348 - fmt.Fprintf(buf, "* querying %s\n", obj.ID)
349 - case notif.QueryError:
350 - fmt.Fprintf(buf, "error: %s\n", obj.Extra)
351 - default:
352 - fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
353 - }
304 + printEvent(obj, buf, true, pfm)
305 return buf, nil
306 }
307
@@ -435,6 +386,15 @@ GetValue will return the value stored in the dht at the given key.
386
387 verbose, _, _ := res.Request().Option("v").Bool()
388
389 + pfm := pfuncMap{
390 + notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
391 + if verbose {
392 + fmt.Fprintf(out, "got value: '%s'\n", obj.Extra)
393 + } else {
394 + fmt.Fprintln(out, obj.Extra)
395 + }
396 + },
397 + }
398 marshal := func(v interface{}) (io.Reader, error) {
399 obj, ok := v.(*notif.QueryEvent)
400 if !ok {
@@ -442,33 +402,9 @@ GetValue will return the value stored in the dht at the given key.
402 }
403
404 buf := new(bytes.Buffer)
445 - if verbose {
446 - fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
447 - }
448 - switch obj.Type {
449 - case notif.PeerResponse:
450 - if verbose {
451 - fmt.Fprintf(buf, "* %s says use ", obj.ID)
452 - for _, p := range obj.Responses {
453 - fmt.Fprintf(buf, "%s ", p.ID)
454 - }
455 - fmt.Fprintln(buf)
456 - }
457 - case notif.SendingQuery:
458 - if verbose {
459 - fmt.Fprintf(buf, "* querying %s\n", obj.ID)
460 - }
461 - case notif.Value:
462 - if verbose {
463 - fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra)
464 - } else {
465 - buf.WriteString(obj.Extra)
466 - }
467 - case notif.QueryError:
468 - fmt.Fprintf(buf, "error: %s\n", obj.Extra)
469 - default:
470 - fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
471 - }
405 +
406 + printEvent(obj, buf, verbose, pfm)
407 +
408 return buf, nil
409 }
410
@@ -550,6 +486,16 @@ PutValue will store the given key value pair in the dht.
486 }
487
488 verbose, _, _ := res.Request().Option("v").Bool()
489 + pfm := pfuncMap{
490 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
491 + if verbose {
492 + fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
493 + }
494 + },
495 + notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
496 + fmt.Fprintf(out, "storing value at %s\n", obj.ID)
497 + },
498 + }
499
500 marshal := func(v interface{}) (io.Reader, error) {
501 obj, ok := v.(*notif.QueryEvent)
@@ -558,33 +504,8 @@ PutValue will store the given key value pair in the dht.
504 }
505
506 buf := new(bytes.Buffer)
561 - if verbose {
562 - fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
563 - }
564 - switch obj.Type {
565 - case notif.FinalPeer:
566 - if verbose {
567 - fmt.Fprintf(buf, "* closest peer %s\n", obj.ID)
568 - }
569 - case notif.PeerResponse:
570 - if verbose {
571 - fmt.Fprintf(buf, "* %s says use ", obj.ID)
572 - for _, p := range obj.Responses {
573 - fmt.Fprintf(buf, "%s ", p.ID)
574 - }
575 - fmt.Fprintln(buf)
576 - }
577 - case notif.SendingQuery:
578 - if verbose {
579 - fmt.Fprintf(buf, "* querying %s\n", obj.ID)
580 - }
581 - case notif.QueryError:
582 - fmt.Fprintf(buf, "error: %s\n", obj.Extra)
583 - case notif.Value:
584 - fmt.Fprintf(buf, "storing value at %s\n", obj.ID)
585 - default:
586 - fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
587 - }
507 + printEvent(obj, buf, verbose, pfm)
508 +
509 return buf, nil
510 }
511
@@ -598,6 +519,53 @@ PutValue will store the given key value pair in the dht.
519 Type: notif.QueryEvent{},
520 }
521
522 +type printFunc func(obj *notif.QueryEvent, out io.Writer, verbose bool)
523 +type pfuncMap map[notif.QueryEventType]printFunc
524 +
525 +func printEvent(obj *notif.QueryEvent, out io.Writer, verbose bool, override pfuncMap) {
526 + if verbose {
527 + fmt.Fprintf(out, "%s: ", time.Now().Format("15:04:05.000"))
528 + }
529 +
530 + if override != nil {
531 + if pf, ok := override[obj.Type]; ok {
532 + pf(obj, out, verbose)
533 + return
534 + }
535 + }
536 +
537 + switch obj.Type {
538 + case notif.SendingQuery:
539 + if verbose {
540 + fmt.Fprintf(out, "* querying %s\n", obj.ID)
541 + }
542 + case notif.Value:
543 + if verbose {
544 + fmt.Fprintf(out, "got value: '%s'\n", obj.Extra)
545 + } else {
546 + fmt.Fprint(out, obj.Extra)
547 + }
548 + case notif.PeerResponse:
549 + fmt.Fprintf(out, "* %s says use ", obj.ID)
550 + for _, p := range obj.Responses {
551 + fmt.Fprintf(out, "%s ", p.ID)
552 + }
553 + fmt.Fprintln(out)
554 + case notif.QueryError:
555 + fmt.Fprintf(out, "error: %s\n", obj.Extra)
556 + case notif.DialingPeer:
557 + if verbose {
558 + fmt.Fprintf(out, "dialing peer: %s\n", obj.ID)
559 + }
560 + case notif.AddingPeer:
561 + if verbose {
562 + fmt.Fprintf(out, "adding peer to query: %s\n", obj.ID)
563 + }
564 + default:
565 + fmt.Fprintf(out, "unrecognized event type: %d\n", obj.Type)
566 + }
567 +}
568 +
569 func escapeDhtKey(s string) (key.Key, error) {
570 parts := path.SplitList(s)
571 switch len(parts) {