Skip to content

Commit b4e9777

Browse files
committed
Moved downloaders subroutines in 'resources' package
1 parent 38ebf64 commit b4e9777

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

commands/download.go renamed to arduino/resources/download.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package commands
16+
package resources
1717

1818
import (
1919
"time"
2020

2121
"github.com/arduino/arduino-cli/arduino"
2222
"github.com/arduino/arduino-cli/httpclient"
23-
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2423
"go.bug.st/downloader/v2"
2524
)
2625

@@ -36,24 +35,42 @@ func GetDownloaderConfig() (*downloader.Config, error) {
3635
}, nil
3736
}
3837

38+
// DownloadProgress is a report of the download progress, not all fields may be
39+
// filled and multiple reports may be sent during a download.
40+
type DownloadProgress struct {
41+
// URL of the download.
42+
URL string
43+
// The file being downloaded.
44+
File string
45+
// TotalSize is the total size of the file being downloaded.
46+
TotalSize int64
47+
// Downloaded is the size of the downloaded portion of the file.
48+
Downloaded int64
49+
// Completed reports whether the download is complete.
50+
Completed bool
51+
}
52+
53+
// DownloadProgressCB is a callback function to report download progress
54+
type DownloadProgressCB func(progress *DownloadProgress)
55+
3956
// Download performs a download loop using the provided downloader.Downloader.
4057
// Messages are passed back to the DownloadProgressCB using label as text for the File field.
4158
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {
4259
if d == nil {
4360
// This signal means that the file is already downloaded
44-
downloadCB(&rpc.DownloadProgress{
61+
downloadCB(&DownloadProgress{
4562
File: label,
4663
Completed: true,
4764
})
4865
return nil
4966
}
50-
downloadCB(&rpc.DownloadProgress{
67+
downloadCB(&DownloadProgress{
5168
File: label,
52-
Url: d.URL,
69+
URL: d.URL,
5370
TotalSize: d.Size(),
5471
})
5572
d.RunAndPoll(func(downloaded int64) {
56-
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
73+
downloadCB(&DownloadProgress{Downloaded: downloaded})
5774
}, 250*time.Millisecond)
5875
if d.Error() != nil {
5976
return d.Error()
@@ -62,6 +79,6 @@ func Download(d *downloader.Downloader, label string, downloadCB DownloadProgres
6279
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
6380
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
6481
}
65-
downloadCB(&rpc.DownloadProgress{Completed: true})
82+
downloadCB(&DownloadProgress{Completed: true})
6683
return nil
6784
}

commands/bundled_tools.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ import (
1919
"github.com/arduino/arduino-cli/arduino"
2020
"github.com/arduino/arduino-cli/arduino/cores"
2121
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
22+
"github.com/arduino/arduino-cli/arduino/resources"
2223
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2324
)
2425

2526
// DownloadToolRelease downloads a ToolRelease
2627
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
27-
config, err := GetDownloaderConfig()
28+
config, err := resources.GetDownloaderConfig()
2829
if err != nil {
2930
return err
3031
}
3132
resp, err := pm.DownloadToolRelease(toolRelease, config)
3233
if err != nil {
3334
return err
3435
}
35-
return Download(resp, toolRelease.String(), downloadCB)
36+
return resources.Download(resp, toolRelease.String(), downloadCB.FromRPC())
3637
}
3738

3839
// InstallToolRelease installs a ToolRelease

commands/core/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/arduino/arduino-cli/arduino"
2323
"github.com/arduino/arduino-cli/arduino/cores"
2424
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
25+
"github.com/arduino/arduino-cli/arduino/resources"
2526
"github.com/arduino/arduino-cli/commands"
2627
"github.com/arduino/arduino-cli/i18n"
2728
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -66,15 +67,15 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
6667

6768
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
6869
// Download platform
69-
config, err := commands.GetDownloaderConfig()
70+
config, err := resources.GetDownloaderConfig()
7071
if err != nil {
7172
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7273
}
7374
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
7475
if err != nil {
7576
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7677
}
77-
return commands.Download(resp, platformRelease.String(), downloadCB)
78+
return resources.Download(resp, platformRelease.String(), downloadCB.FromRPC())
7879
}
7980

8081
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {

commands/instances.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/arduino/arduino-cli/arduino/libraries"
3232
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
3333
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
34+
"github.com/arduino/arduino-cli/arduino/resources"
3435
"github.com/arduino/arduino-cli/arduino/security"
3536
sk "github.com/arduino/arduino-cli/arduino/sketch"
3637
"github.com/arduino/arduino-cli/arduino/utils"
@@ -362,13 +363,13 @@ func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse
362363
}
363364

364365
// UpdateLibrariesIndex updates the library_index.json
365-
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequest, downloadCB func(*rpc.DownloadProgress)) error {
366+
func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequest, downloadCB DownloadProgressCB) error {
366367
logrus.Info("Updating libraries index")
367368
lm := GetLibraryManager(req.GetInstance().GetId())
368369
if lm == nil {
369370
return &arduino.InvalidInstanceError{}
370371
}
371-
config, err := GetDownloaderConfig()
372+
config, err := resources.GetDownloaderConfig()
372373
if err != nil {
373374
return err
374375
}
@@ -387,7 +388,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
387388
// Download gzipped library_index
388389
tmpIndexGz := tmp.Join("library_index.json.gz")
389390
if d, err := downloader.DownloadWithConfig(tmpIndexGz.String(), librariesmanager.LibraryIndexGZURL.String(), *config, downloader.NoResume); err == nil {
390-
if err := Download(d, tr("Updating index: library_index.json.gz"), downloadCB); err != nil {
391+
if err := resources.Download(d, tr("Updating index: library_index.json.gz"), downloadCB.FromRPC()); err != nil {
391392
return &arduino.FailedDownloadError{Message: tr("Error downloading library_index.json.gz"), Cause: err}
392393
}
393394
} else {
@@ -397,7 +398,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
397398
// Download signature
398399
tmpSignature := tmp.Join("library_index.json.sig")
399400
if d, err := downloader.DownloadWithConfig(tmpSignature.String(), librariesmanager.LibraryIndexSignature.String(), *config, downloader.NoResume); err == nil {
400-
if err := Download(d, tr("Updating index: library_index.json.sig"), downloadCB); err != nil {
401+
if err := resources.Download(d, tr("Updating index: library_index.json.sig"), downloadCB.FromRPC()); err != nil {
401402
return &arduino.FailedDownloadError{Message: tr("Error downloading library_index.json.sig"), Cause: err}
402403
}
403404
} else {
@@ -477,7 +478,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
477478
}
478479
defer tmp.Remove()
479480

480-
config, err := GetDownloaderConfig()
481+
config, err := resources.GetDownloaderConfig()
481482
if err != nil {
482483
return nil, &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", URL), Cause: err}
483484
}
@@ -486,7 +487,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
486487
return nil, &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", URL), Cause: err}
487488
}
488489
coreIndexPath := indexpath.Join(path.Base(URL.Path))
489-
err = Download(d, tr("Updating index: %s", coreIndexPath.Base()), downloadCB)
490+
err = resources.Download(d, tr("Updating index: %s", coreIndexPath.Base()), downloadCB.FromRPC())
490491
if err != nil {
491492
return nil, &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", URL), Cause: err}
492493
}
@@ -516,7 +517,7 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
516517
}
517518

518519
coreIndexSigPath = indexpath.Join(path.Base(URLSig.Path))
519-
Download(d, tr("Updating index: %s", coreIndexSigPath.Base()), downloadCB)
520+
resources.Download(d, tr("Updating index: %s", coreIndexSigPath.Base()), downloadCB.FromRPC())
520521
if d.Error() != nil {
521522
return nil, &arduino.FailedDownloadError{Message: tr("Error downloading index signature '%s'", URLSig), Cause: err}
522523
}
@@ -688,7 +689,7 @@ func getOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease {
688689

689690
// Upgrade downloads and installs outdated Cores and Libraries
690691
func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadProgressCB, taskCB TaskProgressCB) error {
691-
downloaderConfig, err := GetDownloaderConfig()
692+
downloaderConfig, err := resources.GetDownloaderConfig()
692693
if err != nil {
693694
return err
694695
}
@@ -712,7 +713,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadPr
712713
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", available)})
713714
if d, err := available.Resource.Download(lm.DownloadsDir, downloaderConfig); err != nil {
714715
return &arduino.FailedDownloadError{Message: tr("Error downloading library"), Cause: err}
715-
} else if err := Download(d, available.String(), downloadCB); err != nil {
716+
} else if err := resources.Download(d, available.String(), downloadCB.FromRPC()); err != nil {
716717
return &arduino.FailedDownloadError{Message: tr("Error downloading library"), Cause: err}
717718
}
718719

@@ -795,7 +796,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadPr
795796
// Downloads platform
796797
if d, err := pm.DownloadPlatformRelease(latest, downloaderConfig); err != nil {
797798
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", latest), Cause: err}
798-
} else if err := Download(d, latest.String(), downloadCB); err != nil {
799+
} else if err := resources.Download(d, latest.String(), downloadCB.FromRPC()); err != nil {
799800
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", latest), Cause: err}
800801
}
801802

commands/lib/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/arduino/arduino-cli/arduino"
2222
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2323
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
24+
"github.com/arduino/arduino-cli/arduino/resources"
2425
"github.com/arduino/arduino-cli/commands"
2526
"github.com/arduino/arduino-cli/i18n"
2627
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -56,13 +57,13 @@ func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librarie
5657
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
5758

5859
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", libRelease)})
59-
config, err := commands.GetDownloaderConfig()
60+
config, err := resources.GetDownloaderConfig()
6061
if err != nil {
6162
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6263
}
6364
if d, err := libRelease.Resource.Download(lm.DownloadsDir, config); err != nil {
6465
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
65-
} else if err := commands.Download(d, libRelease.String(), downloadCB); err != nil {
66+
} else if err := resources.Download(d, libRelease.String(), downloadCB.FromRPC()); err != nil {
6667
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6768
}
6869
taskCB(&rpc.TaskProgress{Completed: true})

commands/progress.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@
1515

1616
package commands
1717

18-
import rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
18+
import (
19+
"github.com/arduino/arduino-cli/arduino/resources"
20+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
21+
)
1922

2023
// DownloadProgressCB is a callback to get updates on download progress
2124
type DownloadProgressCB func(curr *rpc.DownloadProgress)
2225

26+
// FromRPC converts the gRPC DownloadProgessCB in a resources.DownloadProgressCB
27+
func (rpcCB DownloadProgressCB) FromRPC() resources.DownloadProgressCB {
28+
return func(cb *resources.DownloadProgress) {
29+
rpcCB(&rpc.DownloadProgress{
30+
Url: cb.URL,
31+
File: cb.File,
32+
TotalSize: cb.TotalSize,
33+
Downloaded: cb.Downloaded,
34+
Completed: cb.Completed,
35+
})
36+
}
37+
}
38+
2339
// TaskProgressCB is a callback to receive progress messages
2440
type TaskProgressCB func(msg *rpc.TaskProgress)

0 commit comments

Comments
 (0)