16
16
package tools
17
17
18
18
import (
19
- "bytes"
20
19
"context"
21
- "crypto/sha256"
22
- "encoding/hex"
23
20
"encoding/json"
24
21
"errors"
25
- "fmt"
26
- "io"
27
- "net/http"
28
22
"os"
29
23
"os/exec"
30
24
"path/filepath"
31
25
"runtime"
32
26
27
+ "github.com/arduino/arduino-create-agent/gen/tools"
28
+ "github.com/arduino/arduino-create-agent/utilities"
33
29
"github.com/arduino/arduino-create-agent/v2/pkgs"
34
- "github.com/arduino/go-paths-helper"
35
30
"github.com/blang/semver"
36
- "github.com/codeclysm/extract/v3"
37
31
)
38
32
39
33
// public vars to allow override in the tests
@@ -99,68 +93,21 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
99
93
}
100
94
}
101
95
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 })
105
98
if err != nil {
106
99
return err
107
100
}
108
- defer resp .Body .Close ()
109
101
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 )
112
104
if err != nil {
113
105
return err
114
106
}
115
107
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
-
161
108
// if the tool contains a post_install script, run it: it means it is a tool that needs to install drivers
162
109
// AFAIK this is only the case for the windows-driver tool
163
- err = t .installDrivers (location . String () )
110
+ err = t .installDrivers (safePath )
164
111
if err != nil {
165
112
return err
166
113
}
@@ -169,25 +116,12 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
169
116
t .logger ("Ensure that the files are executable" )
170
117
171
118
// Update the tool map
172
- t .logger ("Updating map with location " + location . String () )
119
+ t .logger ("Updating map with location " + safePath )
173
120
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 )
185
123
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
191
125
}
192
126
193
127
func findTool (pack , name , version string , data pkgs.Index ) (pkgs.Tool , pkgs.System ) {
0 commit comments