@@ -5,16 +5,21 @@ package commands
5
5
import (
6
6
"context"
7
7
"fmt"
8
+ "io/ioutil"
8
9
"net/url"
10
+ "path"
9
11
"time"
10
12
13
+ "github.com/arduino/arduino-cli/arduino/cores/packageindex"
14
+
11
15
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
12
16
"github.com/arduino/arduino-cli/arduino/libraries"
13
17
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
14
18
"github.com/arduino/arduino-cli/configs"
15
19
"github.com/arduino/arduino-cli/rpc"
16
20
paths "github.com/arduino/go-paths-helper"
17
21
"github.com/sirupsen/logrus"
22
+ "go.bug.st/downloader"
18
23
)
19
24
20
25
// this map contains all the running Arduino Core Services instances
@@ -128,6 +133,59 @@ func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesMan
128
133
}
129
134
}
130
135
136
+ func UpdateIndex (ctx context.Context , req * rpc.UpdateIndexReq , downloadCB DownloadProgressCB ) (* rpc.UpdateIndexResp , error ) {
137
+ id := req .Instance .Id
138
+ coreInstance , ok := instances [id ]
139
+
140
+ if ! ok {
141
+ return nil , fmt .Errorf ("invalid handle" )
142
+ }
143
+ indexpath := coreInstance .config .IndexesDir ()
144
+ for _ , URL := range coreInstance .config .BoardManagerAdditionalUrls {
145
+ logrus .WithField ("url" , URL ).Print ("Updating index" )
146
+
147
+ tmpFile , err := ioutil .TempFile ("" , "" )
148
+ if err != nil {
149
+ return nil , fmt .Errorf ("Error creating temp file for download" , err )
150
+
151
+ }
152
+ if err := tmpFile .Close (); err != nil {
153
+ return nil , fmt .Errorf ("Error creating temp file for download" , err )
154
+ }
155
+ tmp := paths .New (tmpFile .Name ())
156
+ defer tmp .Remove ()
157
+
158
+ d , err := downloader .Download (tmp .String (), URL .String ())
159
+ if err != nil {
160
+ return nil , fmt .Errorf ("Error downloading index " + URL .String (), err )
161
+ }
162
+ coreIndexPath := indexpath .Join (path .Base (URL .Path ))
163
+ Download (d , "Updating index: " + coreIndexPath .Base (), downloadCB )
164
+ if d .Error () != nil {
165
+ return nil , fmt .Errorf ("Error downloading index " + URL .String (), d .Error ())
166
+ }
167
+
168
+ if _ , err := packageindex .LoadIndex (tmp ); err != nil {
169
+ return nil , fmt .Errorf ("Invalid package index in " + URL .String (), err )
170
+ }
171
+
172
+ if err := indexpath .MkdirAll (); err != nil {
173
+ return nil , fmt .Errorf ("Can't create data directory " + indexpath .String (), err )
174
+ }
175
+
176
+ if err := tmp .CopyTo (coreIndexPath ); err != nil {
177
+ return nil , fmt .Errorf ("Error saving downloaded index " + URL .String (), err )
178
+ }
179
+
180
+ // err := packageindex.UpdateIndex(URL, coreInstance.config.IndexesDir())
181
+ // if err != nil {
182
+ // return nil, fmt.Errorf("cannot create file: %s", err)
183
+ // }
184
+ }
185
+ Rescan (ctx , & rpc.RescanReq {Instance : req .Instance })
186
+ return & rpc.UpdateIndexResp {}, nil
187
+ }
188
+
131
189
func Rescan (ctx context.Context , req * rpc.RescanReq ) (* rpc.RescanResp , error ) {
132
190
id := req .Instance .Id
133
191
coreInstance , ok := instances [id ]
@@ -205,3 +263,27 @@ func createInstance(ctx context.Context, config *configs.Configuration, getLibOn
205
263
}
206
264
return pm , lm , nil
207
265
}
266
+
267
+ func Download (d * downloader.Downloader , label string , downloadCB DownloadProgressCB ) error {
268
+ if d == nil {
269
+ // This signal means that the file is already downloaded
270
+ downloadCB (& rpc.DownloadProgress {
271
+ File : label ,
272
+ Completed : true ,
273
+ })
274
+ return nil
275
+ }
276
+ downloadCB (& rpc.DownloadProgress {
277
+ File : label ,
278
+ Url : d .URL ,
279
+ TotalSize : d .Size (),
280
+ })
281
+ d .RunAndPoll (func (downloaded int64 ) {
282
+ downloadCB (& rpc.DownloadProgress {Downloaded : downloaded })
283
+ }, 250 * time .Millisecond )
284
+ if d .Error () != nil {
285
+ return d .Error ()
286
+ }
287
+ downloadCB (& rpc.DownloadProgress {Completed : true })
288
+ return nil
289
+ }
0 commit comments