Skip to content

Check if a signed URL is specified and use it download tools #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Download tools defaulting to the replace behaviour
  • Loading branch information
MatteoPologruto committed Jun 19, 2024
commit ed8eac622f17d003ad6246f6cbf547d49e76f729
47 changes: 4 additions & 43 deletions tools/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package tools

import (
"context"
"encoding/json"
"errors"
"os"
"os/exec"
Expand All @@ -36,17 +35,6 @@ var (
Arch = runtime.GOARCH
)

func pathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}

// Download will parse the index at the indexURL for the tool to download.
// It will extract it in a folder in .arduino-create, and it will update the
// Installed map.
Expand All @@ -62,44 +50,17 @@ func pathExists(path string) bool {
// If version is not "latest" and behaviour is "replace", it will download the
// version again. If instead behaviour is "keep" it will not download the version
// if it already exists.
//
// At the moment the value of behaviour is ignored.
func (t *Tools) Download(pack, name, version, behaviour string) error {

body, err := t.index.Read()
if err != nil {
return err
}

var data pkgs.Index
json.Unmarshal(body, &data)

// Find the tool by name
correctTool, correctSystem := findTool(pack, name, version, data)

if correctTool.Name == "" || correctSystem.URL == "" {
t.logger("We couldn't find a tool with the name " + name + " and version " + version + " packaged by " + pack)
return nil
}

key := correctTool.Name + "-" + correctTool.Version

// Check if it already exists
if behaviour == "keep" {
location, ok := t.getMapValue(key)
if ok && pathExists(location) {
// overwrite the default tool with this one
t.setMapValue(correctTool.Name, location)
t.logger("The tool is already present on the system")
return t.writeMap()
}
}

tool := pkgs.New(t.index, t.directory.String())
_, err = tool.Install(context.Background(), &tools.ToolPayload{Name: correctTool.Name, Version: correctTool.Version, Packager: pack})
_, err := tool.Install(context.Background(), &tools.ToolPayload{Name: name, Version: version, Packager: pack})
if err != nil {
return err
}

path := filepath.Join(pack, correctTool.Name, correctTool.Version)
path := filepath.Join(pack, name, version)
safePath, err := utilities.SafeJoin(t.directory.String(), path)
if err != nil {
return err
Expand Down
12 changes: 0 additions & 12 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,6 @@ func (t *Tools) getMapValue(key string) (string, bool) {
return value, ok
}

// writeMap() writes installed map to the json file "installed.json"
func (t *Tools) writeMap() error {
t.mutex.RLock()
defer t.mutex.RUnlock()
b, err := json.Marshal(t.installed)
if err != nil {
return err
}
filePath := t.directory.Join("installed.json")
return filePath.WriteFile(b)
}

// readMap() reads the installed map from json file "installed.json"
func (t *Tools) readMap() error {
t.mutex.Lock()
Expand Down
51 changes: 37 additions & 14 deletions v2/pkgs/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/arduino/arduino-create-agent/gen/tools"
"github.com/arduino/arduino-create-agent/index"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/blang/semver"
"github.com/codeclysm/extract/v3"
)

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

for _, packager := range index.Packages {
if packager.Name != payload.Packager {
continue
}

for _, tool := range packager.Tools {
if tool.Name == payload.Name &&
tool.Version == payload.Version {

sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)

return t.install(ctx, path, sys.URL, sys.Checksum)
}
}
correctSystem, found := findTool(payload.Packager, payload.Name, payload.Version, index)
if found {
return t.install(ctx, path, correctSystem.URL, correctSystem.Checksum)
}

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

return os.WriteFile(installedFile, data, 0644)
}

func findTool(pack, name, version string, data Index) (System, bool) {
var correctTool Tool
correctTool.Version = "0.0"
found := false

for _, p := range data.Packages {
if p.Name != pack {
continue
}
for _, t := range p.Tools {
if version != "latest" {
if t.Name == name && t.Version == version {
correctTool = t
found = true
}
} else {
// Find latest
v1, _ := semver.Make(t.Version)
v2, _ := semver.Make(correctTool.Version)
if t.Name == name && v1.Compare(v2) > 0 {
correctTool = t
found = true
}
}
}
}

// Find the url based on system
correctSystem := correctTool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)

return correctSystem, found
}