Skip to content

Commit bcf3965

Browse files
committed
Implement cli commander (#17)
arduino-cli packages have been imported in order to directly call the arduino-cli commands packages. This allows to remove arduino-cli daemon as a requisite. Moreover, this completely remove the needs to have arduino-cli installed. Compile is not required at the moment * Remove compile from commander interface * Implement cli commander * Update readme
1 parent 07514c2 commit bcf3965

File tree

5 files changed

+120
-8
lines changed

5 files changed

+120
-8
lines changed

Diff for: README.md

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ iot-cloud-cli is a command line interface that allows to exploit the features of
66

77
This is all you need to use iot-cloud-cli for device **provisioning**:
88
* A client ID and a secret ID, retrievable from the [cloud](https://create.arduino.cc/iot/integrations) by creating a new API key
9-
* arduino-cli in daemon mode, to start it use the command 'arduino-cli daemon'
109
* The folder containing the precompiled provisioning firmwares (`binaries`) needs to be in the same location you run the command from
1110

1211
This is all you need to use iot-cloud-cli as a **virtual device**:

Diff for: arduino/cli/commander.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/arduino/arduino-cli/cli/instance"
11+
"github.com/arduino/arduino-cli/commands/board"
12+
"github.com/arduino/arduino-cli/commands/upload"
13+
"github.com/arduino/arduino-cli/configuration"
14+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
15+
"github.com/arduino/iot-cloud-cli/arduino"
16+
"github.com/sirupsen/logrus"
17+
)
18+
19+
type commander struct {
20+
*rpc.Instance
21+
}
22+
23+
// NewCommander instantiates and returns a new arduino-cli commander that allows to
24+
// programmatically call arduino-cli commands.
25+
// It directly imports the golang packages of the arduino-cli.
26+
func NewCommander() (arduino.Commander, error) {
27+
// Discard arduino-cli log messages
28+
logrus.SetOutput(ioutil.Discard)
29+
// Initialize arduino-cli configuration
30+
configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args))
31+
// Create arduino-cli instance, needed to execute arduino-cli commands
32+
inst, err := instance.CreateInstance()
33+
if err != nil {
34+
err = fmt.Errorf("%s: %w", "creating arduino-cli instance", err)
35+
return nil, err
36+
}
37+
38+
cmd := &commander{inst}
39+
return cmd, nil
40+
}
41+
42+
// BoardList executes the 'arduino-cli board list' command
43+
// and returns its result.
44+
func (c *commander) BoardList() ([]*rpc.DetectedPort, error) {
45+
ports, err := board.List(c.GetId())
46+
if err != nil {
47+
err = fmt.Errorf("%s: %w", "detecting boards", err)
48+
return nil, err
49+
}
50+
return ports, nil
51+
}
52+
53+
// UploadBin executes the 'arduino-cli upload -i' command
54+
// and returns its result.
55+
func (c *commander) UploadBin(fqbn, bin, port string) error {
56+
req := &rpc.UploadRequest{
57+
Instance: c.Instance,
58+
Fqbn: fqbn,
59+
SketchPath: filepath.Dir(bin),
60+
ImportFile: bin,
61+
Port: port,
62+
Verbose: false,
63+
}
64+
65+
if _, err := upload.Upload(context.Background(), req, os.Stdout, os.Stderr); err != nil {
66+
err = fmt.Errorf("%s: %w", "uploading binary", err)
67+
return err
68+
}
69+
70+
return nil
71+
}

Diff for: arduino/commander.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88
// the arduino-cli commands in a programmatic way
99
type Commander interface {
1010
BoardList() ([]*rpc.DetectedPort, error)
11-
UploadBin(fqbn, path, port string) error
12-
Compile() error
11+
UploadBin(fqbn, bin, port string) error
12+
//Compile() error
1313
}

Diff for: command/device/create.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
9-
"github.com/arduino/iot-cloud-cli/arduino/grpc"
9+
"github.com/arduino/iot-cloud-cli/arduino/cli"
1010
"github.com/arduino/iot-cloud-cli/internal/config"
1111
"github.com/arduino/iot-cloud-cli/internal/iot"
1212
)
@@ -31,13 +31,12 @@ type device struct {
3131
// Create command is used to provision a new arduino device
3232
// and to add it to the arduino iot cloud.
3333
func Create(params *CreateParams) (string, error) {
34-
rpcComm, rpcClose, err := grpc.NewClient()
34+
comm, err := cli.NewCommander()
3535
if err != nil {
3636
return "", err
3737
}
38-
defer rpcClose()
3938

40-
ports, err := rpcComm.BoardList()
39+
ports, err := comm.BoardList()
4140
if err != nil {
4241
return "", err
4342
}
@@ -63,7 +62,7 @@ func Create(params *CreateParams) (string, error) {
6362
}
6463

6564
prov := &provision{
66-
Commander: rpcComm,
65+
Commander: comm,
6766
Client: iotClient,
6867
dev: dev,
6968
id: devID}

0 commit comments

Comments
 (0)