@@ -2,7 +2,6 @@ package commands
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"errors"
7
6
"fmt"
8
7
"io"
@@ -366,71 +365,25 @@ Different key types can specify other 'best' rules.
366
365
cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
367
366
},
368
367
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 )
370
369
if err != nil {
371
370
return err
372
371
}
373
372
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 ])
379
374
if err != nil {
380
375
return err
381
376
}
382
377
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 )
411
379
},
412
380
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
431
384
}),
432
385
},
433
- Type : routing. QueryEvent {},
386
+ Type : [] byte {},
434
387
}
435
388
436
389
var putValueRoutingCmd = & cmds.Command {
@@ -463,16 +416,7 @@ identified by QmFoo.
463
416
cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
464
417
},
465
418
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 )
476
420
if err != nil {
477
421
return err
478
422
}
@@ -488,50 +432,20 @@ identified by QmFoo.
488
432
return err
489
433
}
490
434
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
510
438
}
511
439
512
- return putErr
440
+ return res . Emit ([] byte ( fmt . Sprintf ( "%s added" , req . Arguments [ 0 ])))
513
441
},
514
442
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
532
446
}),
533
447
},
534
- Type : routing. QueryEvent {},
448
+ Type : [] byte {},
535
449
}
536
450
537
451
type printFunc func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error
0 commit comments