Skip to content

Commit b23b372

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 12: UpdateLibrariesIndex)
1 parent cd7078c commit b23b372

File tree

5 files changed

+58
-48
lines changed

5 files changed

+58
-48
lines changed

commands/instances.go

+47-15
Original file line numberDiff line numberDiff line change
@@ -422,47 +422,78 @@ func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq
422422
return &rpc.DestroyResponse{}, nil
423423
}
424424

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+
425442
// 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+
}
428449

429450
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
430451
if err != nil {
431-
return nil, err
452+
return err
432453
}
433454
indexDir := pme.IndexDir
434455
release()
435-
436456
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+
},
442467
},
443-
}
468+
})
444469
}
445470

446471
// Create the index directory if it doesn't exist
447472
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}
449475
}
450476

451477
// Check if the index file is already up-to-date
452478
indexFileName, _ := index.IndexFileName()
453479
if info, err := indexDir.Join(indexFileName).Stat(); err == nil {
454480
ageSecs := int64(time.Since(info.ModTime()).Seconds())
455481
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
457484
}
458485
}
459486

460487
// Perform index update
488+
// TODO: pass context
489+
// ctx := stream.Context()
461490
if err := globals.LibrariesIndexResource.Download(indexDir, downloadCB); err != nil {
462-
return nil, err
491+
resultCB(rpc.IndexUpdateReport_STATUS_FAILED)
492+
return err
463493
}
464494

465-
return result(rpc.IndexUpdateReport_STATUS_UPDATED), nil
495+
resultCB(rpc.IndexUpdateReport_STATUS_UPDATED)
496+
return nil
466497
}
467498

468499
// 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
593624
// The library_index.json file doesn't exists, that means the CLI is run for the first time
594625
// so we proceed with the first update that downloads the file
595626
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 {
597629
return err
598630
}
599631
}

commands/service.go

-18
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ type arduinoCoreServerImpl struct {
3737
versionString string
3838
}
3939

40-
// UpdateLibrariesIndex FIXMEDOC
41-
func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexRequest, stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer) error {
42-
syncSend := NewSynchronizedSend(stream.Send)
43-
res, err := UpdateLibrariesIndex(stream.Context(), req,
44-
func(p *rpc.DownloadProgress) {
45-
syncSend.Send(&rpc.UpdateLibrariesIndexResponse{
46-
Message: &rpc.UpdateLibrariesIndexResponse_DownloadProgress{DownloadProgress: p},
47-
})
48-
},
49-
)
50-
if res != nil {
51-
syncSend.Send(&rpc.UpdateLibrariesIndexResponse{
52-
Message: &rpc.UpdateLibrariesIndexResponse_Result_{Result: res},
53-
})
54-
}
55-
return err
56-
}
57-
5840
// Version FIXMEDOC
5941
func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionRequest) (*rpc.VersionResponse, error) {
6042
return &rpc.VersionResponse{Version: s.versionString}, nil

internal/cli/lib/search.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,12 @@ func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string, namesOnly
107107

108108
logrus.Info("Executing `arduino-cli lib search`")
109109

110-
res, err := commands.UpdateLibrariesIndex(
111-
context.Background(),
112-
&rpc.UpdateLibrariesIndexRequest{Instance: inst, UpdateIfOlderThanSecs: int64(indexUpdateInterval.Seconds())},
113-
feedback.ProgressBar(),
114-
)
115-
if err != nil {
110+
stream, res := commands.UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
111+
req := &rpc.UpdateLibrariesIndexRequest{Instance: inst, UpdateIfOlderThanSecs: int64(indexUpdateInterval.Seconds())}
112+
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
116113
feedback.Fatal(tr("Error updating library index: %v", err), feedback.ErrGeneric)
117114
}
118-
if res.GetLibrariesIndex().GetStatus() == rpc.IndexUpdateReport_STATUS_UPDATED {
115+
if res().GetLibrariesIndex().GetStatus() == rpc.IndexUpdateReport_STATUS_UPDATED {
119116
instance.Init(ctx, srv, inst)
120117
}
121118

internal/cli/lib/update_index.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,18 @@ func runUpdateIndexCommand(srv rpc.ArduinoCoreServiceServer) {
4747
inst := instance.CreateAndInit(ctx, srv)
4848

4949
logrus.Info("Executing `arduino-cli lib update-index`")
50-
resp := UpdateIndex(inst)
50+
resp := UpdateIndex(ctx, srv, inst)
5151
feedback.PrintResult(&libUpdateIndexResult{result.NewUpdateLibrariesIndexResponse_ResultResult(resp)})
5252
}
5353

5454
// UpdateIndex updates the index of libraries.
55-
func UpdateIndex(inst *rpc.Instance) *rpc.UpdateLibrariesIndexResponse_Result {
56-
resp, err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
57-
Instance: inst,
58-
}, feedback.ProgressBar())
59-
if err != nil {
55+
func UpdateIndex(ctx context.Context, srv rpc.ArduinoCoreServiceServer, inst *rpc.Instance) *rpc.UpdateLibrariesIndexResponse_Result {
56+
req := &rpc.UpdateLibrariesIndexRequest{Instance: inst}
57+
stream, resp := commands.UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
58+
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
6059
feedback.Fatal(tr("Error updating library index: %v", err), feedback.ErrGeneric)
6160
}
62-
return resp
61+
return resp()
6362
}
6463

6564
type libUpdateIndexResult struct {

internal/cli/update/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func runUpdateCommand(srv rpc.ArduinoCoreServiceServer, showOutdated bool) {
5353
ctx := context.Background()
5454
inst := instance.CreateAndInit(ctx, srv)
5555

56-
lib.UpdateIndex(inst)
56+
lib.UpdateIndex(ctx, srv, inst)
5757
core.UpdateIndex(ctx, srv, inst)
5858
instance.Init(ctx, srv, inst)
5959
if showOutdated {

0 commit comments

Comments
 (0)