Skip to content

Commit 5712969

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 7: UpdateIndex)
Added helpers to get download progress.
1 parent 8bad94c commit 5712969

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

Diff for: commands/instances.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
182182
allPackageIndexUrls = append(allPackageIndexUrls, URL)
183183
}
184184
}
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 {
186187
e := &cmderrors.InitFailedError{
187188
Code: codes.InvalidArgument,
188189
Cause: err,
@@ -440,12 +441,27 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
440441
return nil
441442
}
442443

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+
443455
// 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 {
445457
if !instances.IsValid(req.GetInstance()) {
446458
return &cmderrors.InvalidInstanceError{}
447459
}
448460

461+
syncSend := NewSynchronizedSend(stream.Send)
462+
var downloadCB rpc.DownloadProgressCB = func(p *rpc.DownloadProgress) {
463+
syncSend.Send(&rpc.UpdateIndexResponse{DownloadProgress: p})
464+
}
449465
indexpath := configuration.DataDir(configuration.Settings)
450466

451467
urls := []string{globals.DefaultIndexURL}
@@ -498,7 +514,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB rp
498514

499515
// firstUpdate downloads libraries and packages indexes if they don't exist.
500516
// 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 {
502518
// Gets the data directory to verify if library_index.json and package_index.json exist
503519
dataDir := configuration.DataDir(configuration.Settings)
504520
libraryIndex := dataDir.Join("library_index.json")
@@ -529,7 +545,8 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
529545
// library update we download that file and all the other package indexes from
530546
// additional_urls
531547
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 {
533550
return err
534551
}
535552
break

Diff for: commands/service.go

-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ type arduinoCoreServerImpl struct {
4040
versionString string
4141
}
4242

43-
// UpdateIndex FIXMEDOC
44-
func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream rpc.ArduinoCoreService_UpdateIndexServer) error {
45-
syncSend := NewSynchronizedSend(stream.Send)
46-
return UpdateIndex(stream.Context(), req,
47-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.UpdateIndexResponse{DownloadProgress: p}) },
48-
)
49-
}
50-
5143
// UpdateLibrariesIndex FIXMEDOC
5244
func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesIndexRequest, stream rpc.ArduinoCoreService_UpdateLibrariesIndexServer) error {
5345
syncSend := NewSynchronizedSend(stream.Send)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func runSearchCommand(srv rpc.ArduinoCoreServiceServer, args []string, allVersio
6161
inst := instance.CreateAndInit(ctx, srv)
6262

6363
if indexesNeedUpdating(indexUpdateInterval) {
64-
err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, feedback.ProgressBar())
64+
stream := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
65+
err := srv.UpdateIndex(&rpc.UpdateIndexRequest{Instance: inst}, stream)
6566
if err != nil {
6667
feedback.FatalError(err, feedback.ErrGeneric)
6768
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func runUpdateIndexCommand(srv rpc.ArduinoCoreServiceServer) {
4646
ctx := context.Background()
4747
inst := instance.CreateAndInit(ctx, srv)
4848

49-
UpdateIndex(inst)
49+
UpdateIndex(ctx, srv, inst)
5050
}
5151

5252
// UpdateIndex updates the index of platforms.
53-
func UpdateIndex(inst *rpc.Instance) {
54-
err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{Instance: inst}, feedback.ProgressBar())
53+
func UpdateIndex(ctx context.Context, srv rpc.ArduinoCoreServiceServer, inst *rpc.Instance) {
54+
stream := commands.UpdateIndexStreamResponseToCallbackFunction(ctx, feedback.ProgressBar())
55+
err := srv.UpdateIndex(&rpc.UpdateIndexRequest{Instance: inst}, stream)
5556
if err != nil {
5657
feedback.FatalError(err, feedback.ErrGeneric)
5758
}

Diff for: internal/cli/update/update.go

+1-1
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)