@@ -184,7 +184,8 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
184184 allPackageIndexUrls = append (allPackageIndexUrls , URL )
185185 }
186186 }
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 {
188189 e := & cmderrors.InitFailedError {
189190 Code : codes .InvalidArgument ,
190191 Cause : err ,
@@ -464,10 +465,27 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
464465 return result (rpc .IndexUpdateReport_STATUS_UPDATED ), nil
465466}
466467
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+
467485// 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 {
469487 if ! instances .IsValid (req .GetInstance ()) {
470- return nil , & cmderrors.InvalidInstanceError {}
488+ return & cmderrors.InvalidInstanceError {}
471489 }
472490
473491 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
477495 }
478496 }
479497
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+ }
480504 indexpath := configuration .DataDir (configuration .Settings )
481505
482506 urls := []string {globals .DefaultIndexURL }
@@ -549,16 +573,18 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rp
549573 result .UpdatedIndexes = append (result .GetUpdatedIndexes (), report (URL , rpc .IndexUpdateReport_STATUS_UPDATED ))
550574 }
551575 }
552-
576+ syncSend .Send (& rpc.UpdateIndexResponse {
577+ Message : & rpc.UpdateIndexResponse_Result_ {Result : result },
578+ })
553579 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." )}
555581 }
556- return result , nil
582+ return nil
557583}
558584
559585// firstUpdate downloads libraries and packages indexes if they don't exist.
560586// 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 {
562588 // Gets the data directory to verify if library_index.json and package_index.json exist
563589 dataDir := configuration .DataDir (configuration .Settings )
564590 libraryIndex := dataDir .Join ("library_index.json" )
@@ -589,7 +615,8 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
589615 // library update we download that file and all the other package indexes from
590616 // additional_urls
591617 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 {
593620 return err
594621 }
595622 break
0 commit comments