Skip to content

Commit aa23ea1

Browse files
committed
Merge Download subroutine for resources
1 parent caab911 commit aa23ea1

File tree

8 files changed

+51
-69
lines changed

8 files changed

+51
-69
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"github.com/arduino/arduino-cli/arduino/cores"
22+
"github.com/arduino/arduino-cli/arduino/resources"
2223
"go.bug.st/downloader/v2"
2324
semver "go.bug.st/relaxed-semver"
2425
)
@@ -117,16 +118,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
117118

118119
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
119120
// is returned.
120-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config) (*downloader.Downloader, error) {
121+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config, label string, progressCB resources.DownloadProgressCB) error {
121122
resource := tool.GetCompatibleFlavour()
122123
if resource == nil {
123-
return nil, fmt.Errorf(tr("tool not available for your OS"))
124+
return fmt.Errorf(tr("tool not available for your OS"))
124125
}
125-
return resource.Download(pm.DownloadDir, config)
126+
return resource.Download(pm.DownloadDir, config, label, progressCB)
126127
}
127128

128129
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
129130
// nil Downloader is returned.
130-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config) (*downloader.Downloader, error) {
131-
return platform.Resource.Download(pm.DownloadDir, config)
131+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config, label string, progressCB resources.DownloadProgressCB) error {
132+
return platform.Resource.Download(pm.DownloadDir, config, label, progressCB)
132133
}

arduino/resources/download.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import (
2626
"go.bug.st/downloader/v2"
2727
)
2828

29-
// Download a DownloadResource.
30-
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config) (*downloader.Downloader, error) {
29+
// Download performs a download loop using the provided downloader.Downloader.
30+
// Messages are passed back to the DownloadProgressCB using label as text for the File field.
31+
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config, label string, downloadCB DownloadProgressCB) error {
3132
path, err := r.ArchivePath(downloadDir)
3233
if err != nil {
33-
return nil, fmt.Errorf(tr("getting archive path: %s"), err)
34+
return fmt.Errorf(tr("getting archive path: %s"), err)
3435
}
3536

3637
if _, err := path.Stat(); os.IsNotExist(err) {
@@ -40,17 +41,46 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.
4041
ok, err := r.TestLocalArchiveIntegrity(downloadDir)
4142
if err != nil || !ok {
4243
if err := path.Remove(); err != nil {
43-
return nil, fmt.Errorf(tr("removing corrupted archive file: %s"), err)
44+
return fmt.Errorf(tr("removing corrupted archive file: %s"), err)
4445
}
4546
} else {
4647
// File is cached, nothing to do here
47-
return nil, nil
48+
49+
// This signal means that the file is already downloaded
50+
downloadCB(&DownloadProgress{
51+
File: label,
52+
Completed: true,
53+
})
54+
return nil
4855
}
4956
} else {
50-
return nil, fmt.Errorf(tr("getting archive file info: %s"), err)
57+
return fmt.Errorf(tr("getting archive file info: %s"), err)
58+
}
59+
60+
d, err := downloader.DownloadWithConfig(path.String(), r.URL, *config)
61+
if err != nil {
62+
return err
63+
}
64+
downloadCB(&DownloadProgress{
65+
File: label,
66+
URL: d.URL,
67+
TotalSize: d.Size(),
68+
})
69+
70+
err = d.RunAndPoll(func(downloaded int64) {
71+
downloadCB(&DownloadProgress{Downloaded: downloaded})
72+
}, 250*time.Millisecond)
73+
if err != nil {
74+
return err
75+
}
76+
77+
// The URL is not reachable for some reason
78+
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
79+
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
5180
}
5281

53-
return downloader.DownloadWithConfig(path.String(), r.URL, *config)
82+
downloadCB(&DownloadProgress{Completed: true})
83+
return nil
5484
}
5585

5686
// GetDownloaderConfig returns the downloader configuration based on
@@ -82,33 +112,3 @@ type DownloadProgress struct {
82112

83113
// DownloadProgressCB is a callback function to report download progress
84114
type DownloadProgressCB func(progress *DownloadProgress)
85-
86-
// Download performs a download loop using the provided downloader.Downloader.
87-
// Messages are passed back to the DownloadProgressCB using label as text for the File field.
88-
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {
89-
if d == nil {
90-
// This signal means that the file is already downloaded
91-
downloadCB(&DownloadProgress{
92-
File: label,
93-
Completed: true,
94-
})
95-
return nil
96-
}
97-
downloadCB(&DownloadProgress{
98-
File: label,
99-
URL: d.URL,
100-
TotalSize: d.Size(),
101-
})
102-
d.RunAndPoll(func(downloaded int64) {
103-
downloadCB(&DownloadProgress{Downloaded: downloaded})
104-
}, 250*time.Millisecond)
105-
if d.Error() != nil {
106-
return d.Error()
107-
}
108-
// The URL is not reachable for some reason
109-
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
110-
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
111-
}
112-
downloadCB(&DownloadProgress{Completed: true})
113-
return nil
114-
}

