Skip to content

Commit abe4926

Browse files
committed
Add device list command (#21)
This PR introduces the device list command, useful to retrieve devices of arduino iot cloud. To list devices, no flags or arguments are needed, the command is simply: $ iot-cloud-cli device list maybe worth doing in the future: list command could accept a list of IDs that should be retrieved from the cloud edit: a new show command might be better commits log: * Add device list command * Add comments * Update readme
1 parent 9ad000c commit abe4926

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ When provisioning a device, you can optionally specify the port to which the dev
3232
Use this command to provision a device:
3333
3434
`$ iot-cloud-cli device create --name <deviceName> --port <port> --fqbn <deviceFqbn>`
35+
36+
## Device commands
37+
38+
Devices currently present on Arduino IoT Cloud can be retrieved by using this command:
39+
`$ iot-cloud-cli device list`

cli/device/device.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func NewCommand() *cobra.Command {
1212
}
1313

1414
deviceCommand.AddCommand(initCreateCommand())
15+
deviceCommand.AddCommand(initListCommand())
1516

1617
return deviceCommand
1718
}

cli/device/list.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package device
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/arduino/arduino-cli/cli/feedback"
7+
"github.com/arduino/arduino-cli/table"
8+
"github.com/arduino/iot-cloud-cli/command/device"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func initListCommand() *cobra.Command {
13+
listCommand := &cobra.Command{
14+
Use: "list",
15+
Short: "List devices",
16+
Long: "List devices on Arduino IoT Cloud",
17+
RunE: runListCommand,
18+
}
19+
return listCommand
20+
}
21+
22+
func runListCommand(cmd *cobra.Command, args []string) error {
23+
fmt.Println("Listing devices")
24+
25+
devs, err := device.List()
26+
if err != nil {
27+
return err
28+
}
29+
30+
feedback.PrintResult(result{devs})
31+
32+
return nil
33+
}
34+
35+
type result struct {
36+
devices []device.DeviceInfo
37+
}
38+
39+
func (r result) Data() interface{} {
40+
return r.devices
41+
}
42+
43+
func (r result) String() string {
44+
if len(r.devices) == 0 {
45+
return "No devices found."
46+
}
47+
t := table.New()
48+
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber")
49+
for _, device := range r.devices {
50+
t.AddRow(
51+
device.Name,
52+
device.ID,
53+
device.Board,
54+
device.FQBN,
55+
device.Serial,
56+
)
57+
}
58+
return t.Render()
59+
}

command/device/list.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package device
2+
3+
import (
4+
"github.com/arduino/iot-cloud-cli/internal/config"
5+
"github.com/arduino/iot-cloud-cli/internal/iot"
6+
)
7+
8+
// DeviceInfo contains the most interesting
9+
// parameters of an Arduino IoT Cloud device.
10+
type DeviceInfo struct {
11+
Name string
12+
ID string
13+
Board string
14+
Serial string
15+
FQBN string
16+
}
17+
18+
// List command is used to list
19+
// the devices of Arduino IoT Cloud.
20+
func List() ([]DeviceInfo, error) {
21+
conf, err := config.Retrieve()
22+
if err != nil {
23+
return nil, err
24+
}
25+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
foundDevices, err := iotClient.ListDevices()
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
var devices []DeviceInfo
36+
for _, foundDev := range foundDevices {
37+
dev := DeviceInfo{
38+
Name: foundDev.Name,
39+
ID: foundDev.Id,
40+
Board: foundDev.Type,
41+
Serial: foundDev.Serial,
42+
FQBN: foundDev.Fqbn,
43+
}
44+
devices = append(devices, dev)
45+
}
46+
47+
return devices, nil
48+
}

internal/iot/client.go

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
// Client can be used to perform actions on the arduino iot cloud.
1111
type Client interface {
1212
AddDevice(fqbn, name, serial, devType string) (string, error)
13+
ListDevices() ([]iotclient.ArduinoDevicev2, error)
1314
AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
1415
}
1516

@@ -47,6 +48,17 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) {
4748
return dev.Id, nil
4849
}
4950

51+
// ListDevices retrieves and returns a list of all Arduino IoT Cloud devices
52+
// belonging to the user performing the request.
53+
func (cl *client) ListDevices() ([]iotclient.ArduinoDevicev2, error) {
54+
devices, _, err := cl.api.DevicesV2Api.DevicesV2List(cl.ctx, nil)
55+
if err != nil {
56+
err = fmt.Errorf("listing devices: %w", err)
57+
return nil, err
58+
}
59+
return devices, nil
60+
}
61+
5062
// AddCertifcate allows to upload a certificate on arduino iot cloud.
5163
// It returns the certificate parameters populated by the cloud.
5264
func (cl *client) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error) {

0 commit comments

Comments
 (0)