Skip to content

Implement cli commander #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ iot-cloud-cli is a command line interface that allows to exploit the features of

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

This is all you need to use iot-cloud-cli as a **virtual device**:
Expand Down
71 changes: 71 additions & 0 deletions arduino/cli/commander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cli

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/commands/upload"
"github.com/arduino/arduino-cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/iot-cloud-cli/arduino"
"github.com/sirupsen/logrus"
)

type commander struct {
*rpc.Instance
}

// NewCommander instantiates and returns a new arduino-cli commander that allows to
// programmatically call arduino-cli commands.
// It directly imports the golang packages of the arduino-cli.
func NewCommander() (arduino.Commander, error) {
// Discard arduino-cli log messages
logrus.SetOutput(ioutil.Discard)
// Initialize arduino-cli configuration
configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args))
// Create arduino-cli instance, needed to execute arduino-cli commands
inst, err := instance.CreateInstance()
if err != nil {
err = fmt.Errorf("%s: %w", "creating arduino-cli instance", err)
return nil, err
}

cmd := &commander{inst}
return cmd, nil
}

// BoardList executes the 'arduino-cli board list' command
// and returns its result.
func (c *commander) BoardList() ([]*rpc.DetectedPort, error) {
ports, err := board.List(c.GetId())
if err != nil {
err = fmt.Errorf("%s: %w", "detecting boards", err)
return nil, err
}
return ports, nil
}

// UploadBin executes the 'arduino-cli upload -i' command
// and returns its result.
func (c *commander) UploadBin(fqbn, bin, port string) error {
req := &rpc.UploadRequest{
Instance: c.Instance,
Fqbn: fqbn,
SketchPath: filepath.Dir(bin),
ImportFile: bin,
Port: port,
Verbose: false,
}

if _, err := upload.Upload(context.Background(), req, os.Stdout, os.Stderr); err != nil {
err = fmt.Errorf("%s: %w", "uploading binary", err)
return err
}

return nil
}
4 changes: 2 additions & 2 deletions arduino/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
// the arduino-cli commands in a programmatic way
type Commander interface {
BoardList() ([]*rpc.DetectedPort, error)
UploadBin(fqbn, path, port string) error
Compile() error
UploadBin(fqbn, bin, port string) error
//Compile() error
}
9 changes: 4 additions & 5 deletions command/device/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/iot-cloud-cli/arduino/grpc"
"github.com/arduino/iot-cloud-cli/arduino/cli"
"github.com/arduino/iot-cloud-cli/internal/config"
"github.com/arduino/iot-cloud-cli/internal/iot"
)
Expand All @@ -31,13 +31,12 @@ type device struct {
// Create command is used to provision a new arduino device
// and to add it to the arduino iot cloud.
func Create(params *CreateParams) (string, error) {
rpcComm, rpcClose, err := grpc.NewClient()
comm, err := cli.NewCommander()
if err != nil {
return "", err
}
defer rpcClose()

ports, err := rpcComm.BoardList()
ports, err := comm.BoardList()
if err != nil {
return "", err
}
Expand All @@ -63,7 +62,7 @@ func Create(params *CreateParams) (string, error) {
}

prov := &provision{
Commander: rpcComm,
Commander: comm,
Client: iotClient,
dev: dev,
id: devID}
Expand Down
Loading