Skip to content

Commit 9334716

Browse files
committed
Factored all progress reports callback definitions in the rpc package
1 parent 9cfb057 commit 9334716

File tree

21 files changed

+60
-95
lines changed

21 files changed

+60
-95
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/httpclient"
22+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
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 httpclient.DownloadProgressCB) error {
121+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config, label string, progressCB rpc.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 httpclient.DownloadProgressCB) error {
131+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config, label string, progressCB rpc.DownloadProgressCB) error {
132132
return platform.Resource.Download(pm.DownloadDir, config, label, progressCB)
133133
}

arduino/httpclient/httpclient.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/arduino"
2424
"github.com/arduino/arduino-cli/configuration"
2525
"github.com/arduino/arduino-cli/i18n"
26+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2627
"github.com/arduino/go-paths-helper"
2728
"go.bug.st/downloader/v2"
2829
)
@@ -31,7 +32,7 @@ var tr = i18n.Tr
3132

3233
// 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).
3334
// 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+
func DownloadFile(path *paths.Path, URL string, label string, downloadCB rpc.DownloadProgressCB, config *downloader.Config, options ...downloader.DownloadOptions) error {
3536
if config == nil {
3637
c, err := GetDownloaderConfig()
3738
if err != nil {
@@ -44,14 +45,14 @@ func DownloadFile(path *paths.Path, URL string, label string, downloadCB Downloa
4445
if err != nil {
4546
return err
4647
}
47-
downloadCB(&DownloadProgress{
48+
downloadCB(&rpc.DownloadProgress{
4849
File: label,
49-
URL: d.URL,
50+
Url: d.URL,
5051
TotalSize: d.Size(),
5152
})
5253

5354
err = d.RunAndPoll(func(downloaded int64) {
54-
downloadCB(&DownloadProgress{Downloaded: downloaded})
55+
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
5556
}, 250*time.Millisecond)
5657
if err != nil {
5758
return err
@@ -62,7 +63,7 @@ func DownloadFile(path *paths.Path, URL string, label string, downloadCB Downloa
6263
return &arduino.FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
6364
}
6465

65-
downloadCB(&DownloadProgress{Completed: true})
66+
downloadCB(&rpc.DownloadProgress{Completed: true})
6667
return nil
6768
}
6869

@@ -94,24 +95,6 @@ func NewWithConfig(config *Config) *http.Client {
9495
}
9596
}
9697

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-
11598
// GetDownloaderConfig returns the downloader configuration based on current settings.
11699
func GetDownloaderConfig() (*downloader.Config, error) {
117100
httpClient, err := New()

arduino/resources/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
"os"
2121

2222
"github.com/arduino/arduino-cli/arduino/httpclient"
23+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2324
paths "github.com/arduino/go-paths-helper"
2425
"go.bug.st/downloader/v2"
2526
)
2627

2728
// Download performs a download loop using the provided downloader.Downloader.
2829
// Messages are passed back to the DownloadProgressCB using label as text for the File field.
29-
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config, label string, downloadCB httpclient.DownloadProgressCB) error {
30+
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config, label string, downloadCB rpc.DownloadProgressCB) error {
3031
path, err := r.ArchivePath(downloadDir)
3132
if err != nil {
3233
return fmt.Errorf(tr("getting archive path: %s"), err)
@@ -45,7 +46,7 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.
4546
// File is cached, nothing to do here
4647

4748
// This signal means that the file is already downloaded
48-
downloadCB(&httpclient.DownloadProgress{
49+
downloadCB(&rpc.DownloadProgress{
4950
File: label,
5051
Completed: true,
5152
})

arduino/resources/helpers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424

2525
"github.com/arduino/arduino-cli/arduino/httpclient"
26+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2627
"github.com/arduino/go-paths-helper"
2728
"github.com/stretchr/testify/require"
2829
"go.bug.st/downloader/v2"
@@ -55,7 +56,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5556

5657
httpClient := httpclient.NewWithConfig(&httpclient.Config{UserAgent: goldUserAgentValue})
5758

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

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

arduino/resources/index.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/arduino"
2424
"github.com/arduino/arduino-cli/arduino/httpclient"
2525
"github.com/arduino/arduino-cli/arduino/security"
26+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2627
"github.com/arduino/go-paths-helper"
2728
"go.bug.st/downloader/v2"
2829
)
@@ -35,7 +36,7 @@ type IndexResource struct {
3536

3637
// Download will download the index and possibly check the signature using the Arduino's public key.
3738
// If the file is in .gz format it will be unpacked first.
38-
func (res *IndexResource) Download(destDir *paths.Path, downloadCB httpclient.DownloadProgressCB) error {
39+
func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadProgressCB) error {
3940
// Create destination directory
4041
if err := destDir.MkdirAll(); err != nil {
4142
return &arduino.PermissionDeniedError{Message: tr("Can't create data directory %s", destDir), Cause: err}

arduino/resources/resources_test.go

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

23-
"github.com/arduino/arduino-cli/arduino/httpclient"
23+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2424
"github.com/arduino/go-paths-helper"
2525
"github.com/stretchr/testify/require"
2626
"go.bug.st/downloader/v2"
@@ -45,7 +45,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4545
require.NoError(t, err)
4646

4747
downloadAndTestChecksum := func() {
48-
err := r.Download(tmp, &downloader.Config{}, "", func(*httpclient.DownloadProgress) {})
48+
err := r.Download(tmp, &downloader.Config{}, "", func(*rpc.DownloadProgress) {})
4949
require.NoError(t, err)
5050

5151
data, err := testFile.ReadFile()
@@ -59,7 +59,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5959
downloadAndTestChecksum()
6060

6161
// Download with cached file
62-
err = r.Download(tmp, &downloader.Config{}, "", func(*httpclient.DownloadProgress) {})
62+
err = r.Download(tmp, &downloader.Config{}, "", func(*rpc.DownloadProgress) {})
6363
require.NoError(t, err)
6464

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

cli/output/rpc_progress.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package output
1818
import (
1919
"fmt"
2020

21-
"github.com/arduino/arduino-cli/commands"
2221
"github.com/arduino/arduino-cli/i18n"
2322
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2423
"github.com/cmaglie/pb"
@@ -32,7 +31,7 @@ var (
3231

3332
// ProgressBar returns a DownloadProgressCB that prints a progress bar.
3433
// If JSON output format has been selected, the callback outputs nothing.
35-
func ProgressBar() commands.DownloadProgressCB {
34+
func ProgressBar() rpc.DownloadProgressCB {
3635
if OutputFormat != "json" {
3736
return NewDownloadProgressBarCB()
3837
}
@@ -43,7 +42,7 @@ func ProgressBar() commands.DownloadProgressCB {
4342

4443
// TaskProgress returns a TaskProgressCB that prints the task progress.
4544
// If JSON output format has been selected, the callback outputs nothing.
46-
func TaskProgress() commands.TaskProgressCB {
45+
func TaskProgress() rpc.TaskProgressCB {
4746
if OutputFormat != "json" {
4847
return NewTaskProgressCB()
4948
}

commands/board/attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
var tr = i18n.Tr
3737

3838
// Attach FIXMEDOC
39-
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResponse, error) {
39+
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB rpc.TaskProgressCB) (*rpc.BoardAttachResponse, error) {
4040
pm := commands.GetPackageManager(req.GetInstance().GetId())
4141
if pm == nil {
4242
return nil, &arduino.InvalidInstanceError{}

commands/bundled_tools.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ import (
2424
)
2525

2626
// DownloadToolRelease downloads a ToolRelease
27-
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
27+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB rpc.DownloadProgressCB) error {
2828
config, err := httpclient.GetDownloaderConfig()
2929
if err != nil {
3030
return err
3131
}
32-
return pm.DownloadToolRelease(toolRelease, config, toolRelease.String(), downloadCB.FromRPC())
32+
return pm.DownloadToolRelease(toolRelease, config, toolRelease.String(), downloadCB)
3333
}
3434

3535
// InstallToolRelease installs a ToolRelease
36-
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB TaskProgressCB) error {
36+
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB rpc.TaskProgressCB) error {
3737
log := pm.Log.WithField("Tool", toolRelease)
3838

3939
if toolRelease.IsInstalled() {

commands/compile/compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import (
4545
var tr = i18n.Tr
4646

4747
// Compile FIXMEDOC
48-
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, progressCB commands.TaskProgressCB, debug bool) (r *rpc.CompileResponse, e error) {
48+
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, progressCB rpc.TaskProgressCB, debug bool) (r *rpc.CompileResponse, e error) {
4949

5050
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
5151
// since we want this binding to work also for the gRPC interface we must read it here in this

commands/core/download.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
var tr = i18n.Tr
3232

3333
// PlatformDownload FIXMEDOC
34-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
34+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB rpc.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
3535
pm := commands.GetPackageManager(req.GetInstance().GetId())
3636
if pm == nil {
3737
return nil, &arduino.InvalidInstanceError{}
@@ -65,16 +65,16 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
6565
return &rpc.PlatformDownloadResponse{}, nil
6666
}
6767

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

77-
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {
77+
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB rpc.DownloadProgressCB) error {
7878
// Check if tool has a flavor available for the current OS
7979
if tool.GetCompatibleFlavour() == nil {
8080
return &arduino.FailedDownloadError{

commands/core/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// PlatformInstall FIXMEDOC
2929
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
30-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
30+
downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
3131

3232
pm := commands.GetPackageManager(req.GetInstance().GetId())
3333
if pm == nil {
@@ -63,7 +63,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
6363

6464
func installPlatform(pm *packagemanager.PackageManager,
6565
platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease,
66-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB,
66+
downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB,
6767
skipPostInstall bool) error {
6868
log := pm.Log.WithField("platform", platformRelease)
6969

commands/core/uninstall.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
// PlatformUninstall FIXMEDOC
29-
func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, taskCB commands.TaskProgressCB) (*rpc.PlatformUninstallResponse, error) {
29+
func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) (*rpc.PlatformUninstallResponse, error) {
3030
pm := commands.GetPackageManager(req.GetInstance().GetId())
3131
if pm == nil {
3232
return nil, &arduino.InvalidInstanceError{}
@@ -70,7 +70,7 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t
7070
return &rpc.PlatformUninstallResponse{}, nil
7171
}
7272

73-
func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, taskCB commands.TaskProgressCB) error {
73+
func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, taskCB rpc.TaskProgressCB) error {
7474
log := pm.Log.WithField("platform", platformRelease)
7575

7676
log.Info("Uninstalling platform")
@@ -86,7 +86,7 @@ func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease
8686
return nil
8787
}
8888

89-
func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB commands.TaskProgressCB) error {
89+
func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB rpc.TaskProgressCB) error {
9090
log := pm.Log.WithField("Tool", toolRelease)
9191

9292
log.Info("Uninstalling tool")

commands/core/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// PlatformUpgrade FIXMEDOC
2828
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
29-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformUpgradeResponse, error) {
29+
downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformUpgradeResponse, error) {
3030

3131
pm := commands.GetPackageManager(req.GetInstance().GetId())
3232
if pm == nil {
@@ -50,7 +50,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
5050
}
5151

5252
func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference,
53-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, skipPostInstall bool) error {
53+
downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, skipPostInstall bool) error {
5454
if platformRef.PlatformVersion != nil {
5555
return &arduino.InvalidArgumentError{Message: tr("Upgrade doesn't accept parameters with version")}
5656
}

0 commit comments

Comments
 (0)