@@ -182,7 +182,8 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
182
182
allPackageIndexUrls = append (allPackageIndexUrls , URL )
183
183
}
184
184
}
185
- if err := firstUpdate (context .Background (), req .GetInstance (), downloadCallback , allPackageIndexUrls ); err != nil {
185
+
186
+ if err := firstUpdate (context .Background (), s , req .GetInstance (), downloadCallback , allPackageIndexUrls ); err != nil {
186
187
e := & cmderrors.InitFailedError {
187
188
Code : codes .InvalidArgument ,
188
189
Cause : err ,
@@ -440,12 +441,27 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
440
441
return nil
441
442
}
442
443
444
+ // UpdateIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in UpdateIndex that sends
445
+ // all responses to the callback function.
446
+ func UpdateIndexStreamResponseToCallbackFunction (ctx context.Context , downloadCB rpc.DownloadProgressCB ) rpc.ArduinoCoreService_UpdateIndexServer {
447
+ return streamResponseToCallback (ctx , func (r * rpc.UpdateIndexResponse ) error {
448
+ if r .GetDownloadProgress () != nil {
449
+ downloadCB (r .GetDownloadProgress ())
450
+ }
451
+ return nil
452
+ })
453
+ }
454
+
443
455
// UpdateIndex FIXMEDOC
444
- func UpdateIndex ( ctx context. Context , req * rpc.UpdateIndexRequest , downloadCB rpc.DownloadProgressCB ) error {
456
+ func ( s * arduinoCoreServerImpl ) UpdateIndex ( req * rpc.UpdateIndexRequest , stream rpc.ArduinoCoreService_UpdateIndexServer ) error {
445
457
if ! instances .IsValid (req .GetInstance ()) {
446
458
return & cmderrors.InvalidInstanceError {}
447
459
}
448
460
461
+ syncSend := NewSynchronizedSend (stream .Send )
462
+ var downloadCB rpc.DownloadProgressCB = func (p * rpc.DownloadProgress ) {
463
+ syncSend .Send (& rpc.UpdateIndexResponse {DownloadProgress : p })
464
+ }
449
465
indexpath := configuration .DataDir (configuration .Settings )
450
466
451
467
urls := []string {globals .DefaultIndexURL }
@@ -498,7 +514,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rp
498
514
499
515
// firstUpdate downloads libraries and packages indexes if they don't exist.
500
516
// This ideally is only executed the first time the CLI is run.
501
- func firstUpdate (ctx context.Context , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
517
+ func firstUpdate (ctx context.Context , srv rpc. ArduinoCoreServiceServer , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
502
518
// Gets the data directory to verify if library_index.json and package_index.json exist
503
519
dataDir := configuration .DataDir (configuration .Settings )
504
520
libraryIndex := dataDir .Join ("library_index.json" )
@@ -529,7 +545,8 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
529
545
// library update we download that file and all the other package indexes from
530
546
// additional_urls
531
547
req := & rpc.UpdateIndexRequest {Instance : instance }
532
- if err := UpdateIndex (ctx , req , downloadCb ); err != nil {
548
+ stream := UpdateIndexStreamResponseToCallbackFunction (ctx , downloadCb )
549
+ if err := srv .UpdateIndex (req , stream ); err != nil {
533
550
return err
534
551
}
535
552
break
0 commit comments