Skip to content

Commit d5560fc

Browse files
Wrap v2 tools install function inside tools.Download
1 parent 2fa738f commit d5560fc

File tree

1 file changed

+11
-77
lines changed

1 file changed

+11
-77
lines changed

tools/download.go

+11-77
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,18 @@
1616
package tools
1717

1818
import (
19-
"bytes"
2019
"context"
21-
"crypto/sha256"
22-
"encoding/hex"
2320
"encoding/json"
2421
"errors"
25-
"fmt"
26-
"io"
27-
"net/http"
2822
"os"
2923
"os/exec"
3024
"path/filepath"
3125
"runtime"
3226

27+
"github.com/arduino/arduino-create-agent/gen/tools"
28+
"github.com/arduino/arduino-create-agent/utilities"
3329
"github.com/arduino/arduino-create-agent/v2/pkgs"
34-
"github.com/arduino/go-paths-helper"
3530
"github.com/blang/semver"
36-
"github.com/codeclysm/extract/v3"
3731
)
3832

3933
// public vars to allow override in the tests
@@ -99,68 +93,21 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
9993
}
10094
}
10195

102-
// Download the tool
103-
t.logger("Downloading tool " + name + " from " + correctSystem.URL)
104-
resp, err := http.Get(correctSystem.URL)
96+
tool := pkgs.New(t.index, t.directory.String())
97+
_, err = tool.Install(context.Background(), &tools.ToolPayload{Name: correctTool.Name, Version: correctTool.Version, Packager: pack})
10598
if err != nil {
10699
return err
107100
}
108-
defer resp.Body.Close()
109101

110-
// Read the body
111-
body, err = io.ReadAll(resp.Body)
102+
path := filepath.Join(pack, correctTool.Name, correctTool.Version)
103+
safePath, err := utilities.SafeJoin(t.directory.String(), path)
112104
if err != nil {
113105
return err
114106
}
115107

116-
// Checksum
117-
checksum := sha256.Sum256(body)
118-
checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size])
119-
120-
if checkSumString != correctSystem.Checksum {
121-
return errors.New("checksum doesn't match")
122-
}
123-
124-
tempPath := paths.TempDir()
125-
// Create a temporary dir to extract package
126-
if err := tempPath.MkdirAll(); err != nil {
127-
return fmt.Errorf("creating temp dir for extraction: %s", err)
128-
}
129-
tempDir, err := tempPath.MkTempDir("package-")
130-
if err != nil {
131-
return fmt.Errorf("creating temp dir for extraction: %s", err)
132-
}
133-
defer tempDir.RemoveAll()
134-
135-
t.logger("Unpacking tool " + name)
136-
ctx := context.Background()
137-
reader := bytes.NewReader(body)
138-
// Extract into temp directory
139-
if err := extract.Archive(ctx, reader, tempDir.String(), nil); err != nil {
140-
return fmt.Errorf("extracting archive: %s", err)
141-
}
142-
143-
location := t.directory.Join(pack, correctTool.Name, correctTool.Version)
144-
err = location.RemoveAll()
145-
if err != nil {
146-
return err
147-
}
148-
149-
// Check package content and find package root dir
150-
root, err := findPackageRoot(tempDir)
151-
if err != nil {
152-
return fmt.Errorf("searching package root dir: %s", err)
153-
}
154-
155-
if err := root.Rename(location); err != nil {
156-
if err := root.CopyDirTo(location); err != nil {
157-
return fmt.Errorf("moving extracted archive to destination dir: %s", err)
158-
}
159-
}
160-
161108
// if the tool contains a post_install script, run it: it means it is a tool that needs to install drivers
162109
// AFAIK this is only the case for the windows-driver tool
163-
err = t.installDrivers(location.String())
110+
err = t.installDrivers(safePath)
164111
if err != nil {
165112
return err
166113
}
@@ -169,25 +116,12 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
169116
t.logger("Ensure that the files are executable")
170117

171118
// Update the tool map
172-
t.logger("Updating map with location " + location.String())
119+
t.logger("Updating map with location " + safePath)
173120

174-
t.setMapValue(name, location.String())
175-
t.setMapValue(name+"-"+correctTool.Version, location.String())
176-
return t.writeMap()
177-
}
178-
179-
func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
180-
files, err := parent.ReadDir()
181-
if err != nil {
182-
return nil, fmt.Errorf("reading package root dir: %s", err)
183-
}
184-
files.FilterOutPrefix("__MACOSX")
121+
t.setMapValue(name, safePath)
122+
t.setMapValue(name+"-"+version, safePath)
185123

186-
// if there is only one dir, it is the root dir
187-
if len(files) == 1 && files[0].IsDir() {
188-
return files[0], nil
189-
}
190-
return parent, nil
124+
return nil
191125
}
192126

193127
func findTool(pack, name, version string, data pkgs.Index) (pkgs.Tool, pkgs.System) {

0 commit comments

Comments
 (0)