Skip to content

Commit e5b0c5b

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 12: UpdateLibrariesIndex)
1 parent 461b2b1 commit e5b0c5b

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

Diff for: commands/instances.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,22 @@ func (s *arduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq
420420
return &rpc.DestroyResponse{}, nil
421421
}
422422

423+
// UpdateLibrariesIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in UpdateLibrariesIndex that sends
424+
// all responses to the callback function.
425+
func UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB) rpc.ArduinoCoreService_UpdateLibrariesIndexServer {
426+
return streamResponseToCallback(ctx, func(r *rpc.UpdateLibrariesIndexResponse) error {
427+
if r.GetDownloadProgress() != nil {
428+
downloadCB(r.GetDownloadProgress())
429+
}
430+
return nil
431+
})
432+
}
433+
423434
// UpdateLibrariesIndex updates the library_index.json
424-
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequest, downloadCB rpc.DownloadProgressCB) error {
425-
logrus.Info("Updating libraries index")
435+
func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexRequest, stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer) error {
436+
syncSend := NewSynchronizedSend(stream.Send)
437+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.UpdateLibrariesIndexResponse{DownloadProgress: p}) }
438+
426439
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
427440
if err != nil {
428441
return err
@@ -434,6 +447,8 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
434447
return &cmderrors.PermissionDeniedError{Message: tr("Could not create index directory"), Cause: err}
435448
}
436449

450+
// TODO: pass context
451+
// ctx := stream.Context()
437452
if err := globals.LibrariesIndexResource.Download(indexDir, downloadCB); err != nil {
438453
return err
439454
}
@@ -523,7 +538,8 @@ func firstUpdate(ctx context.Context, srv rpc.ArduinoCoreServiceServer, instance
523538
// The library_index.json file doesn't exists, that means the CLI is run for the first time
524539
// so we proceed with the first update that downloads the file
525540
req := &rpc.UpdateLibrariesIndexRequest{Instance: instance}
526-
if err := UpdateLibrariesIndex(ctx, req, downloadCb); err != nil {
541+
stream := UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, downloadCb)
542+
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
527543
return err
528544
}
529545
}

Diff for: commands/service.go

-8
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ type arduinoCoreServerImpl struct {
3535
versionString string
3636
}
3737

38-
// UpdateLibrariesIndex FIXMEDOC
39-
func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexRequest, stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer) error {
40-
syncSend := NewSynchronizedSend(stream.Send)
41-
return UpdateLibrariesIndex(stream.Context(), req,
42-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.UpdateLibrariesIndexResponse{DownloadProgress: p}) },
43-
)
44-
}
45-
4638
// Version FIXMEDOC
4739
func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionRequest) (*rpc.VersionResponse, error) {
4840
return &rpc.VersionResponse{Version: s.versionString}, nil

Diff for: internal/cli/lib/search.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string, namesOnly
110110
logrus.Info("Executing `arduino-cli lib search`")
111111

112112
if indexNeedsUpdating(indexUpdateInterval) {
113-
if err := commands.UpdateLibrariesIndex(
114-
context.Background(),
115-
&rpc.UpdateLibrariesIndexRequest{Instance: inst},
116-
feedback.ProgressBar(),
117-
); err != nil {
113+
req := &rpc.UpdateLibrariesIndexRequest{Instance: inst}
114+
stream := commands.UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
115+
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
118116
feedback.Fatal(tr("Error updating library index: %v", err), feedback.ErrGeneric)
119117
}
120118
instance.Init(ctx, srv, inst)

Diff for: internal/cli/lib/update_index.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ func runUpdateIndexCommand(srv rpc.ArduinoCoreServiceServer) {
4646
inst := instance.CreateAndInit(ctx, srv)
4747

4848
logrus.Info("Executing `arduino-cli lib update-index`")
49-
UpdateIndex(inst)
49+
UpdateIndex(ctx, srv, inst)
5050
}
5151

5252
// UpdateIndex updates the index of libraries.
53-
func UpdateIndex(inst *rpc.Instance) {
54-
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexRequest{
55-
Instance: inst,
56-
}, feedback.ProgressBar())
57-
if err != nil {
53+
func UpdateIndex(ctx context.Context, srv rpc.ArduinoCoreServiceServer, inst *rpc.Instance) {
54+
req := &rpc.UpdateLibrariesIndexRequest{Instance: inst}
55+
stream := commands.UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
56+
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
5857
feedback.Fatal(tr("Error updating library index: %v", err), feedback.ErrGeneric)
5958
}
6059
}

Diff for: 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)