Skip to content

Commit 63eb9fe

Browse files
committed
Use downloader.SetDefaultConfig() to set user-agent for the arduino-cli
This change allows to not pass-trough the downloader configuration from function to function everywhere (and sometime we forget to pass it for example in the "core update-index" command).
1 parent 025d931 commit 63eb9fe

22 files changed

+78
-99
lines changed

arduino/cores/packagemanager/download.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package packagemanager
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120

2221
"github.com/arduino/arduino-cli/arduino/cores"
2322
"go.bug.st/downloader"
@@ -101,16 +100,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
101100

102101
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
103102
// is returned.
104-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
103+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*downloader.Downloader, error) {
105104
resource := tool.GetCompatibleFlavour()
106105
if resource == nil {
107106
return nil, fmt.Errorf("tool not available for your OS")
108107
}
109-
return resource.Download(pm.DownloadDir, downloaderHeaders)
108+
return resource.Download(pm.DownloadDir)
110109
}
111110

112111
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
113112
// nil Downloader is returned.
114-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
115-
return platform.Resource.Download(pm.DownloadDir, downloaderHeaders)
113+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*downloader.Downloader, error) {
114+
return platform.Resource.Download(pm.DownloadDir)
116115
}

arduino/resources/helpers.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package resources
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120
"os"
2221

2322
"github.com/arduino/go-paths-helper"
@@ -44,7 +43,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
4443
}
4544

4645
// Download a DownloadResource.
47-
func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders http.Header) (*downloader.Downloader, error) {
46+
func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downloader, error) {
4847
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
4948
if err != nil {
5049
return nil, fmt.Errorf("testing local archive integrity: %s", err)
@@ -72,7 +71,5 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders h
7271
return nil, fmt.Errorf("getting archive file info: %s", err)
7372
}
7473

75-
downloadConfig := downloader.Config{
76-
RequestHeaders: downloaderHeaders}
77-
return downloader.DownloadWithConfig(path.String(), r.URL, downloadConfig)
74+
return downloader.Download(path.String(), r.URL)
7875
}

arduino/resources/helpers_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/arduino/go-paths-helper"
2727
"github.com/stretchr/testify/require"
28+
"go.bug.st/downloader"
2829
)
2930

3031
type EchoHandler struct{}
@@ -52,7 +53,10 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5253
URL: srv.URL,
5354
}
5455

55-
d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}})
56+
prev := downloader.GetDefaultConfig()
57+
downloader.SetDefaultConfig(downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
58+
d, err := r.Download(tmp)
59+
downloader.SetDefaultConfig(prev)
5660
require.NoError(t, err)
5761
err = d.Run()
5862
require.NoError(t, err)

arduino/resources/resources_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package resources
1818
import (
1919
"crypto"
2020
"encoding/hex"
21-
"net/http"
2221
"testing"
2322

2423
"github.com/arduino/go-paths-helper"
@@ -42,7 +41,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4241
require.NoError(t, err)
4342

4443
downloadAndTestChecksum := func() {
45-
d, err := r.Download(tmp, http.Header{})
44+
d, err := r.Download(tmp)
4645
require.NoError(t, err)
4746
err = d.Run()
4847
require.NoError(t, err)
@@ -58,7 +57,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5857
downloadAndTestChecksum()
5958

6059
// Download with cached file
61-
d, err := r.Download(tmp, http.Header{})
60+
d, err := r.Download(tmp)
6261
require.NoError(t, err)
6362
require.Nil(t, d)
6463

cli/cli.go

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cli
1818
import (
1919
"fmt"
2020
"io/ioutil"
21+
"net/url"
2122
"os"
2223
"path/filepath"
2324
"strings"
@@ -44,6 +45,7 @@ import (
4445
"github.com/sirupsen/logrus"
4546
"github.com/spf13/cobra"
4647
"github.com/spf13/viper"
48+
"go.bug.st/downloader"
4749
)
4850

4951
var (
@@ -250,4 +252,12 @@ func preRun(cmd *cobra.Command, args []string) {
250252
os.Exit(errorcodes.ErrBadCall)
251253
})
252254
}
255+
256+
//
257+
// Configure network
258+
//
259+
netConf := downloader.Config{
260+
RequestHeaders: globals.NewHTTPClientHeader(),
261+
}
262+
downloader.SetDefaultConfig(netConf)
253263
}

cli/core/download.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
6666
Architecture: platformRef.Architecture,
6767
Version: platformRef.Version,
6868
}
69-
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar(),
70-
globals.NewHTTPClientHeader())
69+
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar())
7170
if err != nil {
7271
feedback.Errorf("Error downloading %s: %v", args[i], err)
7372
os.Exit(errorcodes.ErrNetwork)

cli/core/install.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
6767
Architecture: platformRef.Architecture,
6868
Version: platformRef.Version,
6969
}
70-
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(),
71-
output.TaskProgress(), globals.NewHTTPClientHeader())
70+
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(), output.TaskProgress())
7271
if err != nil {
7372
feedback.Errorf("Error during install: %v", err)
7473
os.Exit(errorcodes.ErrGeneric)

cli/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9393
Architecture: platformRef.Architecture,
9494
}
9595

