Skip to content

Commit 7efde33

Browse files
committed
Moved DownloadFile from 'resources' package into 'httpclient'
1 parent ec606c4 commit 7efde33

File tree

11 files changed

+98
-90
lines changed

11 files changed

+98
-90
lines changed

arduino/cores/packagemanager/download.go

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

2121
"github.com/arduino/arduino-cli/arduino/cores"
22-
"github.com/arduino/arduino-cli/arduino/resources"
22+
"github.com/arduino/arduino-cli/arduino/httpclient"
2323
"go.bug.st/downloader/v2"
2424
semver "go.bug.st/relaxed-semver"
2525
)
@@ -118,7 +118,7 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
118118

119119
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
120120
// is returned.
121-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config, label string, progressCB resources.DownloadProgressCB) error {
121+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config, label string, progressCB httpclient.DownloadProgressCB) error {
122122
resource := tool.GetCompatibleFlavour()
123123
if resource == nil {
124124
return fmt.Errorf(tr("tool not available for your OS"))
@@ -128,6 +128,6 @@ func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *d
128128

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

arduino/httpclient/httpclient.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,54 @@ package httpclient
1818
import (
1919
"net/http"
2020
"net/url"
21+
"time"
2122

23+
"github.com/arduino/arduino-cli/arduino"
2224
"github.com/arduino/arduino-cli/configuration"
25+
"github.com/arduino/arduino-cli/i18n"
26+
"github.com/arduino/go-paths-helper"
27+
"go.bug.st/downloader/v2"
2328
)
2429

30+
var tr = i18n.Tr
31+
32+
// DownloadFile downloads a file from a URL into the specified path. An optional config and options may be passed (or nil to use the defaults).
33+
// A DownloadProgressCB callback function must be passed to monitor download progress.
34+
func DownloadFile(path *paths.Path, URL string, label string, downloadCB DownloadProgressCB, config *downloader.Config, options ...downloader.DownloadOptions) error {
35+
if config == nil {
36+
c, err := GetDownloaderConfig()
37+
if err != nil {
38+
return err
39+
}
40+
config = c
41+
}
42+
43+
d, err := downloader.DownloadWithConfig(path.String(), URL, *config, options...)
44+
if err != nil {
45+
return err
46+
}
47+
downloadCB(&DownloadProgress{
48+
File: label,
49+
URL: d.URL,
50+
TotalSize: d.Size(),
51+
})
52+
53+
err = d.RunAndPoll(func(downloaded int64) {
54+
downloadCB(&DownloadProgress{Downloaded: downloaded})
55+
}, 250*time.Millisecond)
56+
if err != nil {
57+
return err
58+
}
59+
60+
// The URL is not reachable for some reason
61+
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
62+
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
63+
}
64+
65+
downloadCB(&DownloadProgress{Completed: true})
66+
return nil
67+
}
68+
2569
// Config is the configuration of the http client
2670
type Config struct {
2771
UserAgent string
@@ -50,6 +94,35 @@ func NewWithConfig(config *Config) *http.Client {
5094
}
5195
}
5296

97+
// DownloadProgress is a report of the download progress, not all fields may be
98+
// filled and multiple reports may be sent during a download.
99+
type DownloadProgress struct {
100+
// URL of the download.
101+
URL string
102+
// The file being downloaded.
103+
File string
104+
// TotalSize is the total size of the file being downloaded.
105+
TotalSize int64
106+
// Downloaded is the size of the downloaded portion of the file.
107+
Downloaded int64
108+
// Completed reports whether the download is complete.
109+
Completed bool
110+
}
111+
112+
// DownloadProgressCB is a callback function to report download progress
113+
type DownloadProgressCB func(progress *DownloadProgress)
114+
115+
// GetDownloaderConfig returns the downloader configuration based on current settings.
116+
func GetDownloaderConfig() (*downloader.Config, error) {
117+
httpClient, err := New()
118+
if err != nil {
119+
return nil, &arduino.InvalidArgumentError{Message: tr("Could not connect via HTTP"), Cause: err}
120+
}
121+
return &downloader.Config{
122+
HttpClient: *httpClient,
123+
}, nil
124+
}
125+
53126
type httpClientRoundTripper struct {
54127
transport http.RoundTripper
55128
userAgent string

arduino/resources/download.go

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ package resources
1818
import (
1919
"fmt"
2020
"os"
21-
"time"
2221

23-
"github.com/arduino/arduino-cli/arduino"
2422
"github.com/arduino/arduino-cli/arduino/httpclient"
2523
paths "github.com/arduino/go-paths-helper"
2624
"go.bug.st/downloader/v2"
2725
)
2826

2927
// Download performs a download loop using the provided downloader.Downloader.
3028
// 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 {
29+
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config, label string, downloadCB httpclient.DownloadProgressCB) error {
3230
path, err := r.ArchivePath(downloadDir)
3331
if err != nil {
3432
return fmt.Errorf(tr("getting archive path: %s"), err)
@@ -47,7 +45,7 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.
4745
// File is cached, nothing to do here
4846

4947
// This signal means that the file is already downloaded
50-
downloadCB(&DownloadProgress{
48+
downloadCB(&httpclient.DownloadProgress{
5149
File: label,
5250
Completed: true,
5351
})
@@ -56,71 +54,5 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.
5654
} else {
5755
return fmt.Errorf(tr("getting archive file info: %s"), err)
5856
}
59-
return DownloadFile(path, r.URL, label, downloadCB, config)
57+
return httpclient.DownloadFile(path, r.URL, label, downloadCB, config)
6058
}
61-
62-
// DownloadFile downloads a file from a URL into the specified path. An optional config and options may be passed (or nil to use the defaults).
63-
// A DownloadProgressCB callback function must be passed to monitor download progress.
64-
func DownloadFile(path *paths.Path, URL string, label string, downloadCB DownloadProgressCB, config *downloader.Config, options ...downloader.DownloadOptions) error {
65-
if config == nil {
66-
c, err := GetDownloaderConfig()
67-
if err != nil {
68-
return err
69-
}
70-
config = c
71-
}
72-
73-
d, err := downloader.DownloadWithConfig(path.String(), URL, *config, options...)
74-
if err != nil {
75-
return err
76-
}
77-
downloadCB(&DownloadProgress{
78-
File: label,
79-
URL: d.URL,
80-
TotalSize: d.Size(),
81-
})
82-
83-
err = d.RunAndPoll(func(downloaded int64) {
84-
downloadCB(&DownloadProgress{Downloaded: downloaded})
85-
}, 250*time.Millisecond)
86-
if err != nil {
87-
return err
88-
}
89-
90-
// The URL is not reachable for some reason
91-
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
92-
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
93-
}
94-
95-
downloadCB(&DownloadProgress{Completed: true})
96-
return nil
97-
}
98-
99-
// GetDownloaderConfig returns the downloader configuration based on current settings.
100-
func GetDownloaderConfig() (*downloader.Config, error) {
101-
httpClient, err := httpclient.New()
102-
if err != nil {
103-
return nil, &arduino.InvalidArgumentError{Message: tr("Could not connect via HTTP"), Cause: err}
104-
}
105-
return &downloader.Config{
106-
HttpClient: *httpClient,
107-
}, nil
108-
}
109-
110-
// DownloadProgress is a report of the download progress, not all fields may be
111-
// filled and multiple reports may be sent during a download.
112-
type DownloadProgress struct {
113-
// URL of the download.
114-
URL string
115-
// The file being downloaded.
116-
File string
117-
// TotalSize is the total size of the file being downloaded.
118-
TotalSize int64
119-
// Downloaded is the size of the downloaded portion of the file.
120-
Downloaded int64
121-
// Completed reports whether the download is complete.
122-
Completed bool
123-
}
124-
125-
// DownloadProgressCB is a callback function to report download progress
126-
type DownloadProgressCB func(progress *DownloadProgress)

arduino/resources/helpers_test.go

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

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

58-
err = r.Download(tmp, &downloader.Config{HttpClient: *httpClient}, "", func(progress *DownloadProgress) {})
58+
err = r.Download(tmp, &downloader.Config{HttpClient: *httpClient}, "", func(progress *httpclient.DownloadProgress) {})
5959
require.NoError(t, err)
6060

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

arduino/resources/index.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/arduino/arduino-cli/arduino"
24+
"github.com/arduino/arduino-cli/arduino/httpclient"
2425
"github.com/arduino/arduino-cli/arduino/security"
2526
"github.com/arduino/go-paths-helper"
2627
"go.bug.st/downloader/v2"
@@ -34,7 +35,7 @@ type IndexResource struct {
3435

3536
// Download will download the index and possibly check the signature using the Arduino's public key.
3637
// If the file is in .gz format it will be unpacked first.
37-
func (res *IndexResource) Download(destDir *paths.Path, downloadCB DownloadProgressCB) error {
38+
func (res *IndexResource) Download(destDir *paths.Path, downloadCB httpclient.DownloadProgressCB) error {
3839
// Create destination directory
3940
if err := destDir.MkdirAll(); err != nil {
4041
return &arduino.PermissionDeniedError{Message: tr("Can't create data directory %s", destDir), Cause: err}
@@ -50,7 +51,7 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB DownloadProgr
5051
// Download index file
5152
indexFileName := path.Base(res.URL.Path) // == pakcage_index.json[.gz]
5253
tmpIndexPath := tmp.Join(indexFileName)
53-
if err := DownloadFile(tmpIndexPath, res.URL.String(), tr("Downloading index: %s", indexFileName), downloadCB, nil, downloader.NoResume); err != nil {
54+
if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), tr("Downloading index: %s", indexFileName), downloadCB, nil, downloader.NoResume); err != nil {
5455
return &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", res.URL), Cause: err}
5556
}
5657

@@ -73,7 +74,7 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB DownloadProgr
7374
// Download signature
7475
signaturePath = destDir.Join(signatureFileName)
7576
tmpSignaturePath = tmp.Join(signatureFileName)
76-
if err := DownloadFile(tmpSignaturePath, res.SignatureURL.String(), tr("Downloading index signature: %s", signatureFileName), downloadCB, nil, downloader.NoResume); err != nil {
77+
if err := httpclient.DownloadFile(tmpSignaturePath, res.SignatureURL.String(), tr("Downloading index signature: %s", signatureFileName), downloadCB, nil, downloader.NoResume); err != nil {
7778
return &arduino.FailedDownloadError{Message: tr("Error downloading index signature '%s'", res.SignatureURL), Cause: err}
7879
}
7980

arduino/resources/resources_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/hex"
2121
"testing"
2222

23+
"github.com/arduino/arduino-cli/arduino/httpclient"
2324
"github.com/arduino/go-paths-helper"
2425
"github.com/stretchr/testify/require"
2526
"go.bug.st/downloader/v2"
@@ -44,7 +45,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4445
require.NoError(t, err)
4546

4647
downloadAndTestChecksum := func() {
47-
err := r.Download(tmp, &downloader.Config{}, "", func(*DownloadProgress) {})
48+
err := r.Download(tmp, &downloader.Config{}, "", func(*httpclient.DownloadProgress) {})
4849
require.NoError(t, err)
4950

5051
data, err := testFile.ReadFile()
@@ -58,7 +59,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5859
downloadAndTestChecksum()
5960

6061
// Download with cached file
61-
err = r.Download(tmp, &downloader.Config{}, "", func(*DownloadProgress) {})
62+
err = r.Download(tmp, &downloader.Config{}, "", func(*httpclient.DownloadProgress) {})
6263
require.NoError(t, err)
6364

6465
// Download if cached file has data in excess (redownload)

commands/bundled_tools.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ 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"
22+
"github.com/arduino/arduino-cli/arduino/httpclient"
2323
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2424
)
2525

2626
// DownloadToolRelease downloads a ToolRelease
2727
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
28-
config, err := resources.GetDownloaderConfig()
28+
config, err := httpclient.GetDownloaderConfig()
2929
if err != nil {
3030
return err
3131
}

commands/core/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +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"
25+
"github.com/arduino/arduino-cli/arduino/httpclient"
2626
"github.com/arduino/arduino-cli/commands"
2727
"github.com/arduino/arduino-cli/i18n"
2828
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -67,7 +67,7 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
6767

6868
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
6969
// Download platform
70-
config, err := resources.GetDownloaderConfig()
70+
config, err := httpclient.GetDownloaderConfig()
7171
if err != nil {
7272
return &arduino.FailedDownloadError{Message: tr("Error downloading platform %s", platformRelease), Cause: err}
7373
}

commands/instances.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/arduino/cores"
2828
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2929
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
30+
"github.com/arduino/arduino-cli/arduino/httpclient"
3031
"github.com/arduino/arduino-cli/arduino/libraries"
3132
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
3233
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -580,7 +581,7 @@ func getOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease {
580581

581582
// Upgrade downloads and installs outdated Cores and Libraries
582583
func Upgrade(ctx context.Context, req *rpc.UpgradeRequest, downloadCB DownloadProgressCB, taskCB TaskProgressCB) error {
583-
downloaderConfig, err := resources.GetDownloaderConfig()
584+
downloaderConfig, err := httpclient.GetDownloaderConfig()
584585
if err != nil {
585586
return err
586587
}

commands/lib/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"context"
2020

2121
"github.com/arduino/arduino-cli/arduino"
22+
"github.com/arduino/arduino-cli/arduino/httpclient"
2223
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2324
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
24-
"github.com/arduino/arduino-cli/arduino/resources"
2525
"github.com/arduino/arduino-cli/commands"
2626
"github.com/arduino/arduino-cli/i18n"
2727
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -57,7 +57,7 @@ func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librarie
5757
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
5858

5959
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", libRelease)})
60-
config, err := resources.GetDownloaderConfig()
60+
config, err := httpclient.GetDownloaderConfig()
6161
if err != nil {
6262
return &arduino.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
6363
}

commands/progress.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
package commands
1717

1818
import (
19-
"github.com/arduino/arduino-cli/arduino/resources"
19+
"github.com/arduino/arduino-cli/arduino/httpclient"
2020
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2121
)
2222

2323
// DownloadProgressCB is a callback to get updates on download progress
2424
type DownloadProgressCB func(curr *rpc.DownloadProgress)
2525

2626
// FromRPC converts the gRPC DownloadProgessCB in a resources.DownloadProgressCB
27-
func (rpcCB DownloadProgressCB) FromRPC() resources.DownloadProgressCB {
28-
return func(cb *resources.DownloadProgress) {
27+
func (rpcCB DownloadProgressCB) FromRPC() httpclient.DownloadProgressCB {
28+
return func(cb *httpclient.DownloadProgress) {
2929
rpcCB(&rpc.DownloadProgress{
3030
Url: cb.URL,
3131
File: cb.File,

0 commit comments

Comments
 (0)