From b43c918c50a1825bddcc1f4e1b2216b7faac948f Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 5 Aug 2021 15:19:23 +0200 Subject: [PATCH 1/3] Add device list command --- cli/device/device.go | 1 + cli/device/list.go | 59 ++++++++++++++++++++++++++++++++++++++++++ command/device/list.go | 44 +++++++++++++++++++++++++++++++ internal/iot/client.go | 12 +++++++++ 4 files changed, 116 insertions(+) create mode 100644 cli/device/list.go create mode 100644 command/device/list.go 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..c1977ca1 --- /dev/null +++ b/command/device/list.go @@ -0,0 +1,44 @@ +package device + +import ( + "github.com/arduino/iot-cloud-cli/internal/config" + "github.com/arduino/iot-cloud-cli/internal/iot" +) + +type DeviceInfo struct { + Name string + ID string + Board string + Serial string + FQBN string +} + +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) { From e7f63adc8fffdd8bf53f4ee4043db24c57b40ae4 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 5 Aug 2021 18:11:57 +0200 Subject: [PATCH 2/3] Add comments --- command/device/list.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/command/device/list.go b/command/device/list.go index c1977ca1..739e2a62 100644 --- a/command/device/list.go +++ b/command/device/list.go @@ -5,6 +5,8 @@ import ( "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 @@ -13,6 +15,8 @@ type DeviceInfo struct { 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 { From 06f09aa51ab4df7cd0df9c745dd6044997846e7f Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 5 Aug 2021 18:20:36 +0200 Subject: [PATCH 3/3] Update readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) 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`