Skip to content

Commit d67fad9

Browse files
authored
Provide package indexes from file paths using "additional-paths" flag (#1093)
* Initial commit * Bind new flag --additional-paths and add new function LoadIndexNoSign * Use absolute file path to package_index.json * Add documentation for additional-paths feature * Update comment for LoadIndexNoSign * Update variable names to pass checks + simplify code * Add new config for additional_paths
1 parent 05ce150 commit d67fad9

File tree

10 files changed

+113
-13
lines changed

10 files changed

+113
-13
lines changed

arduino/cores/packageindex/index.go

+17
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,20 @@ func LoadIndex(jsonIndexFile *paths.Path) (*Index, error) {
312312
}
313313
return &index, nil
314314
}
315+
316+
// LoadIndexNoSign reads a package_index.json from a file and returns the corresponding Index structure.
317+
func LoadIndexNoSign(jsonIndexFile *paths.Path) (*Index, error) {
318+
buff, err := jsonIndexFile.ReadFile()
319+
if err != nil {
320+
return nil, err
321+
}
322+
var index Index
323+
err = json.Unmarshal(buff, &index)
324+
if err != nil {
325+
return nil, err
326+
}
327+
328+
index.IsTrusted = true
329+
330+
return &index, nil
331+
}

cli/cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func createCliCommandTree(cmd *cobra.Command) {
104104
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", "The output format, can be {text|json}.")
105105
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", "The custom config file (if not specified the default will be used).")
106106
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, "Comma-separated list of additional URLs for the Boards Manager.")
107+
cmd.PersistentFlags().StringSlice("additional-paths", []string{}, "Comma-separated list of additional file paths for the Boards Manager.")
107108
configuration.BindFlags(cmd, configuration.Settings)
108109
}
109110

cli/config/validate.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ import (
2121
)
2222

2323
var validMap = map[string]reflect.Kind{
24-
"board_manager.additional_urls": reflect.Slice,
25-
"daemon.port": reflect.String,
26-
"directories.data": reflect.String,
27-
"directories.downloads": reflect.String,
28-
"directories.user": reflect.String,
29-
"library.enable_unsafe_install": reflect.Bool,
30-
"logging.file": reflect.String,
31-
"logging.format": reflect.String,
32-
"logging.level": reflect.String,
33-
"sketch.always_export_binaries": reflect.Bool,
34-
"telemetry.addr": reflect.String,
35-
"telemetry.enabled": reflect.Bool,
24+
"board_manager.additional_urls": reflect.Slice,
25+
"board_manager.additional_paths": reflect.Slice,
26+
"daemon.port": reflect.String,
27+
"directories.data": reflect.String,
28+
"directories.downloads": reflect.String,
29+
"directories.user": reflect.String,
30+
"library.enable_unsafe_install": reflect.Bool,
31+
"logging.file": reflect.String,
32+
"logging.format": reflect.String,
33+
"logging.level": reflect.String,
34+
"sketch.always_export_binaries": reflect.Bool,
35+
"telemetry.addr": reflect.String,
36+
"telemetry.enabled": reflect.Bool,
3637
}
3738

3839
func typeOf(key string) (reflect.Kind, error) {

commands/daemon/testdata/arduino-cli.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ board_manager:
22
additional_urls:
33
- http://foobar.com
44
- http://example.com
5+
additional_paths: []
56

67
daemon:
78
port: "50051"

commands/instances.go

+30
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"net/url"
24+
"os"
2425
"path"
2526

2627
"github.com/arduino/arduino-cli/arduino/builder"
@@ -200,9 +201,29 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
200201
}
201202

