@@ -49,8 +49,9 @@ import (
49
49
func installTool (pm * packagemanager.PackageManager , tool * cores.ToolRelease , downloadCB rpc.DownloadProgressCB , taskCB rpc.TaskProgressCB ) error {
50
50
pme , release := pm .NewExplorer ()
51
51
defer release ()
52
+
52
53
taskCB (& rpc.TaskProgress {Name : tr ("Downloading missing tool %s" , tool )})
53
- if err := pme .DownloadToolRelease (tool , nil , downloadCB ); err != nil {
54
+ if err := pme .DownloadToolRelease (tool , downloadCB ); err != nil {
54
55
return fmt .Errorf (tr ("downloading %[1]s tool: %[2]s" ), tool , err )
55
56
}
56
57
taskCB (& rpc.TaskProgress {Completed : true })
@@ -62,16 +63,16 @@ func installTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, dow
62
63
63
64
// Create a new Instance ready to be initialized, supporting directories are also created.
64
65
func (s * arduinoCoreServerImpl ) Create (ctx context.Context , req * rpc.CreateRequest ) (* rpc.CreateResponse , error ) {
65
- var userAgent [] string
66
+ var userAgent string
66
67
if md , ok := metadata .FromIncomingContext (ctx ); ok {
67
- userAgent = md .Get ("user-agent" )
68
+ userAgent = strings . Join ( md .Get ("user-agent" ), " " )
68
69
}
69
- if len ( userAgent ) == 0 {
70
- userAgent = [] string { "gRPCClientUnknown/0.0.0" }
70
+ if userAgent == "" {
71
+ userAgent = "gRPCClientUnknown/0.0.0"
71
72
}
72
73
73
74
// Setup downloads directory
74
- downloadsDir := configuration .DownloadsDir (configuration . Settings )
75
+ downloadsDir := configuration .DownloadsDir (s . settings )
75
76
if downloadsDir .NotExist () {
76
77
err := downloadsDir .MkdirAll ()
77
78
if err != nil {
@@ -80,16 +81,20 @@ func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateReque
80
81
}
81
82
82
83
// Setup data directory
83
- dataDir := configuration .DataDir (configuration . Settings )
84
- packagesDir := configuration .PackagesDir (configuration . Settings )
84
+ dataDir := configuration .DataDir (s . settings )
85
+ packagesDir := configuration .PackagesDir (s . settings )
85
86
if packagesDir .NotExist () {
86
87
err := packagesDir .MkdirAll ()
87
88
if err != nil {
88
89
return nil , & cmderrors.PermissionDeniedError {Message : tr ("Failed to create data directory" ), Cause : err }
89
90
}
90
91
}
91
92
92
- inst , err := instances .Create (dataDir , packagesDir , downloadsDir , userAgent ... )
93
+ config , err := s .settings .DownloaderConfig ()
94
+ if err != nil {
95
+ return nil , err
96
+ }
97
+ inst , err := instances .Create (dataDir , packagesDir , downloadsDir , userAgent , config )
93
98
if err != nil {
94
99
return nil , err
95
100
}
@@ -108,6 +113,8 @@ func InitStreamResponseToCallbackFunction(ctx context.Context, cb func(r *rpc.In
108
113
// Failures don't stop the loading process, in case of loading failure the Platform or library
109
114
// is simply skipped and an error gRPC status is sent to responseCallback.
110
115
func (s * arduinoCoreServerImpl ) Init (req * rpc.InitRequest , stream rpc.ArduinoCoreService_InitServer ) error {
116
+ ctx := stream .Context ()
117
+
111
118
instance := req .GetInstance ()
112
119
if ! instances .IsValid (instance ) {
113
120
return & cmderrors.InvalidInstanceError {}
@@ -170,7 +177,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
170
177
defaultIndexURL , _ := utils .URLParse (globals .DefaultIndexURL )
171
178
allPackageIndexUrls := []* url.URL {defaultIndexURL }
172
179
if profile == nil {
173
- for _ , u := range configuration . Settings .GetStringSlice ("board_manager.additional_urls" ) {
180
+ for _ , u := range s . settings .GetStringSlice ("board_manager.additional_urls" ) {
174
181
URL , err := utils .URLParse (u )
175
182
if err != nil {
176
183
e := & cmderrors.InitFailedError {
@@ -185,7 +192,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
185
192
}
186
193
}
187
194
188
- if err := firstUpdate (context . Background () , s , req .GetInstance (), downloadCallback , allPackageIndexUrls ); err != nil {
195
+ if err := firstUpdate (ctx , s , req .GetInstance (), configuration . DataDir ( s . settings ), downloadCallback , allPackageIndexUrls ); err != nil {
189
196
e := & cmderrors.InitFailedError {
190
197
Code : codes .InvalidArgument ,
191
198
Cause : err ,
@@ -238,15 +245,13 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
238
245
239
246
// Load Platforms
240
247
if profile == nil {
241
- for _ , err := range pmb .LoadHardware () {
248
+ for _ , err := range pmb .LoadHardware (s . settings ) {
242
249
s := & cmderrors.PlatformLoadingError {Cause : err }
243
250
responseError (s .GRPCStatus ())
244
251
}
245
252
} else {
246
253
// Load platforms from profile
247
- errs := pmb .LoadHardwareForProfile (
248
- profile , true , downloadCallback , taskCallback ,
249
- )
254
+ errs := pmb .LoadHardwareForProfile (profile , true , downloadCallback , taskCallback , s .settings )
250
255
for _ , err := range errs {
251
256
s := & cmderrors.PlatformLoadingError {Cause : err }
252
257
responseError (s .GRPCStatus ())
@@ -344,7 +349,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
344
349
345
350
if profile == nil {
346
351
// Add directories of libraries bundled with IDE
347
- if bundledLibsDir := configuration .IDEBuiltinLibrariesDir (configuration . Settings ); bundledLibsDir != nil {
352
+ if bundledLibsDir := configuration .IDEBuiltinLibrariesDir (s . settings ); bundledLibsDir != nil {
348
353
lmb .AddLibrariesDir (librariesmanager.LibrariesDir {
349
354
Path : bundledLibsDir ,
350
355
Location : libraries .IDEBuiltIn ,
@@ -353,14 +358,14 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
353
358
354
359
// Add libraries directory from config file
355
360
lmb .AddLibrariesDir (librariesmanager.LibrariesDir {
356
- Path : configuration .LibrariesDir (configuration . Settings ),
361
+ Path : configuration .LibrariesDir (s . settings ),
357
362
Location : libraries .User ,
358
363
})
359
364
} else {
360
365
// Load libraries required for profile
361
366
for _ , libraryRef := range profile .Libraries {
362
367
uid := libraryRef .InternalUniqueIdentifier ()
363
- libRoot := configuration .ProfilesCacheDir (configuration . Settings ).Join (uid )
368
+ libRoot := configuration .ProfilesCacheDir (s . settings ).Join (uid )
364
369
libDir := libRoot .Join (libraryRef .Library )
365
370
366
371
if ! libDir .IsDir () {
@@ -373,7 +378,14 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
373
378
responseError (err .GRPCStatus ())
374
379
continue
375
380
}
376
- if err := libRelease .Resource .Download (pme .DownloadDir , nil , libRelease .String (), downloadCallback , "" ); err != nil {
381
+ config , err := s .settings .DownloaderConfig ()
382
+ if err != nil {
383
+ taskCallback (& rpc.TaskProgress {Name : tr ("Error downloading library %s" , libraryRef )})
384
+ e := & cmderrors.FailedLibraryInstallError {Cause : err }
385
+ responseError (e .GRPCStatus ())
386
+ continue
387
+ }
388
+ if err := libRelease .Resource .Download (pme .DownloadDir , config , libRelease .String (), downloadCallback , "" ); err != nil {
377
389
taskCallback (& rpc.TaskProgress {Name : tr ("Error downloading library %s" , libraryRef )})
378
390
e := & cmderrors.FailedLibraryInstallError {Cause : err }
379
391
responseError (e .GRPCStatus ())
@@ -409,7 +421,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
409
421
// Refreshes the locale used, this will change the
410
422
// language of the CLI if the locale is different
411
423
// after started.
412
- i18n .Init (configuration . Settings .GetString ("locale" ))
424
+ i18n .Init (s . settings .GetString ("locale" ))
413
425
414
426
return nil
415
427
}
@@ -487,7 +499,11 @@ func (s *arduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
487
499
// Perform index update
488
500
// TODO: pass context
489
501
// ctx := stream.Context()
490
- if err := globals .LibrariesIndexResource .Download (indexDir , downloadCB ); err != nil {
502
+ config , err := s .settings .DownloaderConfig ()
503
+ if err != nil {
504
+ return err
505
+ }
506
+ if err := globals .LibrariesIndexResource .Download (indexDir , downloadCB , config ); err != nil {
491
507
resultCB (rpc .IndexUpdateReport_STATUS_FAILED )
492
508
return err
493
509
}
@@ -532,11 +548,11 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
532
548
Message : & rpc.UpdateIndexResponse_DownloadProgress {DownloadProgress : p },
533
549
})
534
550
}
535
- indexpath := configuration .DataDir (configuration . Settings )
551
+ indexpath := configuration .DataDir (s . settings )
536
552
537
553
urls := []string {globals .DefaultIndexURL }
538
554
if ! req .GetIgnoreCustomPackageIndexes () {
539
- urls = append (urls , configuration . Settings .GetStringSlice ("board_manager.additional_urls" )... )
555
+ urls = append (urls , s . settings .GetStringSlice ("board_manager.additional_urls" )... )
540
556
}
541
557
542
558
failed := false
@@ -593,11 +609,18 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
593
609
}
594
610
}
595
611
612
+ config , err := s .settings .DownloaderConfig ()
613
+ if err != nil {
614
+ downloadCB .Start (u , tr ("Downloading index: %s" , filepath .Base (URL .Path )))
615
+ downloadCB .End (false , tr ("Invalid network configuration: %s" , err ))
616
+ failed = true
617
+ }
618
+
596
619
if strings .HasSuffix (URL .Host , "arduino.cc" ) && strings .HasSuffix (URL .Path , ".json" ) {
597
620
indexResource .SignatureURL , _ = url .Parse (u ) // should not fail because we already parsed it
598
621
indexResource .SignatureURL .Path += ".sig"
599
622
}
600
- if err := indexResource .Download (indexpath , downloadCB ); err != nil {
623
+ if err := indexResource .Download (indexpath , downloadCB , config ); err != nil {
601
624
failed = true
602
625
result .UpdatedIndexes = append (result .GetUpdatedIndexes (), report (URL , rpc .IndexUpdateReport_STATUS_FAILED ))
603
626
} else {
@@ -615,10 +638,8 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
615
638
616
639
// firstUpdate downloads libraries and packages indexes if they don't exist.
617
640
// This ideally is only executed the first time the CLI is run.
618
- func firstUpdate (ctx context.Context , srv rpc.ArduinoCoreServiceServer , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
619
- // Gets the data directory to verify if library_index.json and package_index.json exist
620
- dataDir := configuration .DataDir (configuration .Settings )
621
- libraryIndex := dataDir .Join ("library_index.json" )
641
+ func firstUpdate (ctx context.Context , srv rpc.ArduinoCoreServiceServer , instance * rpc.Instance , indexDir * paths.Path , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
642
+ libraryIndex := indexDir .Join ("library_index.json" )
622
643
623
644
if libraryIndex .NotExist () {
624
645
// The library_index.json file doesn't exists, that means the CLI is run for the first time
@@ -640,7 +661,7 @@ func firstUpdate(ctx context.Context, srv rpc.ArduinoCoreServiceServer, instance
640
661
Message : tr ("Error downloading index '%s'" , URL ),
641
662
Cause : & cmderrors.InvalidURLError {}}
642
663
}
643
- packageIndexFile := dataDir .Join (packageIndexFileName )
664
+ packageIndexFile := indexDir .Join (packageIndexFileName )
644
665
if packageIndexFile .NotExist () {
645
666
// The index file doesn't exists, that means the CLI is run for the first time,
646
667
// or the 3rd party package index URL has just been added. Similarly to the
0 commit comments