Skip to content

Commit f61f4d1

Browse files
Download tools defaulting to the replace behaviour
1 parent 4264332 commit f61f4d1

File tree

3 files changed

+41
-69
lines changed

3 files changed

+41
-69
lines changed

tools/download.go

+4-43
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package tools
1717

1818
import (
1919
"context"
20-
"encoding/json"
2120
"errors"
2221
"os"
2322
"os/exec"
@@ -36,17 +35,6 @@ var (
3635
Arch = runtime.GOARCH
3736
)
3837

39-
func pathExists(path string) bool {
40-
_, err := os.Stat(path)
41-
if err == nil {
42-
return true
43-
}
44-
if os.IsNotExist(err) {
45-
return false
46-
}
47-
return true
48-
}
49-
5038
// Download will parse the index at the indexURL for the tool to download.
5139
// It will extract it in a folder in .arduino-create, and it will update the
5240
// Installed map.
@@ -62,44 +50,17 @@ func pathExists(path string) bool {
6250
// If version is not "latest" and behaviour is "replace", it will download the
6351
// version again. If instead behaviour is "keep" it will not download the version
6452
// if it already exists.
53+
//
54+
// At the moment the value of behaviour is ignored.
6555
func (t *Tools) Download(pack, name, version, behaviour string) error {
6656

67-
body, err := t.index.Read()
68-
if err != nil {
69-
return err
70-
}
71-
72-
var data pkgs.Index
73-
json.Unmarshal(body, &data)
74-
75-
// Find the tool by name
76-
correctTool, correctSystem := findTool(pack, name, version, data)
77-
78-
if correctTool.Name == "" || correctSystem.URL == "" {
79-
t.logger("We couldn't find a tool with the name " + name + " and version " + version + " packaged by " + pack)
80-
return nil
81-
}
82-
83-
key := correctTool.Name + "-" + correctTool.Version
84-
85-
// Check if it already exists
86-
if behaviour == "keep" {
87-
location, ok := t.getMapValue(key)
88-
if ok && pathExists(location) {
89-
// overwrite the default tool with this one
90-
t.setMapValue(correctTool.Name, location)
91-
t.logger("The tool is already present on the system")
92-
return t.writeMap()
93-
}
94-
}
95-
9657
tool := pkgs.New(t.index, t.directory.String())
97-
_, err = tool.Install(context.Background(), &tools.ToolPayload{Name: correctTool.Name, Version: correctTool.Version, Packager: pack})
58+
_, err := tool.Install(context.Background(), &tools.ToolPayload{Name: name, Version: version, Packager: pack})
9859
if err != nil {
9960
return err
10061
}
10162

102-
path := filepath.Join(pack, correctTool.Name, correctTool.Version)
63+
path := filepath.Join(pack, name, version)
10364
safePath, err := utilities.SafeJoin(t.directory.String(), path)
10465
if err != nil {
10566
return err

tools/tools.go

-12
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,6 @@ func (t *Tools) getMapValue(key string) (string, bool) {
7878
return value, ok
7979
}
8080

81-
// writeMap() writes installed map to the json file "installed.json"
82-
func (t *Tools) writeMap() error {
83-
t.mutex.RLock()
84-
defer t.mutex.RUnlock()
85-
b, err := json.Marshal(t.installed)
86-
if err != nil {
87-
return err
88-
}
89-
filePath := t.directory.Join("installed.json")
90-
return filePath.WriteFile(b)
91-
}
92-
9381
// readMap() reads the installed map from json file "installed.json"
9482
func (t *Tools) readMap() error {
9583
t.mutex.Lock()

v2/pkgs/tools.go

+37-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/arduino/arduino-create-agent/index"
3535
"github.com/arduino/arduino-create-agent/utilities"
3636
"github.com/arduino/go-paths-helper"
37+
"github.com/blang/semver"
3738
"github.com/codeclysm/extract/v3"
3839
)
3940

@@ -167,20 +168,9 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
167168
var index Index
168169
json.Unmarshal(body, &index)
169170

170-
for _, packager := range index.Packages {
171-
if packager.Name != payload.Packager {
172-
continue
173-
}
174-
175-
for _, tool := range packager.Tools {
176-
if tool.Name == payload.Name &&
177-
tool.Version == payload.Version {
178-
179-
sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
180-
181-
return t.install(ctx, path, sys.URL, sys.Checksum)
182-
}
183-
}
171+
correctSystem, found := findTool(payload.Packager, payload.Name, payload.Version, index)
172+
if found {
173+
return t.install(ctx, path, correctSystem.URL, correctSystem.Checksum)
184174
}
185175

186176
return nil, tools.MakeNotFound(
@@ -325,3 +315,36 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
325315
}
326316
return parent, nil
327317
}
318+
319+
func findTool(pack, name, version string, data Index) (System, bool) {
320+
var correctTool Tool
321+
correctTool.Version = "0.0"
322+
found := false
323+
324+
for _, p := range data.Packages {
325+
if p.Name != pack {
326+
continue
327+
}
328+
for _, t := range p.Tools {
329+
if version != "latest" {
330+
if t.Name == name && t.Version == version {
331+
correctTool = t
332+
found = true
333+
}
334+
} else {
335+
// Find latest
336+
v1, _ := semver.Make(t.Version)
337+
v2, _ := semver.Make(correctTool.Version)
338+
if t.Name == name && v1.Compare(v2) > 0 {
339+
correctTool = t
340+
found = true
341+
}
342+
}
343+
}
344+
}
345+
346+
// Find the url based on system
347+
correctSystem := correctTool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
348+
349+
return correctSystem, found
350+
}

0 commit comments

Comments
 (0)