Skip to content

Commit 5f571ef

Browse files
author
Roberto Sora
committed
clean inside cobra commands for empty http.Header struct and inject headers build from VersionInfo struct in cli InitInstance func
1 parent ddd6bd1 commit 5f571ef

21 files changed

+34
-41
lines changed

cli/board/attach.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package board
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -50,7 +49,7 @@ var attachFlags struct {
5049
}
5150

5251
func runAttachCommand(cmd *cobra.Command, args []string) {
53-
instance := cli.CreateInstance(http.Header{})
52+
instance := cli.CreateInstance()
5453
var path string
5554
if len(args) > 0 {
5655
path = args[1]

cli/board/details.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package board
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524

2625
"github.com/arduino/arduino-cli/cli"
@@ -44,7 +43,7 @@ func initDetailsCommand() *cobra.Command {
4443
}
4544

4645
func runDetailsCommand(cmd *cobra.Command, args []string) {
47-
instance := cli.CreateInstance(http.Header{})
46+
instance := cli.CreateInstance()
4847

4948
res, err := board.Details(context.Background(), &rpc.BoardDetailsReq{
5049
Instance: instance,

cli/board/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package board
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524
"sort"
2625
"time"
@@ -54,7 +53,7 @@ var listFlags struct {
5453

5554
// runListCommand detects and lists the connected arduino boards
5655
func runListCommand(cmd *cobra.Command, args []string) {
57-
instance := cli.CreateInstance(http.Header{})
56+
instance := cli.CreateInstance()
5857

5958
if timeout, err := time.ParseDuration(listFlags.timeout); err != nil {
6059
formatter.PrintError(err, "Invalid timeout.")

cli/board/listall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package board
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524
"sort"
2625

@@ -50,7 +49,7 @@ func initListAllCommand() *cobra.Command {
5049

5150
// runListAllCommand list all installed boards
5251
func runListAllCommand(cmd *cobra.Command, args []string) {
53-
instance := cli.CreateInstance(http.Header{})
52+
instance := cli.CreateInstance()
5453

5554
list, err := board.ListAll(context.Background(), &rpc.BoardListAllReq{
5655
Instance: instance,

cli/cli.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ package cli
2020
import (
2121
"context"
2222
"errors"
23+
"fmt"
2324
"net/http"
2425
"os"
2526
"path/filepath"
27+
"runtime"
2628

2729
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2830
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -85,9 +87,14 @@ func packageManagerInitReq() *rpc.InitReq {
8587
return &rpc.InitReq{Configuration: conf}
8688
}
8789

88-
func InitInstance(downloaderHeaders http.Header) *rpc.InitResp {
90+
func InitInstance() *rpc.InitResp {
8991
logrus.Info("Initializing package manager")
9092
req := packageManagerInitReq()
93+
94+
userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s/Build:%s", version.GetApplication(),
95+
version.GetVersion(), runtime.GOARCH, runtime.GOOS, runtime.Version(), version.GetCommit(), version.GetBuildDate())
96+
downloaderHeaders := http.Header{"User-Agent": []string{userAgentValue}}
97+
9198
resp, err := commands.Init(context.Background(), req, OutputProgressBar(), OutputTaskProgress(), downloaderHeaders)
9299
if err != nil {
93100
formatter.PrintError(err, "Error initializing package manager")
@@ -112,8 +119,8 @@ func InitInstance(downloaderHeaders http.Header) *rpc.InitResp {
112119
}
113120

114121
// CreateInstance creates and return an instance of the Arduino Core engine
115-
func CreateInstance(downloaderHeaders http.Header) *rpc.Instance {
116-
resp := InitInstance(downloaderHeaders)
122+
func CreateInstance() *rpc.Instance {
123+
resp := InitInstance()
117124
if resp.GetPlatformsIndexErrors() != nil {
118125
for _, err := range resp.GetPlatformsIndexErrors() {
119126
formatter.PrintError(errors.New(err), "Error loading index")
@@ -126,14 +133,14 @@ func CreateInstance(downloaderHeaders http.Header) *rpc.Instance {
126133

127134
// CreateInstaceIgnorePlatformIndexErrors creates and return an instance of the
128135
// Arduino Core Engine, but won't stop on platforms index loading errors.
129-
func CreateInstaceIgnorePlatformIndexErrors(downloaderHeaders http.Header) *rpc.Instance {
130-
return InitInstance(downloaderHeaders).GetInstance()
136+
func CreateInstaceIgnorePlatformIndexErrors() *rpc.Instance {
137+
return InitInstance().GetInstance()
131138
}
132139

133140
// InitPackageAndLibraryManager initializes the PackageManager and the
134141
// LibaryManager with the default configuration. (DEPRECATED)
135-
func InitPackageAndLibraryManager(downloaderHeaders http.Header) (*packagemanager.PackageManager, *librariesmanager.LibrariesManager) {
136-
resp := InitInstance(downloaderHeaders)
142+
func InitPackageAndLibraryManager() (*packagemanager.PackageManager, *librariesmanager.LibrariesManager) {
143+
resp := InitInstance()
137144
return commands.GetPackageManager(resp), commands.GetLibraryManager(resp)
138145
}
139146

cli/compile/compile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package compile
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -91,7 +90,7 @@ var flags struct {
9190
}
9291

9392
func run(cmd *cobra.Command, args []string) {
94-
instance := cli.CreateInstance(http.Header{})
93+
instance := cli.CreateInstance()
9594

9695
var path *paths.Path
9796
if len(args) > 0 {

cli/core/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func initDownloadCommand() *cobra.Command {
4545
}
4646

4747
func runDownloadCommand(cmd *cobra.Command, args []string) {
48-
instance := cli.CreateInstance(http.Header{})
48+
instance := cli.CreateInstance()
4949
logrus.Info("Executing `arduino core download`")
5050

5151
platformsRefs := parsePlatformReferenceArgs(args)

cli/core/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func initInstallCommand() *cobra.Command {
4646
}
4747

4848
func runInstallCommand(cmd *cobra.Command, args []string) {
49-
instance := cli.CreateInstance(http.Header{})
49+
instance := cli.CreateInstance()
5050
logrus.Info("Executing `arduino core install`")
5151

5252
platformsRefs := parsePlatformReferenceArgs(args)

cli/core/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package core
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524
"sort"
2625

@@ -51,7 +50,7 @@ var listFlags struct {
5150
}
5251

5352
func runListCommand(cmd *cobra.Command, args []string) {
54-
instance := cli.CreateInstance(http.Header{})
53+
instance := cli.CreateInstance()
5554
logrus.Info("Executing `arduino core list`")
5655

5756
resp, err := core.PlatformList(context.Background(), &rpc.PlatformListReq{

cli/core/search.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package core
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524
"sort"
2625
"strings"
@@ -47,7 +46,7 @@ func initSearchCommand() *cobra.Command {
4746
}
4847

4948
func runSearchCommand(cmd *cobra.Command, args []string) {
50-
instance := cli.CreateInstance(http.Header{})
49+
instance := cli.CreateInstance()
5150
logrus.Info("Executing `arduino core search`")
5251

5352
arguments := strings.ToLower(strings.Join(args, " "))

cli/core/uninstall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package core
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -43,7 +42,7 @@ func initUninstallCommand() *cobra.Command {
4342
}
4443

4544
func runUninstallCommand(cmd *cobra.Command, args []string) {
46-
instance := cli.CreateInstance(http.Header{})
45+
instance := cli.CreateInstance()
4746
logrus.Info("Executing `arduino core uninstall`")
4847

4948
platformsRefs := parsePlatformReferenceArgs(args)

cli/core/update_index.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package core
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -43,7 +42,7 @@ func initUpdateIndexCommand() *cobra.Command {
4342
}
4443

4544
func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
46-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
45+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
4746
logrus.Info("Executing `arduino core update-index`")
4847

4948
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexReq{

cli/core/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func initUpgradeCommand() *cobra.Command {
4646
}
4747

4848
func runUpgradeCommand(cmd *cobra.Command, args []string) {
49-
instance := cli.CreateInstance(http.Header{})
49+
instance := cli.CreateInstance()
5050
logrus.Info("Executing `arduino core upgrade`")
5151

5252
platformsRefs := parsePlatformReferenceArgs(args)

cli/lib/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func initDownloadCommand() *cobra.Command {
4545
}
4646

4747
func runDownloadCommand(cmd *cobra.Command, args []string) {
48-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
48+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
4949
pairs, err := librariesindex.ParseArgs(args)
5050
if err != nil {
5151
formatter.PrintError(err, "Arguments error")

cli/lib/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func initInstallCommand() *cobra.Command {
4545
}
4646

4747
func runInstallCommand(cmd *cobra.Command, args []string) {
48-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
48+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
4949
refs, err := librariesindex.ParseArgs(args)
5050
if err != nil {
5151
formatter.PrintError(err, "Arguments error")

cli/lib/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package lib
1919

2020
import (
2121
"fmt"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -52,7 +51,7 @@ var listFlags struct {
5251
}
5352

5453
func runListCommand(cmd *cobra.Command, args []string) {
55-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
54+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
5655
logrus.Info("Listing")
5756

5857
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{

cli/lib/search.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package lib
2020
import (
2121
"context"
2222
"fmt"
23-
"net/http"
2423
"os"
2524
"sort"
2625
"strings"
@@ -52,7 +51,7 @@ var searchFlags struct {
5251
}
5352

5453
func runSearchCommand(cmd *cobra.Command, args []string) {
55-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
54+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
5655
logrus.Info("Executing `arduino lib search`")
5756
searchResp, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{
5857
Instance: instance,

cli/lib/uninstall.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package lib
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
@@ -46,7 +45,7 @@ func initUninstallCommand() *cobra.Command {
4645
func runUninstallCommand(cmd *cobra.Command, args []string) {
4746
logrus.Info("Executing `arduino lib uninstall`")
4847

49-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
48+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
5049
libRefs, err := librariesindex.ParseArgs(args)
5150
if err != nil {
5251
formatter.PrintError(err, "Arguments error")

cli/lib/update_index.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package lib
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -37,7 +36,7 @@ func initUpdateIndexCommand() *cobra.Command {
3736
Example: " " + cli.VersionInfo.Application + " lib update-index",
3837
Args: cobra.NoArgs,
3938
Run: func(cmd *cobra.Command, args []string) {
40-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
39+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
4140
err := commands.UpdateLibrariesIndex(context.Background(), &rpc.UpdateLibrariesIndexReq{
4241
Instance: instance,
4342
}, cli.OutputProgressBar())

cli/lib/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func initUpgradeCommand() *cobra.Command {
4444
}
4545

4646
func runUpgradeCommand(cmd *cobra.Command, args []string) {
47-
instance := cli.CreateInstaceIgnorePlatformIndexErrors(http.Header{})
47+
instance := cli.CreateInstaceIgnorePlatformIndexErrors()
4848

4949
err := lib.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{
5050
Instance: instance,

cli/upload/upload.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package upload
1919

2020
import (
2121
"context"
22-
"net/http"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/cli"
@@ -67,7 +66,7 @@ var flags struct {
6766
}
6867

6968
func run(command *cobra.Command, args []string) {
70-
instance := cli.CreateInstance(http.Header{})
69+
instance := cli.CreateInstance()
7170

7271
var path *paths.Path
7372
if len(args) > 0 {

0 commit comments

Comments
 (0)