@@ -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"
@@ -135,6 +134,7 @@ const (
135
134
)
136
135
137
136
var provideRefRoutingCmd = & cmds.Command {
137
+ Status : cmds .Experimental ,
138
138
Helptext : cmds.HelpText {
139
139
Tagline : "Announce to the network that you are providing given values." ,
140
140
},
@@ -346,6 +346,7 @@ var findPeerRoutingCmd = &cmds.Command{
346
346
}
347
347
348
348
var getValueRoutingCmd = & cmds.Command {
349
+ Status : cmds .Experimental ,
349
350
Helptext : cmds.HelpText {
350
351
Tagline : "Given a key, query the routing system for its best value." ,
351
352
ShortDescription : `
@@ -362,78 +363,30 @@ Different key types can specify other 'best' rules.
362
363
Arguments : []cmds.Argument {
363
364
cmds .StringArg ("key" , true , true , "The key to find a value for." ),
364
365
},
365
- Options : []cmds.Option {
366
- cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
367
- },
368
366
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
369
- nd , err := cmdenv .GetNode (env )
367
+ api , err := cmdenv .GetApi (env , req )
370
368
if err != nil {
371
369
return err
372
370
}
373
371
374
- if ! nd .IsOnline {
375
- return ErrNotOnline
376
- }
377
-
378
- dhtkey , err := escapeDhtKey (req .Arguments [0 ])
372
+ r , err := api .Routing ().Get (req .Context , req .Arguments [0 ])
379
373
if err != nil {
380
374
return err
381
375
}
382
376
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
377
+ return res .Emit (r )
411
378
},
412
379
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 )
380
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out []byte ) error {
381
+ _ , err := w .Write (out )
382
+ return err
431
383
}),
432
384
},
433
- Type : routing. QueryEvent {},
385
+ Type : [] byte {},
434
386
}
435
387
436
388
var putValueRoutingCmd = & cmds.Command {
389
+ Status : cmds .Experimental ,
437
390
Helptext : cmds.HelpText {
438
391
Tagline : "Write a key/value pair to the routing system." ,
439
392
ShortDescription : `
@@ -459,20 +412,8 @@ identified by QmFoo.
459
412
cmds .StringArg ("key" , true , false , "The key to store the value at." ),
460
413
cmds .FileArg ("value-file" , true , false , "A path to a file containing the value to store." ).EnableStdin (),
461
414
},
462
- Options : []cmds.Option {
463
- cmds .BoolOption (dhtVerboseOptionName , "v" , "Print extra information." ),
464
- },
465
415
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 ])
416
+ api , err := cmdenv .GetApi (env , req )
476
417
if err != nil {
477
418
return err
478
419
}
@@ -488,50 +429,20 @@ identified by QmFoo.
488
429
return err
489
430
}
490
431
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
- }
432
+ err = api .Routing ().Put (req .Context , req .Arguments [0 ], data )
433
+ if err != nil {
434
+ return err
510
435
}
511
436
512
- return putErr
437
+ return res . Emit ([] byte ( fmt . Sprintf ( "%s added" , req . Arguments [ 0 ])))
513
438
},
514
439
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 )
440
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out []byte ) error {
441
+ _ , err := w .Write (out )
442
+ return err
532
443
}),
533
444
},
534
- Type : routing. QueryEvent {},
445
+ Type : [] byte {},
535
446
}
536
447
537
448
type printFunc func (obj * routing.QueryEvent , out io.Writer , verbose bool ) error
0 commit comments