Skip to content

Commit afe92ea

Browse files
Find the correct tool and system when version=latest is specified
1 parent f7f5a19 commit afe92ea

File tree

3 files changed

+27
-54
lines changed

3 files changed

+27
-54
lines changed

tools/download.go

+2-39
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ import (
2626
"github.com/arduino/arduino-create-agent/gen/tools"
2727
"github.com/arduino/arduino-create-agent/utilities"
2828
"github.com/arduino/arduino-create-agent/v2/pkgs"
29-
"github.com/blang/semver"
30-
)
31-
32-
// public vars to allow override in the tests
33-
var (
34-
OS = runtime.GOOS
35-
Arch = runtime.GOARCH
3629
)
3730

3831
// Download will parse the index at the indexURL for the tool to download.
@@ -85,42 +78,12 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
8578
return nil
8679
}
8780

88-
func findTool(pack, name, version string, data pkgs.Index) (pkgs.Tool, pkgs.System) {
89-
var correctTool pkgs.Tool
90-
correctTool.Version = "0.0"
91-
92-
for _, p := range data.Packages {
93-
if p.Name != pack {
94-
continue
95-
}
96-
for _, t := range p.Tools {
97-
if version != "latest" {
98-
if t.Name == name && t.Version == version {
99-
correctTool = t
100-
}
101-
} else {
102-
// Find latest
103-
v1, _ := semver.Make(t.Version)
104-
v2, _ := semver.Make(correctTool.Version)
105-
if t.Name == name && v1.Compare(v2) > 0 {
106-
correctTool = t
107-
}
108-
}
109-
}
110-
}
111-
112-
// Find the url based on system
113-
correctSystem := correctTool.GetFlavourCompatibleWith(OS, Arch)
114-
115-
return correctTool, correctSystem
116-
}
117-
11881
func (t *Tools) installDrivers(location string) error {
11982
OkPressed := 6
12083
extension := ".bat"
12184
// add .\ to force locality
12285
preamble := ".\\"
123-
if OS != "windows" {
86+
if runtime.GOOS != "windows" {
12487
extension = ".sh"
12588
// add ./ to force locality
12689
preamble = "./"
@@ -132,7 +95,7 @@ func (t *Tools) installDrivers(location string) error {
13295
os.Chdir(location)
13396
t.logger(preamble + "post_install" + extension)
13497
oscmd := exec.Command(preamble + "post_install" + extension)
135-
if OS != "linux" {
98+
if runtime.GOOS != "linux" {
13699
// spawning a shell could be the only way to let the user type his password
137100
TellCommandNotToSpawnShell(oscmd)
138101
}

tools/download_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func TestDownloadCorrectPlatform(t *testing.T) {
4242
{"linux", "arm", "arm-linux-gnueabihf"},
4343
}
4444
defer func() {
45-
OS = runtime.GOOS // restore `runtime.OS`
46-
Arch = runtime.GOARCH // restore `runtime.ARCH`
45+
pkgs.OS = runtime.GOOS // restore `runtime.OS`
46+
pkgs.Arch = runtime.GOARCH // restore `runtime.ARCH`
4747
}()
4848
testIndex := paths.New("testdata", "test_tool_index.json")
4949
buf, err := testIndex.ReadFile()
@@ -54,10 +54,11 @@ func TestDownloadCorrectPlatform(t *testing.T) {
5454
require.NoError(t, err)
5555
for _, tc := range testCases {
5656
t.Run(tc.hostOS+tc.hostArch, func(t *testing.T) {
57-
OS = tc.hostOS // override `runtime.OS` for testing purposes
58-
Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
57+
pkgs.OS = tc.hostOS // override `runtime.OS` for testing purposes
58+
pkgs.Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
5959
// Find the tool by name
60-
correctTool, correctSystem := findTool("arduino-test", "arduino-fwuploader", "2.2.2", data)
60+
correctTool, correctSystem, found := pkgs.FindTool("arduino-test", "arduino-fwuploader", "2.2.2", data)
61+
require.True(t, found)
6162
require.NotNil(t, correctTool)
6263
require.NotNil(t, correctSystem)
6364
require.Equal(t, correctTool.Name, "arduino-fwuploader")
@@ -78,8 +79,8 @@ func TestDownloadFallbackPlatform(t *testing.T) {
7879
{"windows", "amd64", "i686-mingw32"},
7980
}
8081
defer func() {
81-
OS = runtime.GOOS // restore `runtime.OS`
82-
Arch = runtime.GOARCH // restore `runtime.ARCH`
82+
pkgs.OS = runtime.GOOS // restore `runtime.OS`
83+
pkgs.Arch = runtime.GOARCH // restore `runtime.ARCH`
8384
}()
8485
testIndex := paths.New("testdata", "test_tool_index.json")
8586
buf, err := testIndex.ReadFile()
@@ -90,10 +91,11 @@ func TestDownloadFallbackPlatform(t *testing.T) {
9091
require.NoError(t, err)
9192
for _, tc := range testCases {
9293
t.Run(tc.hostOS+tc.hostArch, func(t *testing.T) {
93-
OS = tc.hostOS // override `runtime.OS` for testing purposes
94-
Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
94+
pkgs.OS = tc.hostOS // override `runtime.OS` for testing purposes
95+
pkgs.Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
9596
// Find the tool by name
96-
correctTool, correctSystem := findTool("arduino-test", "arduino-fwuploader", "2.2.0", data)
97+
correctTool, correctSystem, found := pkgs.FindTool("arduino-test", "arduino-fwuploader", "2.2.0", data)
98+
require.True(t, found)
9799
require.NotNil(t, correctTool)
98100
require.NotNil(t, correctSystem)
99101
require.Equal(t, correctTool.Name, "arduino-fwuploader")
@@ -145,7 +147,7 @@ func TestDownload(t *testing.T) {
145147
if filePath.IsDir() {
146148
require.DirExists(t, filePath.String())
147149
} else {
148-
if OS == "windows" {
150+
if runtime.GOOS == "windows" {
149151
require.FileExists(t, filePath.String()+".exe")
150152
} else {
151153
require.FileExists(t, filePath.String())

v2/pkgs/tools.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ import (
3737
"github.com/codeclysm/extract/v3"
3838
)
3939

40+
// public vars to allow override in the tests
41+
var (
42+
OS = runtime.GOOS
43+
Arch = runtime.GOARCH
44+
)
45+
4046
// Tools is a client that implements github.com/arduino/arduino-create-agent/gen/tools.Service interface.
4147
// It saves tools in a specified folder with this structure: packager/name/version
4248
// For example:
@@ -167,7 +173,8 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
167173
var index Index
168174
json.Unmarshal(body, &index)
169175

170-
correctSystem, found := findTool(payload.Packager, payload.Name, payload.Version, index)
176+
correctTool, correctSystem, found := FindTool(payload.Packager, payload.Name, payload.Version, index)
177+
path = filepath.Join(payload.Packager, correctTool.Name, correctTool.Version)
171178
if found {
172179
return t.install(ctx, path, correctSystem.URL, correctSystem.Checksum)
173180
}
@@ -289,7 +296,8 @@ func writeInstalled(folder, path string) error {
289296
return os.WriteFile(installedFile, data, 0644)
290297
}
291298

292-
func findTool(pack, name, version string, data Index) (System, bool) {
299+
// FindTool searches the index for the correct tool and system that match the specified tool name and version
300+
func FindTool(pack, name, version string, data Index) (Tool, System, bool) {
293301
var correctTool Tool
294302
correctTool.Version = "0.0"
295303
found := false
@@ -317,7 +325,7 @@ func findTool(pack, name, version string, data Index) (System, bool) {
317325
}
318326

319327
// Find the url based on system
320-
correctSystem := correctTool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
328+
correctSystem := correctTool.GetFlavourCompatibleWith(OS, Arch)
321329

322-
return correctSystem, found
330+
return correctTool, correctSystem, found
323331
}

0 commit comments

Comments
 (0)