@@ -2,7 +2,6 @@ package commands
22
33import (
44 "context"
5- "encoding/base64"
65 "errors"
76 "fmt"
87 "io"
@@ -366,71 +365,25 @@ Different key types can specify other 'best' rules.
366365 cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
367366 },
368367 Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
369- nd , err := cmdenv .GetNode (env )
368+ api , err := cmdenv .GetApi (env , req )
370369 if err != nil {
371370 return err
372371 }
373372
374- if ! nd .IsOnline {
375- return ErrNotOnline
376- }
377-
378- dhtkey , err := escapeDhtKey (req .Arguments [0 ])
373+ r , err := api .Routing ().Get (req .Context , req .Arguments [0 ])
379374 if err != nil {
380375 return err
381376 }
382377
383- ctx , cancel := context .WithCancel (req .Context )
384- ctx , events := routing .RegisterForQueryEvents (ctx )
385-
386- var getErr error
387- go func () {
388- defer cancel ()
389- var val []byte
390- val , getErr = nd .Routing .GetValue (ctx , dhtkey )
391- if getErr != nil {
392- routing .PublishQueryEvent (ctx , & routing.QueryEvent {
393- Type : routing .QueryError ,
394- Extra : getErr .Error (),
395- })
396- } else {
397- routing .PublishQueryEvent (ctx , & routing.QueryEvent {
398- Type : routing .Value ,
399- Extra : base64 .StdEncoding .EncodeToString (val ),
400- })
401- }
402- }()
403-
404- for e := range events {
405- if err := res .Emit (e ); err != nil {
406- return err
407- }
408- }
409-
410- return getErr
378+ return res .Emit (r )
411379 },
412380 Encoders : cmds.EncoderMap {
413- cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * routing.QueryEvent ) error {
414- pfm := pfuncMap {
415- routing .Value : func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error {
416- if verbose {
417- _ , err := fmt .Fprintf (out , "got value: '%s'\n " , obj .Extra )
418- return err
419- }
420- res , err := base64 .StdEncoding .DecodeString (obj .Extra )
421- if err != nil {
422- return err
423- }
424- _ , err = out .Write (res )
425- return err
426- },
427- }
428-
429- verbose , _ := req .Options [dhtVerboseOptionName ].(bool )
430- return printEvent (out , w , verbose , pfm )
381+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out []byte ) error {
382+ _ , err := w .Write (out )
383+ return err
431384 }),
432385 },
433- Type : routing. QueryEvent {},
386+ Type : [] byte {},
434387}
435388
436389var putValueRoutingCmd = & cmds.Command {
@@ -463,16 +416,7 @@ identified by QmFoo.
463416 cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
464417 },
465418 Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
466- nd , err := cmdenv .GetNode (env )
467- if err != nil {
468- return err
469- }
470-
471- if ! nd .IsOnline {
472- return ErrNotOnline
473- }
474-
475- key , err := escapeDhtKey (req .Arguments [0 ])
419+ api , err := cmdenv .GetApi (env , req )
476420 if err != nil {
477421 return err
478422 }
@@ -488,50 +432,20 @@ identified by QmFoo.
488432 return err
489433 }
490434
491- ctx , cancel := context .WithCancel (req .Context )
492- ctx , events := routing .RegisterForQueryEvents (ctx )
493-
494- var putErr error
495- go func () {
496- defer cancel ()
497- putErr = nd .Routing .PutValue (ctx , key , []byte (data ))
498- if putErr != nil {
499- routing .PublishQueryEvent (ctx , & routing.QueryEvent {
500- Type : routing .QueryError ,
501- Extra : putErr .Error (),
502- })
503- }
504- }()
505-
506- for e := range events {
507- if err := res .Emit (e ); err != nil {
508- return err
509- }
435+ err = api .Routing ().Put (req .Context , req .Arguments [0 ], data )
436+ if err != nil {
437+ return err
510438 }
511439
512- return putErr
440+ return res . Emit ([] byte ( fmt . Sprintf ( "%s added" , req . Arguments [ 0 ])))
513441 },
514442 Encoders : cmds.EncoderMap {
515- cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * routing.QueryEvent ) error {
516- pfm := pfuncMap {
517- routing .FinalPeer : func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error {
518- if verbose {
519- fmt .Fprintf (out , "* closest peer %s\n " , obj .ID )
520- }
521- return nil
522- },
523- routing .Value : func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error {
524- fmt .Fprintf (out , "%s\n " , obj .ID .Pretty ())
525- return nil
526- },
527- }
528-
529- verbose , _ := req .Options [dhtVerboseOptionName ].(bool )
530-
531- return printEvent (out , w , verbose , pfm )
443+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out []byte ) error {
444+ _ , err := w .Write (out )
445+ return err
532446 }),
533447 },
534- Type : routing. QueryEvent {},
448+ Type : [] byte {},
535449}
536450
537451type printFunc func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error
0 commit comments