@@ -211,14 +211,86 @@ For more information on Arduino CLI's gRPC interface, see the [gRPC interface re
211
211
212
212
Arduino CLI is written in [ Golang] and the code is organized in a way that makes it easy to use it as a library by
213
213
including the modules you need in another Golang application at compile time. Both the first and second pillars rely on
214
- a common Golang API, a set of functions that abstract all the functionalities offered by the Arduino CLI, so that when
215
- we provide a fix or a new feature, they are automatically available to both the command line and gRPC interfaces. The
216
- source modules implementing this API are implemented through the ` commands ` package, and it can be imported in other
217
- Golang programs to embed a full-fledged Arduino CLI. For example, this is how some backend services powering [ Arduino
218
- Cloud] can compile sketches and manage libraries. Just to give you a taste of what it means to embed the Arduino CLI,
219
- here is how to search for a core using the API:
214
+ a common Golang API, based on the gRPC protobuf definitions: a set of functions that abstract all the functionalities
215
+ offered by the Arduino CLI, so that when we provide a fix or a new feature, they are automatically available to both the
216
+ command line and gRPC interfaces. The source modules implementing this API are implemented through the ` commands `
217
+ package, and it can be imported in other Golang programs to embed a full-fledged Arduino CLI. For example, this is how
218
+ some backend services powering [ Arduino Cloud] can compile sketches and manage libraries. Just to give you a taste of
219
+ what it means to embed the Arduino CLI, here is how to search for a core using the API:
220
220
221
- ![ Go library interface screenshot] [ ]
221
+ ``` go
222
+ // This file is part of arduino-cli.
223
+ //
224
+ // Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
225
+ //
226
+ // This software is released under the GNU General Public License version 3,
227
+ // which covers the main part of arduino-cli.
228
+ // The terms of this license can be found at:
229
+ // https://www.gnu.org/licenses/gpl-3.0.en.html
230
+ //
231
+ // You can be released from the requirements of the above licenses by purchasing
232
+ // a commercial license. Buying such a license is mandatory if you want to
233
+ // modify or otherwise use the software for commercial activities involving the
234
+ // Arduino software without disclosing the source code of your own applications.
235
+ // To purchase a commercial license, send an email to [email protected] .
236
+
237
+ package main
238
+
239
+ import (
240
+ " context"
241
+ " fmt"
242
+ " io"
243
+ " log"
244
+
245
+ " github.com/arduino/arduino-cli/commands"
246
+ rpc " github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
247
+ " github.com/sirupsen/logrus"
248
+ )
249
+
250
+ func main () {
251
+ // Create a new ArduinoCoreServer
252
+ srv := commands.NewArduinoCoreServer ()
253
+
254
+ // Disable logging
255
+ logrus.SetOutput (io.Discard )
256
+
257
+ // Create a new instance in the server
258
+ ctx := context.Background ()
259
+ resp , err := srv.Create (ctx, &rpc.CreateRequest {})
260
+ if err != nil {
261
+ log.Fatal (" Error creating instance:" , err)
262
+ }
263
+ instance := resp.GetInstance ()
264
+
265
+ // Defer the destruction of the instance
266
+ defer func () {
267
+ if _ , err := srv.Destroy (ctx, &rpc.DestroyRequest {Instance: instance}); err != nil {
268
+ log.Fatal (" Error destroying instance:" , err)
269
+ }
270
+ fmt.Println (" Instance successfully destroyed" )
271
+ }()
272
+
273
+ // Initialize the instance
274
+ initStream := commands.InitStreamResponseToCallbackFunction (ctx, func (r *rpc.InitResponse ) error {
275
+ fmt.Println (" INIT> " , r)
276
+ return nil
277
+ })
278
+ if err := srv.Init (&rpc.InitRequest {Instance: instance}, initStream); err != nil {
279
+ log.Fatal (" Error during initialization:" , err)
280
+ }
281
+
282
+ // Search for platforms and output the result
283
+ searchResp , err := srv.PlatformSearch (ctx, &rpc.PlatformSearchRequest {Instance: instance})
284
+ if err != nil {
285
+ log.Fatal (" Error searching for platforms:" , err)
286
+ }
287
+ for _ , platformSummary := range searchResp.GetSearchOutput () {
288
+ installed := platformSummary.GetInstalledRelease ()
289
+ meta := platformSummary.GetMetadata ()
290
+ fmt.Printf (" %30s %8s %s \n " , meta.GetId (), installed.GetVersion (), installed.GetName ())
291
+ }
292
+ }
293
+ ```
222
294
223
295
Embedding the Arduino CLI is limited to Golang applications and requires a deep knowledge of its internals. For the
224
296
average use case, the gRPC interface might be a better alternative. Nevertheless, this remains a valid option that we
@@ -250,4 +322,3 @@ tracker] if you’ve got a use case that doesn’t fit one of the three pillars.
250
322
[ commands package ] : https://github.com/arduino/arduino-cli/tree/master/commands
251
323
[ issue tracker ] : https://github.com/arduino/arduino-cli/issues
252
324
[ contextual help screenshot ] : img/CLI_contextual_help_screenshot.png
253
- [ go library interface screenshot ] : img/CLI_Go_library_interface_screenshot.png
0 commit comments