96-
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
96+
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
9797
if err == core.ErrAlreadyLatest {
9898
feedback.Printf("Platform %s is already at the latest version", platformRef)
9999
} else if err != nil {

cli/daemon/daemon.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io"
2222
"io/ioutil"
2323
"net"
24-
"net/http"
2524
"os"
2625
"runtime"
2726
"syscall"
@@ -37,6 +36,7 @@ import (
3736
"github.com/sirupsen/logrus"
3837
"github.com/spf13/cobra"
3938
"github.com/spf13/viper"
39+
"go.bug.st/downloader"
4040
"google.golang.org/grpc"
4141
)
4242

@@ -62,16 +62,19 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
6262
port := viper.GetString("daemon.port")
6363
s := grpc.NewServer()
6464

65-
// register the commands service
66-
headers := http.Header{"User-Agent": []string{
65+
// Set specific user-agent for the daemon
66+
netConf := downloader.GetDefaultConfig()
67+
netConf.RequestHeaders.Set("User-Agent",
6768
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
6869
globals.VersionInfo.Application,
6970
globals.VersionInfo.VersionString,
7071
runtime.GOARCH, runtime.GOOS,
71-
runtime.Version(), globals.VersionInfo.Commit)}}
72+
runtime.Version(), globals.VersionInfo.Commit))
73+
downloader.SetDefaultConfig(netConf)
74+
75+
// register the commands service
7276
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{
73-
DownloaderHeaders: headers,
74-
VersionString: globals.VersionInfo.VersionString,
77+
VersionString: globals.VersionInfo.VersionString,
7578
})
7679

7780
// Register the monitors service

