Skip to content

Commit 5969f58

Browse files
committed
Refactored function to generate user-agent
1 parent d78ac23 commit 5969f58

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func preRun(cmd *cobra.Command, args []string) {
261261
// Configure network
262262
//
263263
netConf := downloader.Config{
264-
RequestHeaders: globals.NewHTTPClientHeader(),
264+
RequestHeaders: globals.NewHTTPClientHeader(""),
265265
}
266266
if viper.IsSet("network.proxy") {
267267
proxy := viper.GetString("network.proxy")

cli/daemon/daemon.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"io/ioutil"
2323
"net"
2424
"os"
25-
"runtime"
2625
"syscall"
2726

2827
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -73,12 +72,7 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
7372

7473
// Set specific user-agent for the daemon
7574
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))
75+
netConf.RequestHeaders = globals.NewHTTPClientHeader("daemon")
8276
downloader.SetDefaultConfig(netConf)
8377

8478
// register the commands service

cli/globals/globals.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ var (
3333
)
3434

3535
// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders
36-
func NewHTTPClientHeader() http.Header {
37-
userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s", VersionInfo.Application,
38-
VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), VersionInfo.Commit)
36+
// and adds the subComponent if specified
37+
func NewHTTPClientHeader(subComponent string) http.Header {
38+
if subComponent != "" {
39+
subComponent = " " + subComponent
40+
}
41+
userAgentValue := fmt.Sprintf("%s/%s%s (%s; %s; %s) Commit:%s",
42+
VersionInfo.Application,
43+
VersionInfo.VersionString,
44+
subComponent,
45+
runtime.GOARCH, runtime.GOOS, runtime.Version(),
46+
VersionInfo.Commit)
3947
return http.Header{"User-Agent": []string{userAgentValue}}
4048
}

commands/board/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
5151
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
5252
retVal := []*rpc.BoardListItem{}
5353
req, _ := http.NewRequest("GET", url, nil)
54-
req.Header = globals.NewHTTPClientHeader()
54+
req.Header = globals.NewHTTPClientHeader("")
5555
req.Header.Set("Content-Type", "application/json")
5656

5757
if res, err := http.DefaultClient.Do(req); err == nil {

0 commit comments

Comments
 (0)