Skip to content

Commit 464dbb5

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 2237ca2 commit 464dbb5

22 files changed

+82
-111
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 4 additions & 5 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 10 additions & 0 deletions
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"
@@ -45,6 +46,7 @@ import (
4546
"github.com/sirupsen/logrus"
4647
"github.com/spf13/cobra"
4748
"github.com/spf13/viper"
49+
"go.bug.st/downloader"
4850
)
4951

5052
var (
@@ -254,4 +256,12 @@ func preRun(cmd *cobra.Command, args []string) {
254256
os.Exit(errorcodes.ErrBadCall)
255257
})
256258
}
259+
260+
//
261+
// Configure network
262+
//
263+
netConf := downloader.Config{
264+
RequestHeaders: globals.NewHTTPClientHeader(),
265+
}
266+
downloader.SetDefaultConfig(netConf)
257267
}

cli/core/download.go

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 16 deletions
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"
@@ -39,6 +38,7 @@ import (
3938
"github.com/sirupsen/logrus"
4039
"github.com/spf13/cobra"
4140
"github.com/spf13/viper"
41+
"go.bug.st/downloader"
4242
"google.golang.org/grpc"
4343
)
4444

@@ -71,22 +71,19 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
7171
port := viper.GetString("daemon.port")
7272
s := grpc.NewServer()
7373

74-
// Compose user agent header
75-
headers := http.Header{
76-
"User-Agent": []string{
77-
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
78-
globals.VersionInfo.Application,
79-
globals.VersionInfo.VersionString,
80-
runtime.GOARCH,
81-
runtime.GOOS,
82-
runtime.Version(),
83-
globals.VersionInfo.Commit),
84-
},
85-
}
86-
// Register the commands service
74+
// Set specific user-agent for the daemon
75+
netConf := downloader.GetDefaultConfig()
76+
netConf.RequestHeaders.Set("User-Agent",
77+
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
78+
globals.VersionInfo.Application,
79+
globals.VersionInfo.VersionString,
80+
runtime.GOARCH, runtime.GOOS,
81+
runtime.Version(), globals.VersionInfo.Commit))
82+
downloader.SetDefaultConfig(netConf)
83+
84+
// register the commands service
8785
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{
88-
DownloaderHeaders: headers,
89-
VersionString: globals.VersionInfo.VersionString,
86+
VersionString: globals.VersionInfo.VersionString,
9087
})
9188

9289
// Register the monitors service

cli/instance/instance.go

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/arduino/arduino-cli/cli/errorcodes"
2323
"github.com/arduino/arduino-cli/cli/feedback"
24-
"github.com/arduino/arduino-cli/cli/globals"
2524
"github.com/arduino/arduino-cli/cli/instance"
2625
"github.com/arduino/arduino-cli/cli/output"
2726
"github.com/arduino/arduino-cli/commands/lib"
@@ -57,8 +56,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
5756
Name: library.Name,
5857
Version: library.Version,
5958
}
60-
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar(),
61-
globals.NewHTTPClientHeader())
59+
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar())
6260
if err != nil {
6361
feedback.Errorf("Error downloading %s: %v", library, err)
6462
os.Exit(errorcodes.ErrNetwork)

cli/lib/install.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/arduino/arduino-cli/cli/errorcodes"
2323
"github.com/arduino/arduino-cli/cli/feedback"
24-
"github.com/arduino/arduino-cli/cli/globals"
2524
"github.com/arduino/arduino-cli/cli/instance"
2625
"github.com/arduino/arduino-cli/cli/output"
2726
"github.com/arduino/arduino-cli/commands/lib"
@@ -95,8 +94,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9594
Name: library.Name,
9695
Version: library.VersionRequired,
9796
}
98-
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(),
99-
output.TaskProgress(), globals.NewHTTPClientHeader())
97+
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
10098
if err != nil {
10199
feedback.Errorf("Error installing %s: %v", library, err)
102100
os.Exit(errorcodes.ErrGeneric)

cli/lib/upgrade.go

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 7 additions & 11 deletions
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
}

0 commit comments

Comments
 (0)