@cryptotaxi247 / kubo / commits / 76dc6f566

commands(dht): make it possible to return errors from printEvent

License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Apr 30, 2019 at 00:56 UTC 76dc6f5662fec7bff66f6e43dae5670a145d9499
1 file changed +26 -28
core/commands/dht.go
+26 -28
@@ -104,15 +104,15 @@ var queryDhtCmd = &cmds.Command{
104 Encoders: cmds.EncoderMap{
105 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
106 pfm := pfuncMap{
107 - notif.PeerResponse: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
107 + notif.PeerResponse: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
108 for _, p := range obj.Responses {
109 fmt.Fprintf(out, "%s\n", p.ID.Pretty())
110 }
111 + return nil
112 },
113 }
114 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
114 - printEvent(out, w, verbose, pfm)
115 - return nil
115 + return printEvent(out, w, verbose, pfm)
116 }),
117 },
118 Type: notif.QueryEvent{},
@@ -182,12 +182,13 @@ var findProvidersDhtCmd = &cmds.Command{
182 Encoders: cmds.EncoderMap{
183 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
184 pfm := pfuncMap{
185 - notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
185 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
186 if verbose {
187 fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
188 }
189 + return nil
190 },
190 - notif.Provider: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
191 + notif.Provider: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
192 prov := obj.Responses[0]
193 if verbose {
194 fmt.Fprintf(out, "provider: ")
@@ -198,13 +199,12 @@ var findProvidersDhtCmd = &cmds.Command{
199 fmt.Fprintf(out, "\t%s\n", a)
200 }
201 }
202 + return nil
203 },
204 }
205
206 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
205 - printEvent(out, w, verbose, pfm)
206 -
207 - return nil
207 + return printEvent(out, w, verbose, pfm)
208 }),
209 },
210 Type: notif.QueryEvent{},
@@ -291,17 +291,16 @@ var provideRefDhtCmd = &cmds.Command{
291 Encoders: cmds.EncoderMap{
292 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
293 pfm := pfuncMap{
294 - notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
294 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
295 if verbose {
296 fmt.Fprintf(out, "sending provider record to peer %s\n", obj.ID)
297 }
298 + return nil
299 },
300 }
301
302 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
302 - printEvent(out, w, verbose, pfm)
303 -
304 - return nil
303 + return printEvent(out, w, verbose, pfm)
304 }),
305 },
306 Type: notif.QueryEvent{},
@@ -403,17 +402,17 @@ var findPeerDhtCmd = &cmds.Command{
402 Encoders: cmds.EncoderMap{
403 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
404 pfm := pfuncMap{
406 - notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
405 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
406 pi := obj.Responses[0]
407 for _, a := range pi.Addrs {
408 fmt.Fprintf(out, "%s\n", a)
409 }
410 + return nil
411 },
412 }
413
414 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
415 - printEvent(out, w, verbose, pfm)
416 - return nil
415 + return printEvent(out, w, verbose, pfm)
416 }),
417 },
418 Type: notif.QueryEvent{},
@@ -486,19 +485,18 @@ Different key types can specify other 'best' rules.
485 Encoders: cmds.EncoderMap{
486 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
487 pfm := pfuncMap{
489 - notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
488 + notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
489 if verbose {
490 fmt.Fprintf(out, "got value: '%s'\n", obj.Extra)
491 } else {
492 fmt.Fprintln(out, obj.Extra)
493 }
494 + return nil
495 },
496 }
497
498 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
499 - printEvent(out, w, verbose, pfm)
500 -
501 - return nil
499 + return printEvent(out, w, verbose, pfm)
500 }),
501 },
502 Type: notif.QueryEvent{},
@@ -576,38 +574,37 @@ NOTE: A value may not exceed 2048 bytes.
574 Encoders: cmds.EncoderMap{
575 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *notif.QueryEvent) error {
576 pfm := pfuncMap{
579 - notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
577 + notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
578 if verbose {
579 fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
580 }
581 + return nil
582 },
584 - notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
583 + notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) error {
584 fmt.Fprintf(out, "%s\n", obj.ID.Pretty())
585 + return nil
586 },
587 }
588
589 verbose, _ := req.Options[dhtVerboseOptionName].(bool)
590
591 - printEvent(out, w, verbose, pfm)
592 -
593 - return nil
591 + return printEvent(out, w, verbose, pfm)
592 }),
593 },
594 Type: notif.QueryEvent{},
595 }
596
599 -type printFunc func(obj *notif.QueryEvent, out io.Writer, verbose bool)
597 +type printFunc func(obj *notif.QueryEvent, out io.Writer, verbose bool) error
598 type pfuncMap map[notif.QueryEventType]printFunc
599
602 -func printEvent(obj *notif.QueryEvent, out io.Writer, verbose bool, override pfuncMap) {
600 +func printEvent(obj *notif.QueryEvent, out io.Writer, verbose bool, override pfuncMap) error {
601 if verbose {
602 fmt.Fprintf(out, "%s: ", time.Now().Format("15:04:05.000"))
603 }
604
605 if override != nil {
606 if pf, ok := override[obj.Type]; ok {
609 - pf(obj, out, verbose)
610 - return
607 + return pf(obj, out, verbose)
608 }
609 }
610
@@ -648,6 +645,7 @@ func printEvent(obj *notif.QueryEvent, out io.Writer, verbose bool, override pfu
645 fmt.Fprintf(out, "unrecognized event type: %d\n", obj.Type)
646 }
647 }
648 + return nil
649 }
650
651 func escapeDhtKey(s string) (string, error) {