Skip to content

Commit 7b0ea75

Browse files
committed
Refactored function to generate user-agent
1 parent 99f4dfa commit 7b0ea75

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

cli/cli.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func preRun(cmd *cobra.Command, args []string) {
257257
// Configure network
258258
//
259259
netConf := downloader.Config{
260-
RequestHeaders: globals.NewHTTPClientHeader(),
260+
RequestHeaders: globals.NewHTTPClientHeader(""),
261261
}
262262
if viper.IsSet("network.proxy") {
263263
proxy := viper.GetString("network.proxy")

cli/daemon/daemon.go

+1-7
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"
@@ -64,12 +63,7 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
6463

6564
// Set specific user-agent for the daemon
6665
netConf := downloader.GetDefaultConfig()
67-
netConf.RequestHeaders.Set("User-Agent",
68-
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
69-
globals.VersionInfo.Application,
70-
globals.VersionInfo.VersionString,
71-
runtime.GOARCH, runtime.GOOS,
72-
runtime.Version(), globals.VersionInfo.Commit))
66+
netConf.RequestHeaders = globals.NewHTTPClientHeader("daemon")
7367
downloader.SetDefaultConfig(netConf)
7468

7569
// register the commands service

cli/globals/globals.go

+11-3
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

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

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

0 commit comments

Comments
 (0)