202203
indexpath := paths.New(configuration.Settings.GetString("directories.Data"))
204+
205+
for _, x := range configuration.Settings.GetStringSlice("board_manager.additional_paths") {
206+
logrus.Info("JSON PATH: ", x)
207+
208+
pathJSON, _ := paths.New(x).Abs()
209+
210+
if _, err := packageindex.LoadIndexNoSign(pathJSON); err != nil {
211+
return nil, fmt.Errorf("invalid package index in %s: %s", pathJSON, err)
212+
}
213+
214+
fi, _ := os.Stat(x)
215+
downloadCB(&rpc.DownloadProgress{
216+
File: "Updating index: " + pathJSON.Base(),
217+
TotalSize: fi.Size(),
218+
})
219+
downloadCB(&rpc.DownloadProgress{Completed: true})
220+
221+
}
222+
203223
urls := []string{globals.DefaultIndexURL}
204224
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
205225
for _, u := range urls {
226+
logrus.Info("URL: ", u)
206227
URL, err := url.Parse(u)
207228
if err != nil {
208229
logrus.Warnf("unable to parse additional URL: %s", u)
@@ -649,6 +670,15 @@ func createInstance(ctx context.Context, getLibOnly bool) (*createInstanceResult
649670
}
650671
}
651672

673+
for _, x := range configuration.Settings.GetStringSlice("board_manager.additional_paths") {
674+
pathJSON, _ := paths.New(x).Abs()
675+
676+
_, err := res.Pm.LoadPackageIndexFromFile(pathJSON)
677+
if err != nil {
678+
res.PlatformIndexErrors = append(res.PlatformIndexErrors, err.Error())
679+
}
680+
}
681+
652682
if err := res.Pm.LoadHardware(); err != nil {
653683
return res, fmt.Errorf("error loading hardware packages: %s", err)
654684
}

configuration/configuration.go

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func BindFlags(cmd *cobra.Command, settings *viper.Viper) {
7676
settings.BindPFlag("logging.file", cmd.Flag("log-file"))
7777
settings.BindPFlag("logging.format", cmd.Flag("log-format"))
7878
settings.BindPFlag("board_manager.additional_urls", cmd.Flag("additional-urls"))
79+
settings.BindPFlag("board_manager.additional_paths", cmd.Flag("additional-paths"))
7980
}
8081

8182
// getDefaultArduinoDataDir returns the full path to the default arduino folder

configuration/configuration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func TestInit(t *testing.T) {
8989
require.Equal(t, "text", settings.GetString("logging.format"))
9090

9191
require.Empty(t, settings.GetStringSlice("board_manager.additional_urls"))
92+
require.Empty(t, settings.GetStringSlice("board_manager.additional_paths"))
9293

9394
require.NotEmpty(t, settings.GetString("directories.Data"))
9495
require.NotEmpty(t, settings.GetString("directories.Downloads"))

configuration/defaults.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func SetDefaults(settings *viper.Viper) {
3333

3434
// Boards Manager
3535
settings.SetDefault("board_manager.additional_urls", []string{})
36+
settings.SetDefault("board_manager.additional_paths", []string{})
3637

3738
// arduino directories
3839
settings.SetDefault("directories.Data", getDefaultArduinoDataDir())

docs/configuration.md

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- `board_manager`
44
- `additional_urls` - the URLs to any additional Boards Manager package index files needed for your boards platforms.
5+
- `additional_paths` - the absolute file paths to any additional Boards Manager package index files needed for your
6+
boards platforms.
57
- `daemon` - options related to running Arduino CLI as a [gRPC] server.
68
- `port` - TCP port used for gRPC client connections.
79
- `directories` - directories used by Arduino CLI.
@@ -66,6 +68,12 @@ Setting an additional Boards Manager URL using the `ARDUINO_BOARD_MANAGER_ADDITI
6668
$ export ARDUINO_BOARD_MANAGER_ADDITIONAL_URLS=https://downloads.arduino.cc/packages/package_staging_index.json
6769
```
6870

71+
Setting an additional Boards Manager file using the `ARDUINO_BOARD_MANAGER_ADDITIONAL_PATHS` environment variable:
72+
73+
```sh
74+
$ export ARDUINO_BOARD_MANAGER_ADDITIONAL_PATHS=/path/to/your/package_staging_index.json
75+
```
76+
6977
### Configuration file
7078

7179
[`arduino-cli config init`][arduino-cli config init] creates or updates a configuration file with the current
@@ -128,6 +136,21 @@ Doing the same using a TOML format file:
128136
additional_urls = [ "https://downloads.arduino.cc/packages/package_staging_index.json" ]
129137
```
130138

139+
Setting an additional Boards Manager File using a YAML format configuration file:
140+
141+
```yaml
142+
board_manager:
143+
additional_paths:
144+
- /path/to/your/package_staging_index.json
145+
```
146+
147+
Doing the same using a TOML format file:
148+
149+
```toml
150+
[board_manager]
151+
additional_paths = [ "/path/to/your/package_staging_index.json" ]
152+
```
153+
131154
[grpc]: https://grpc.io
132155
[sketchbook directory]: sketch-specification.md#sketchbook
133156
[arduino cli lib install]: commands/arduino-cli_lib_install.md

docs/getting-started.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,24 @@ board_manager:
177177
- https://arduino.esp8266.com/stable/package_esp8266com_index.json
178178
```
179179
180-
From now on, commands supporting custom cores will automatically use the additional URL from the configuration file:
180+
If you have your package indexes locally installed, you can list their file path in the Arduino CLI configuration file.
181+
182+
For example, to add the NRF52832 core, edit the configuration file and change the `board_manager` settings as follows:
183+
184+
```yaml
185+
board_manager:
186+
additional_paths:
187+
- /absolute/path/to/your/package_nrf52832_index.json
188+
```
189+
190+
From now on, commands supporting custom cores will automatically use the additional URL and additional paths from the
191+
configuration file:
181192

182193
```sh
183194
$ arduino-cli core update-index
184195
Updating index: package_index.json downloaded
185196
Updating index: package_esp8266com_index.json downloaded
197+
Updating index: package_nrf52832_index.json
186198
Updating index: package_index.json downloaded
187199
188200
$ arduino-cli core search esp8266
@@ -202,6 +214,18 @@ ID Version Name
202214
esp8266:esp8266 2.5.2 esp8266
203215
```
204216

217+
The same applies to the additional package index file provided by file paths. Use the `--additional-paths` option, that
218+
has to be specified every time and for every command that operates on a 3rd party platform core, for example:
219+
220+
```sh
221+
$ arduino-cli core update-index --additional-paths /absolute/path/to/your/package_esp8266com_index.json
222+
Updating index: package_esp8266com_index.json downloaded
223+
224+
$ arduino-cli core search esp8266 --additional-paths /absolute/path/to/your/package_esp8266com_index.json
225+
ID Version Name
226+
esp8266:esp8266 2.5.2 esp8266
227+
```
228+
205229
## Compile and upload the sketch
206230

207231
To compile the sketch you run the `compile` command, passing the proper FQBN string:

0 commit comments

Comments
 (0)