@@ -184,7 +184,8 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
184
184
allPackageIndexUrls = append (allPackageIndexUrls , URL )
185
185
}
186
186
}
187
- if err := firstUpdate (context .Background (), req .GetInstance (), downloadCallback , allPackageIndexUrls ); err != nil {
187
+
188
+ if err := firstUpdate (context .Background (), s , req .GetInstance (), downloadCallback , allPackageIndexUrls ); err != nil {
188
189
e := & cmderrors.InitFailedError {
189
190
Code : codes .InvalidArgument ,
190
191
Cause : err ,
@@ -464,10 +465,27 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
464
465
return result (rpc .IndexUpdateReport_STATUS_UPDATED ), nil
465
466
}
466
467
468
+ // UpdateIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in UpdateIndex that sends
469
+ // all responses to the callback function.
470
+ func UpdateIndexStreamResponseToCallbackFunction (ctx context.Context , downloadCB rpc.DownloadProgressCB ) (rpc.ArduinoCoreService_UpdateIndexServer , func () * rpc.UpdateIndexResponse_Result ) {
471
+ var result * rpc.UpdateIndexResponse_Result
472
+ return streamResponseToCallback (ctx , func (r * rpc.UpdateIndexResponse ) error {
473
+ if r .GetDownloadProgress () != nil {
474
+ downloadCB (r .GetDownloadProgress ())
475
+ }
476
+ if r .GetResult () != nil {
477
+ result = r .GetResult ()
478
+ }
479
+ return nil
480
+ }), func () * rpc.UpdateIndexResponse_Result {
481
+ return result
482
+ }
483
+ }
484
+
467
485
// UpdateIndex FIXMEDOC
468
- func UpdateIndex ( ctx context. Context , req * rpc.UpdateIndexRequest , downloadCB rpc.DownloadProgressCB ) ( * rpc. UpdateIndexResponse_Result , error ) {
486
+ func ( s * arduinoCoreServerImpl ) UpdateIndex ( req * rpc.UpdateIndexRequest , stream rpc.ArduinoCoreService_UpdateIndexServer ) error {
469
487
if ! instances .IsValid (req .GetInstance ()) {
470
- return nil , & cmderrors.InvalidInstanceError {}
488
+ return & cmderrors.InvalidInstanceError {}
471
489
}
472
490
473
491
report := func (indexURL * url.URL , status rpc.IndexUpdateReport_Status ) * rpc.IndexUpdateReport {
@@ -477,6 +495,12 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rp
477
495
}
478
496
}
479
497
498
+ syncSend := NewSynchronizedSend (stream .Send )
499
+ var downloadCB rpc.DownloadProgressCB = func (p * rpc.DownloadProgress ) {
500
+ syncSend .Send (& rpc.UpdateIndexResponse {
501
+ Message : & rpc.UpdateIndexResponse_DownloadProgress {DownloadProgress : p },
502
+ })
503
+ }
480
504
indexpath := configuration .DataDir (configuration .Settings )
481
505
482
506
urls := []string {globals .DefaultIndexURL }
@@ -549,16 +573,18 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rp
549
573
result .UpdatedIndexes = append (result .GetUpdatedIndexes (), report (URL , rpc .IndexUpdateReport_STATUS_UPDATED ))
550
574
}
551
575
}
552
-
576
+ syncSend .Send (& rpc.UpdateIndexResponse {
577
+ Message : & rpc.UpdateIndexResponse_Result_ {Result : result },
578
+ })
553
579
if failed {
554
- return result , & cmderrors.FailedDownloadError {Message : tr ("Some indexes could not be updated." )}
580
+ return & cmderrors.FailedDownloadError {Message : tr ("Some indexes could not be updated." )}
555
581
}
556
- return result , nil
582
+ return nil
557
583
}
558
584
559
585
// firstUpdate downloads libraries and packages indexes if they don't exist.
560
586
// This ideally is only executed the first time the CLI is run.
561
- func firstUpdate (ctx context.Context , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
587
+ func firstUpdate (ctx context.Context , srv rpc. ArduinoCoreServiceServer , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
562
588
// Gets the data directory to verify if library_index.json and package_index.json exist
563
589
dataDir := configuration .DataDir (configuration .Settings )
564
590
libraryIndex := dataDir .Join ("library_index.json" )
@@ -589,7 +615,8 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
589
615
// library update we download that file and all the other package indexes from
590
616
// additional_urls
591
617
req := & rpc.UpdateIndexRequest {Instance : instance }
592
- if _ , err := UpdateIndex (ctx , req , downloadCb ); err != nil {
618
+ stream , _ := UpdateIndexStreamResponseToCallbackFunction (ctx , downloadCb )
619
+ if err := srv .UpdateIndex (req , stream ); err != nil {
593
620
return err
594
621
}
595
622
break
0 commit comments