@@ -33,6 +33,7 @@ import (
33
33
"github.com/arduino/arduino-create-agent/gen/tools"
34
34
"github.com/arduino/arduino-create-agent/index"
35
35
"github.com/arduino/arduino-create-agent/utilities"
36
+ "github.com/blang/semver"
36
37
"github.com/codeclysm/extract/v3"
37
38
)
38
39
@@ -166,20 +167,9 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
166
167
var index Index
167
168
json .Unmarshal (body , & index )
168
169
169
- for _ , packager := range index .Packages {
170
- if packager .Name != payload .Packager {
171
- continue
172
- }
173
-
174
- for _ , tool := range packager .Tools {
175
- if tool .Name == payload .Name &&
176
- tool .Version == payload .Version {
177
-
178
- sys := tool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
179
-
180
- return t .install (ctx , path , sys .URL , sys .Checksum )
181
- }
182
- }
170
+ correctSystem , found := findTool (payload .Packager , payload .Name , payload .Version , index )
171
+ if found {
172
+ return t .install (ctx , path , correctSystem .URL , correctSystem .Checksum )
183
173
}
184
174
185
175
return nil , tools .MakeNotFound (
@@ -295,3 +285,36 @@ func writeInstalled(folder, path string) error {
295
285
296
286
return os .WriteFile (installedFile , data , 0644 )
297
287
}
288
+
289
+ func findTool (pack , name , version string , data Index ) (System , bool ) {
290
+ var correctTool Tool
291
+ correctTool .Version = "0.0"
292
+ found := false
293
+
294
+ for _ , p := range data .Packages {
295
+ if p .Name != pack {
296
+ continue
297
+ }
298
+ for _ , t := range p .Tools {
299
+ if version != "latest" {
300
+ if t .Name == name && t .Version == version {
301
+ correctTool = t
302
+ found = true
303
+ }
304
+ } else {
305
+ // Find latest
306
+ v1 , _ := semver .Make (t .Version )
307
+ v2 , _ := semver .Make (correctTool .Version )
308
+ if t .Name == name && v1 .Compare (v2 ) > 0 {
309
+ correctTool = t
310
+ found = true
311
+ }
312
+ }
313
+ }
314
+ }
315
+
316
+ // Find the url based on system
317
+ correctSystem := correctTool .GetFlavourCompatibleWith (runtime .GOOS , runtime .GOARCH )
318
+
319
+ return correctSystem , found
320
+ }
0 commit comments