cli/instance/instance.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package instance
1818
import (
1919
"context"
2020

21-
"github.com/arduino/arduino-cli/cli/globals"
2221
"github.com/arduino/arduino-cli/cli/output"
2322
"github.com/arduino/arduino-cli/commands"
2423
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -45,8 +44,7 @@ func CreateInstance() (*rpc.Instance, error) {
4544

4645
func getInitResponse() (*rpc.InitResp, error) {
4746
// invoke Init()
48-
resp, err := commands.Init(context.Background(), &rpc.InitReq{},
49-
output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
47+
resp, err := commands.Init(context.Background(), &rpc.InitReq{}, output.ProgressBar(), output.TaskProgress())
5048

5149
// Init() failed
5250
if err != nil {

cli/lib/download.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
5757
Name: library.Name,
5858
Version: library.Version,
5959
}
60-
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar(),
61-
globals.NewHTTPClientHeader())
60+
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar())
6261
if err != nil {
6362
feedback.Errorf("Error downloading %s: %v", library, err)
6463
os.Exit(errorcodes.ErrNetwork)

cli/lib/install.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9595
Name: library.Name,
9696
Version: library.VersionRequired,
9797
}
98-
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(),
99-
output.TaskProgress(), globals.NewHTTPClientHeader())
98+
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
10099
if err != nil {
101100
feedback.Errorf("Error installing %s: %v", library, err)
102101
os.Exit(errorcodes.ErrGeneric)

cli/lib/upgrade.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/arduino/arduino-cli/cli/errorcodes"
2222
"github.com/arduino/arduino-cli/cli/feedback"
23-
"github.com/arduino/arduino-cli/cli/globals"
2423
"github.com/arduino/arduino-cli/cli/instance"
2524
"github.com/arduino/arduino-cli/cli/output"
2625
"github.com/arduino/arduino-cli/commands/lib"
@@ -48,13 +47,13 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
4847
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
4948

5049
if len(args) == 0 {
51-
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
50+
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress())
5251
if err != nil {
5352
feedback.Errorf("Error upgrading libraries: %v", err)
5453
os.Exit(errorcodes.ErrGeneric)
5554
}
5655
} else {
57-
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
56+
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress())
5857
if err != nil {
5958
feedback.Errorf("Error upgrading libraries: %v", err)
6059
os.Exit(errorcodes.ErrGeneric)

commands/bundled_tools.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ package commands
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120

2221
"github.com/arduino/arduino-cli/arduino/cores"
2322
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2423
rpc "github.com/arduino/arduino-cli/rpc/commands"
2524
)
2625

2726
// DownloadToolRelease downloads a ToolRelease
28-
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease,
29-
downloadCB DownloadProgressCB, downloaderHeaders http.Header) error {
30-
resp, err := pm.DownloadToolRelease(toolRelease, downloaderHeaders)
27+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
28+
resp, err := pm.DownloadToolRelease(toolRelease)
3129
if err != nil {
3230
return err
3331
}

commands/core/download.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22-
"net/http"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores"
2524
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -28,8 +27,7 @@ import (
2827
)
2928

3029
// PlatformDownload FIXMEDOC
31-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB,
32-
downloaderHeaders http.Header) (*rpc.PlatformDownloadResp, error) {
30+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResp, error) {
3331
pm := commands.GetPackageManager(req.GetInstance().GetId())
3432
if pm == nil {
3533
return nil, errors.New("invalid instance")
@@ -49,13 +47,13 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa
4947
return nil, fmt.Errorf("find platform dependencies: %s", err)
5048
}
5149

52-
err = downloadPlatform(pm, platform, downloadCB, downloaderHeaders)
50+
err = downloadPlatform(pm, platform, downloadCB)
5351
if err != nil {
5452
return nil, err
5553
}
5654

5755
for _, tool := range tools {
58-
err := downloadTool(pm, tool, downloadCB, downloaderHeaders)
56+
err := downloadTool(pm, tool, downloadCB)
5957
if err != nil {
6058
return nil, fmt.Errorf("downloading tool %s: %s", tool, err)
6159
}
@@ -64,22 +62,20 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa
6462
return &rpc.PlatformDownloadResp{}, nil
6563
}
6664

67-
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease,
68-
downloadCB commands.DownloadProgressCB, downloaderHeaders http.Header) error {
65+
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
6966
// Download platform
70-
resp, err := pm.DownloadPlatformRelease(platformRelease, downloaderHeaders)
67+
resp, err := pm.DownloadPlatformRelease(platformRelease)
7168
if err != nil {
7269
return err
7370
}
7471
return commands.Download(resp, platformRelease.String(), downloadCB)
7572
}
7673

77-
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB,
78-
downloaderHeaders http.Header) error {
74+
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {
7975
// Check if tool has a flavor available for the current OS
8076
if tool.GetCompatibleFlavour() == nil {
8177
return fmt.Errorf("tool %s not available for the current OS", tool)
8278
}
8379

84-
return commands.DownloadToolRelease(pm, tool, downloadCB, downloaderHeaders)
80+
return commands.DownloadToolRelease(pm, tool, downloadCB)
8581
}

commands/core/install.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22-
"net/http"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores"
2524
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -29,7 +28,7 @@ import (
2928

3029
// PlatformInstall FIXMEDOC
3130
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
32-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) (*rpc.PlatformInstallResp, error) {
31+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResp, error) {
3332

3433
pm := commands.GetPackageManager(req.GetInstance().GetId())
3534
if pm == nil {
@@ -50,7 +49,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
5049
return nil, fmt.Errorf("finding platform dependencies: %s", err)
5150
}
5251

53-
err = installPlatform(pm, platform, tools, downloadCB, taskCB, downloaderHeaders)
52+
err = installPlatform(pm, platform, tools, downloadCB, taskCB)
5453
if err != nil {
5554
return nil, err
5655
}
@@ -65,7 +64,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
6564

6665
func installPlatform(pm *packagemanager.PackageManager,
6766
platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease,
68-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error {
67+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
6968
log := pm.Log.WithField("platform", platformRelease)
7069

7170
// Prerequisite checks before install
@@ -87,9 +86,9 @@ func installPlatform(pm *packagemanager.PackageManager,
8786
// Package download
8887
taskCB(&rpc.TaskProgress{Name: "Downloading packages"})
8988
for _, tool := range toolsToInstall {
90-
downloadTool(pm, tool, downloadCB, downloaderHeaders)
89+
downloadTool(pm, tool, downloadCB)
9190
}
92-
downloadPlatform(pm, platformRelease, downloadCB, downloaderHeaders)
91+
downloadPlatform(pm, platformRelease, downloadCB)
9392
taskCB(&rpc.TaskProgress{Completed: true})
9493

9594
// Install tools first

0 commit comments

Comments
 (0)