@@ -422,47 +422,78 @@ func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq
422
422
return & rpc.DestroyResponse {}, nil
423
423
}
424
424
425
+ // UpdateLibrariesIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in UpdateLibrariesIndex that sends
426
+ // all responses to the callback function.
427
+ func UpdateLibrariesIndexStreamResponseToCallbackFunction (ctx context.Context , downloadCB rpc.DownloadProgressCB ) (rpc.ArduinoCoreService_UpdateLibrariesIndexServer , func () * rpc.UpdateLibrariesIndexResponse_Result ) {
428
+ var result * rpc.UpdateLibrariesIndexResponse_Result
429
+ return streamResponseToCallback (ctx , func (r * rpc.UpdateLibrariesIndexResponse ) error {
430
+ if r .GetDownloadProgress () != nil {
431
+ downloadCB (r .GetDownloadProgress ())
432
+ }
433
+ if r .GetResult () != nil {
434
+ result = r .GetResult ()
435
+ }
436
+ return nil
437
+ }), func () * rpc.UpdateLibrariesIndexResponse_Result {
438
+ return result
439
+ }
440
+ }
441
+
425
442
// UpdateLibrariesIndex updates the library_index.json
426
- func UpdateLibrariesIndex (ctx context.Context , req * rpc.UpdateLibrariesIndexRequest , downloadCB rpc.DownloadProgressCB ) (* rpc.UpdateLibrariesIndexResponse_Result , error ) {
427
- logrus .Info ("Updating libraries index" )
443
+ func (s * arduinoCoreServerImpl ) UpdateLibrariesIndex (req * rpc.UpdateLibrariesIndexRequest , stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer ) error {
444
+ syncSend := NewSynchronizedSend (stream .Send )
445
+ downloadCB := func (p * rpc.DownloadProgress ) {
446
+ syncSend .Send (& rpc.UpdateLibrariesIndexResponse {
447
+ Message : & rpc.UpdateLibrariesIndexResponse_DownloadProgress {DownloadProgress : p }})
448
+ }
428
449
429
450
pme , release , err := instances .GetPackageManagerExplorer (req .GetInstance ())
430
451
if err != nil {
431
- return nil , err
452
+ return err
432
453
}
433
454
indexDir := pme .IndexDir
434
455
release ()
435
-
436
456
index := globals .LibrariesIndexResource
437
- result := func (status rpc.IndexUpdateReport_Status ) * rpc.UpdateLibrariesIndexResponse_Result {
438
- return & rpc.UpdateLibrariesIndexResponse_Result {
439
- LibrariesIndex : & rpc.IndexUpdateReport {
440
- IndexUrl : globals .LibrariesIndexResource .URL .String (),
441
- Status : status ,
457
+
458
+ resultCB := func (status rpc.IndexUpdateReport_Status ) {
459
+ syncSend .Send (& rpc.UpdateLibrariesIndexResponse {
460
+ Message : & rpc.UpdateLibrariesIndexResponse_Result_ {
461
+ Result : & rpc.UpdateLibrariesIndexResponse_Result {
462
+ LibrariesIndex : & rpc.IndexUpdateReport {
463
+ IndexUrl : index .URL .String (),
464
+ Status : status ,
465
+ },
466
+ },
442
467
},
443
- }
468
+ })
444
469
}
445
470
446
471
// Create the index directory if it doesn't exist
447
472
if err := indexDir .MkdirAll (); err != nil {
448
- return result (rpc .IndexUpdateReport_STATUS_FAILED ), & cmderrors.PermissionDeniedError {Message : tr ("Could not create index directory" ), Cause : err }
473
+ resultCB (rpc .IndexUpdateReport_STATUS_FAILED )
474
+ return & cmderrors.PermissionDeniedError {Message : tr ("Could not create index directory" ), Cause : err }
449
475
}
450
476
451
477
// Check if the index file is already up-to-date
452
478
indexFileName , _ := index .IndexFileName ()
453
479
if info , err := indexDir .Join (indexFileName ).Stat (); err == nil {
454
480
ageSecs := int64 (time .Since (info .ModTime ()).Seconds ())
455
481
if ageSecs < req .GetUpdateIfOlderThanSecs () {
456
- return result (rpc .IndexUpdateReport_STATUS_ALREADY_UP_TO_DATE ), nil
482
+ resultCB (rpc .IndexUpdateReport_STATUS_ALREADY_UP_TO_DATE )
483
+ return nil
457
484
}
458
485
}
459
486
460
487
// Perform index update
488
+ // TODO: pass context
489
+ // ctx := stream.Context()
461
490
if err := globals .LibrariesIndexResource .Download (indexDir , downloadCB ); err != nil {
462
- return nil , err
491
+ resultCB (rpc .IndexUpdateReport_STATUS_FAILED )
492
+ return err
463
493
}
464
494
465
- return result (rpc .IndexUpdateReport_STATUS_UPDATED ), nil
495
+ resultCB (rpc .IndexUpdateReport_STATUS_UPDATED )
496
+ return nil
466
497
}
467
498
468
499
// UpdateIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in UpdateIndex that sends
@@ -593,7 +624,8 @@ func firstUpdate(ctx context.Context, srv rpc.ArduinoCoreServiceServer, instance
593
624
// The library_index.json file doesn't exists, that means the CLI is run for the first time
594
625
// so we proceed with the first update that downloads the file
595
626
req := & rpc.UpdateLibrariesIndexRequest {Instance : instance }
596
- if _ , err := UpdateLibrariesIndex (ctx , req , downloadCb ); err != nil {
627
+ stream , _ := UpdateLibrariesIndexStreamResponseToCallbackFunction (ctx , downloadCb )
628
+ if err := srv .UpdateLibrariesIndex (req , stream ); err != nil {
597
629
return err
598
630
}
599
631
}
0 commit comments