Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e0a04c

Browse filesBrowse files
Download tools defaulting to the replace behaviour
1 parent 4feffda commit 8e0a04c
Copy full SHA for 8e0a04c

File tree

Expand file treeCollapse file tree

3 files changed

+41
-69
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+41
-69
lines changed

‎tools/download.go

Copy file name to clipboardExpand all lines: tools/download.go
+4-43Lines changed: 4 additions & 43 deletions
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

Copy file name to clipboardExpand all lines: tools/tools.go
-12Lines changed: 0 additions & 12 deletions
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

Copy file name to clipboardExpand all lines: v2/pkgs/tools.go
+37-14Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/arduino/arduino-create-agent/gen/tools"
3434
"github.com/arduino/arduino-create-agent/index"
3535
"github.com/arduino/arduino-create-agent/utilities"
36+
"github.com/blang/semver"
3637
"github.com/codeclysm/extract/v3"
3738
)
3839

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

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)
183173
}
184174

185175
return nil, tools.MakeNotFound(
@@ -295,3 +285,36 @@ func writeInstalled(folder, path string) error {
295285

296286
return os.WriteFile(installedFile, data, 0644)
297287
}
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.