Skip to content

Commit 917dcc5

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 7: UpdateIndex)
Added helpers to get download progress.
1 parent a81069d commit 917dcc5

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

commands/instances.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

commands/service.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,6 @@ type arduinoCoreServerImpl struct {
4242
versionString string
4343
}
4444

45-
// UpdateIndex FIXMEDOC
46-
func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream rpc.ArduinoCoreService_UpdateIndexServer) error {
47-
syncSend := NewSynchronizedSend(stream.Send)
48-
res, err := UpdateIndex(stream.Context(), req,
49-
func(p *rpc.DownloadProgress) {
50-
syncSend.Send(&rpc.UpdateIndexResponse{
51-
Message: &rpc.UpdateIndexResponse_DownloadProgress{DownloadProgress: p},
52-
})
53-
},
54-
)
55-
if res != nil {
56-
syncSend.Send(&rpc.UpdateIndexResponse{
57-
Message: &rpc.UpdateIndexResponse_Result_{Result: res},
58-
})
59-
}
60-
return err
61-
}
62-
6345
// UpdateLibrariesIndex FIXMEDOC
6446
func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexRequest, stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer) error {
6547
syncSend := NewSynchronizedSend(stream.Send)

internal/cli/core/search.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string, allVersio
5656
ctx := context.Background()
5757
inst := instance.CreateAndInit(ctx, srv)
5858

59-
res, err := commands.UpdateIndex(
60-
context.Background(),
59+
stream, res := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
60+
err := srv.UpdateIndex(
6161
&rpc.UpdateIndexRequest{Instance: inst, UpdateIfOlderThanSecs: int64(indexUpdateInterval.Seconds())},
62-
feedback.ProgressBar())
62+
stream)
6363
if err != nil {
6464
feedback.FatalError(err, feedback.ErrGeneric)
6565
}
66-
for _, idxRes := range res.GetUpdatedIndexes() {
66+
for _, idxRes := range res().GetUpdatedIndexes() {
6767
if idxRes.GetStatus() == rpc.IndexUpdateReport_STATUS_UPDATED {
6868
// At least one index has been updated, reinitialize the instance
6969
instance.Init(ctx, srv, inst)

internal/cli/core/update_index.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ func runUpdateIndexCommand(srv rpc.ArduinoCoreServiceServer) {
4646
logrus.Info("Executing `arduino-cli core update-index`")
4747
ctx := context.Background()
4848
inst := instance.CreateAndInit(ctx, srv)
49-
resp := UpdateIndex(inst)
49+
resp := UpdateIndex(ctx, srv, inst)
5050

5151
feedback.PrintResult(&updateIndexResult{result.NewUpdateIndexResponse_ResultResult(resp)})
5252
}
5353

5454
// UpdateIndex updates the index of platforms.
55-
func UpdateIndex(inst *rpc.Instance) *rpc.UpdateIndexResponse_Result {
56-
res, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, feedback.ProgressBar())
55+
func UpdateIndex(ctx context.Context, srv rpc.ArduinoCoreServiceServer, inst *rpc.Instance) *rpc.UpdateIndexResponse_Result {
56+
stream, res := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
57+
err := srv.UpdateIndex(&rpc.UpdateIndexRequest{Instance: inst}, stream)
5758
if err != nil {
5859
feedback.FatalError(err, feedback.ErrGeneric)
5960
}
60-
return res
61+
return res()
6162
}
6263

6364
type updateIndexResult struct {

internal/cli/update/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func runUpdateCommand(srv rpc.ArduinoCoreServiceServer, showOutdated bool) {
5454
inst := instance.CreateAndInit(ctx, srv)
5555

5656
lib.UpdateIndex(inst)
57-
core.UpdateIndex(inst)
57+
core.UpdateIndex(ctx, srv, inst)
5858
instance.Init(ctx, srv, inst)
5959
if showOutdated {
6060
outdated.Outdated(inst)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy