Skip to content

Commit

Permalink
Removed globals.HTTPClientHeader global (arduino#438)
Browse files Browse the repository at this point in the history
To increase thread-safery and allow adding extra headers if needed.
  • Loading branch information
cmaglie authored and Roberto Sora committed Oct 7, 2019
1 parent 39530c0 commit 63da3d8
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
Version: platformRef.Version,
}
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar(),
globals.HTTPClientHeader)
globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error downloading %s: %v", args[i], err)
os.Exit(errorcodes.ErrNetwork)
Expand Down
2 changes: 1 addition & 1 deletion cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
Version: platformRef.Version,
}
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(),
output.TaskProgress(), globals.HTTPClientHeader)
output.TaskProgress(), globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error during install: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
2 changes: 1 addition & 1 deletion cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Architecture: platformRef.Architecture,
}

_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
if err == core.ErrAlreadyLatest {
feedback.Printf("Platform %s is already at the latest version", platformRef)
} else if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions cli/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ var (
Debug bool
// OutputFormat can be "text" or "json"
OutputFormat string
// HTTPClientHeader is the object that will be propagated to configure the clients inside the downloaders
HTTPClientHeader = getHTTPClientHeader()
// VersionInfo contains all info injected during build
VersionInfo = version.NewInfo(filepath.Base(os.Args[0]))
// Config FIXMEDOC
Expand All @@ -46,9 +44,9 @@ var (
LogLevel string
)

func getHTTPClientHeader() http.Header {
// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders
func NewHTTPClientHeader() http.Header {
userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s", VersionInfo.Application,
VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), VersionInfo.Commit)
downloaderHeaders := http.Header{"User-Agent": []string{userAgentValue}}
return downloaderHeaders
return http.Header{"User-Agent": []string{userAgentValue}}
}
2 changes: 1 addition & 1 deletion cli/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initInstance() *rpc.InitResp {
logrus.Info("Initializing package manager")
req := packageManagerInitReq()

resp, err := commands.Init(context.Background(), req, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
resp, err := commands.Init(context.Background(), req, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error initializing package manager: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
Version: library.Version,
}
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar(),
globals.HTTPClientHeader)
globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error downloading %s: %v", library, err)
os.Exit(errorcodes.ErrNetwork)
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
Version: library.Version,
}
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(),
output.TaskProgress(), globals.HTTPClientHeader)
output.TaskProgress(), globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error installing %s: %v", library, err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstaceIgnorePlatformIndexErrors()

if len(args) == 0 {
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error upgrading libraries: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
} else {
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.HTTPClientHeader)
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
if err != nil {
feedback.Errorf("Error upgrading libraries: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
2 changes: 1 addition & 1 deletion commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
func apiByVidPid(url string) ([]*rpc.BoardListItem, error) {
retVal := []*rpc.BoardListItem{}
req, _ := http.NewRequest("GET", url, nil)
req.Header = globals.HTTPClientHeader
req.Header = globals.NewHTTPClientHeader()
req.Header.Set("Content-Type", "application/json")

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

0 comments on commit 63da3d8

Please sign in to comment.