diff --git a/README.md b/README.md index 500b9b96..84d1be2e 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,8 @@ When provisioning a device, you can optionally specify the port to which the dev Use this command to provision a device: `$ iot-cloud-cli device create --name --port --fqbn ` + +## Device commands + +Devices currently present on Arduino IoT Cloud can be retrieved by using this command: +`$ iot-cloud-cli device list` diff --git a/cli/device/device.go b/cli/device/device.go index a9088dab..b149bea5 100644 --- a/cli/device/device.go +++ b/cli/device/device.go @@ -12,6 +12,7 @@ func NewCommand() *cobra.Command { } deviceCommand.AddCommand(initCreateCommand()) + deviceCommand.AddCommand(initListCommand()) return deviceCommand } diff --git a/cli/device/list.go b/cli/device/list.go new file mode 100644 index 00000000..e7f86540 --- /dev/null +++ b/cli/device/list.go @@ -0,0 +1,59 @@ +package device + +import ( + "fmt" + + "github.com/arduino/arduino-cli/cli/feedback" + "github.com/arduino/arduino-cli/table" + "github.com/arduino/iot-cloud-cli/command/device" + "github.com/spf13/cobra" +) + +func initListCommand() *cobra.Command { + listCommand := &cobra.Command{ + Use: "list", + Short: "List devices", + Long: "List devices on Arduino IoT Cloud", + RunE: runListCommand, + } + return listCommand +} + +func runListCommand(cmd *cobra.Command, args []string) error { + fmt.Println("Listing devices") + + devs, err := device.List() + if err != nil { + return err + } + + feedback.PrintResult(result{devs}) + + return nil +} + +type result struct { + devices []device.DeviceInfo +} + +func (r result) Data() interface{} { + return r.devices +} + +func (r result) String() string { + if len(r.devices) == 0 { + return "No devices found." + } + t := table.New() + t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber") + for _, device := range r.devices { + t.AddRow( + device.Name, + device.ID, + device.Board, + device.FQBN, + device.Serial, + ) + } + return t.Render() +} diff --git a/command/device/list.go b/command/device/list.go new file mode 100644 index 00000000..739e2a62 --- /dev/null +++ b/command/device/list.go @@ -0,0 +1,48 @@ +package device + +import ( + "github.com/arduino/iot-cloud-cli/internal/config" + "github.com/arduino/iot-cloud-cli/internal/iot" +) + +// DeviceInfo contains the most interesting +// parameters of an Arduino IoT Cloud device. +type DeviceInfo struct { + Name string + ID string + Board string + Serial string + FQBN string +} + +// List command is used to list +// the devices of Arduino IoT Cloud. +func List() ([]DeviceInfo, error) { + conf, err := config.Retrieve() + if err != nil { + return nil, err + } + iotClient, err := iot.NewClient(conf.Client, conf.Secret) + if err != nil { + return nil, err + } + + foundDevices, err := iotClient.ListDevices() + if err != nil { + return nil, err + } + + var devices []DeviceInfo + for _, foundDev := range foundDevices { + dev := DeviceInfo{ + Name: foundDev.Name, + ID: foundDev.Id, + Board: foundDev.Type, + Serial: foundDev.Serial, + FQBN: foundDev.Fqbn, + } + devices = append(devices, dev) + } + + return devices, nil +} diff --git a/internal/iot/client.go b/internal/iot/client.go index fe68ccb0..3fd584c4 100644 --- a/internal/iot/client.go +++ b/internal/iot/client.go @@ -10,6 +10,7 @@ import ( // Client can be used to perform actions on the arduino iot cloud. type Client interface { AddDevice(fqbn, name, serial, devType string) (string, error) + ListDevices() ([]iotclient.ArduinoDevicev2, error) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error) } @@ -47,6 +48,17 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) { return dev.Id, nil } +// ListDevices retrieves and returns a list of all Arduino IoT Cloud devices +// belonging to the user performing the request. +func (cl *client) ListDevices() ([]iotclient.ArduinoDevicev2, error) { + devices, _, err := cl.api.DevicesV2Api.DevicesV2List(cl.ctx, nil) + if err != nil { + err = fmt.Errorf("listing devices: %w", err) + return nil, err + } + return devices, nil +} + // AddCertifcate allows to upload a certificate on arduino iot cloud. // It returns the certificate parameters populated by the cloud. func (cl *client) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error) {