arduino/resources/helpers_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5555

5656
httpClient := httpclient.NewWithConfig(&httpclient.Config{UserAgent: goldUserAgentValue})
5757

58-
d, err := r.Download(tmp, &downloader.Config{HttpClient: *httpClient})
59-
require.NoError(t, err)
60-
err = d.Run()
58+
err = r.Download(tmp, &downloader.Config{HttpClient: *httpClient}, "", func(progress *DownloadProgress) {})
6159
require.NoError(t, err)
6260

6361
// leverage the download helper to download the echo for the request made by the downloader itself

arduino/resources/resources_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4444
require.NoError(t, err)
4545

4646
downloadAndTestChecksum := func() {
47-
d, err := r.Download(tmp, &downloader.Config{})
48-
require.NoError(t, err)
49-
err = d.Run()
47+
err := r.Download(tmp, &downloader.Config{}, "", func(*DownloadProgress) {})
5048
require.NoError(t, err)
5149

5250
data, err := testFile.ReadFile()
@@ -60,9 +58,8 @@ func TestDownloadAndChecksums(t *testing.T) {
6058
downloadAndTestChecksum()
6159

6260
// Download with cached file
63-
d, err := r.Download(tmp, &downloader.Config{})
61+
err = r.Download(tmp, &downloader.Config{}, "", func(*DownloadProgress) {})
6462
require.NoError(t, err)
65-
require.Nil(t, d)
6663

6764
// Download if cached file has data in excess (redownload)
6865
data, err := testFile.ReadFile()

commands/bundled_tools.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.T
2929
if err != nil {
3030
return err
3131
}
32-
resp, err := pm.DownloadToolRelease(toolRelease, config)
33-
if err != nil {
34-
return err
35-
}
36-
return resources.Download(resp, toolRelease.String(), downloadCB.FromRPC())
32+
return pm.DownloadToolRelease(toolRelease, config, toolRelease.String(), downloadCB.FromRPC())
3733
}
3834

3935
// InstallToolRelease installs a ToolRelease

commands/core/download.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,7 @@ func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.
7171
if err != nil {
7272
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7373
}
74-
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
75-
if err != nil {
76-
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
77-
}
78-
return resources.Download(resp, platformRelease.String(), downloadCB.FromRPC())
74+
return pm.DownloadPlatformRelease(platformRelease, config, platformRelease.String(), downloadCB.FromRPC())
7975
}
8076

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

commands/instances.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadPr
711711

712712
// Downloads latest library release
713713
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", available)})
714-
if d, err := available.Resource.Download(lm.DownloadsDir, downloaderConfig); err != nil {
715-
return &arduino.FailedDownloadError{Message: tr("Error downloading library"), Cause: err}
716-
} else if err := resources.Download(d, available.String(), downloadCB.FromRPC()); err != nil {
714+
if err := available.Resource.Download(lm.DownloadsDir, downloaderConfig, available.String(), downloadCB.FromRPC()); err != nil {
717715
return &arduino.FailedDownloadError{Message: tr("Error downloading library"), Cause: err}
718716
}
719717

@@ -794,9 +792,7 @@ func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadPr
794792
}
795793

796794
// Downloads platform
797-
if d, err := pm.DownloadPlatformRelease(latest, downloaderConfig); err != nil {
798-
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", latest), Cause: err}
799-
} else if err := resources.Download(d, latest.String(), downloadCB.FromRPC()); err != nil {
795+
if err := pm.DownloadPlatformRelease(latest, downloaderConfig, latest.String(), downloadCB.FromRPC()); err != nil {
800796
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", latest), Cause: err}
801797
}
802798

commands/lib/download.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librarie
6161
if err != nil {
6262
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6363
}
64-
if d, err := libRelease.Resource.Download(lm.DownloadsDir, config); err != nil {
65-
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
66-
} else if err := resources.Download(d, libRelease.String(), downloadCB.FromRPC()); err != nil {
64+
if err := libRelease.Resource.Download(lm.DownloadsDir, config, libRelease.String(), downloadCB.FromRPC()); err != nil {
6765
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6866
}
6967
taskCB(&rpc.TaskProgress{Completed: true})

0 commit comments

Comments
 (0)