Skip to content

Commit c0f272f

Browse files
committed
Added device show
1 parent 661683f commit c0f272f

File tree

4 files changed

+191
-14
lines changed

4 files changed

+191
-14
lines changed

cli/device/device.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCommand() *cobra.Command {
3131

3232
deviceCommand.AddCommand(initCreateCommand())
3333
deviceCommand.AddCommand(initListCommand())
34+
deviceCommand.AddCommand(initShowCommand())
3435
deviceCommand.AddCommand(initDeleteCommand())
3536
deviceCommand.AddCommand(tag.InitCreateTagsCommand())
3637
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())

cli/device/show.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
"strings"
25+
26+
"github.com/arduino/arduino-cli/cli/errorcodes"
27+
"github.com/arduino/arduino-cli/cli/feedback"
28+
"github.com/arduino/arduino-cli/table"
29+
"github.com/arduino/arduino-cloud-cli/command/device"
30+
"github.com/arduino/arduino-cloud-cli/config"
31+
"github.com/sirupsen/logrus"
32+
"github.com/spf13/cobra"
33+
)
34+
35+
type showFlags struct {
36+
deviceId string
37+
}
38+
39+
func initShowCommand() *cobra.Command {
40+
flags := &showFlags{}
41+
showCommand := &cobra.Command{
42+
Use: "show",
43+
Short: "Show device properties",
44+
Long: "Show device properties on Arduino IoT Cloud",
45+
Run: func(cmd *cobra.Command, args []string) {
46+
if err := runShowCommand(flags); err != nil {
47+
feedback.Errorf("Error during device show: %v", err)
48+
os.Exit(errorcodes.ErrGeneric)
49+
}
50+
},
51+
}
52+
showCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "device ID")
53+
54+
showCommand.MarkFlagRequired("device-id")
55+
56+
return showCommand
57+
}
58+
59+
func runShowCommand(flags *showFlags) error {
60+
logrus.Info("Show device")
61+
62+
cred, err := config.RetrieveCredentials()
63+
if err != nil {
64+
return fmt.Errorf("retrieving credentials: %w", err)
65+
}
66+
67+
dev, _, err := device.Show(context.TODO(), flags.deviceId, cred)
68+
if err != nil {
69+
return err
70+
}
71+
72+
feedback.PrintResult(showResult{dev})
73+
return nil
74+
}
75+
76+
type showResult struct {
77+
device *device.DeviceInfo
78+
}
79+
80+
func (r showResult) Data() interface{} {
81+
return r.device
82+
}
83+
84+
func (r showResult) String() string {
85+
if r.device == nil {
86+
return "No device found."
87+
}
88+
t := table.New()
89+
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Connection type", "Thing", "Tags")
90+
t.AddRow(
91+
r.device.Name,
92+
r.device.ID,
93+
r.device.Board,
94+
r.device.FQBN,
95+
r.device.Serial,
96+
dereferenceString(r.device.Status),
97+
dereferenceString(r.device.ConnectionType),
98+
dereferenceString(r.device.ThingID),
99+
strings.Join(r.device.Tags, ","),
100+
)
101+
return t.Render()
102+
}

command/device/device.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ import (
2525
// DeviceInfo contains the most interesting
2626
// parameters of an Arduino IoT Cloud device.
2727
type DeviceInfo struct {
28-
Name string `json:"name"`
29-
ID string `json:"id"`
30-
Board string `json:"board"`
31-
Serial string `json:"serial_number"`
32-
FQBN string `json:"fqbn"`
33-
Tags []string `json:"tags,omitempty"`
34-
Status *string `json:"status,omitempty"`
28+
Name string `json:"name"`
29+
ID string `json:"id"`
30+
Board string `json:"board"`
31+
Serial string `json:"serial_number"`
32+
FQBN string `json:"fqbn"`
33+
Tags []string `json:"tags,omitempty"`
34+
Status *string `json:"status,omitempty"`
35+
Type string `json:"type,omitempty"`
36+
ConnectionType *string `json:"connection_type,omitempty"`
37+
ThingID *string `json:"thing_id,omitempty"`
3538
}
3639

3740
func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
@@ -42,13 +45,18 @@ func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
4245
}
4346

4447
dev := &DeviceInfo{
45-
Name: device.Name,
46-
ID: device.Id,
47-
Board: device.Type,
48-
Serial: device.Serial,
49-
FQBN: dereferenceString(device.Fqbn),
50-
Tags: tags,
51-
Status: device.DeviceStatus,
48+
Name: device.Name,
49+
ID: device.Id,
50+
Board: device.Type,
51+
Serial: device.Serial,
52+
FQBN: dereferenceString(device.Fqbn),
53+
Tags: tags,
54+
Status: device.DeviceStatus,
55+
Type: device.Type,
56+
ConnectionType: device.ConnectionType,
57+
}
58+
if device.Thing != nil {
59+
dev.ThingID = &device.Thing.Id
5260
}
5361
return dev, nil
5462
}

command/device/show.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"context"
22+
23+
"github.com/arduino/arduino-cloud-cli/config"
24+
"github.com/arduino/arduino-cloud-cli/internal/iot"
25+
"github.com/sirupsen/logrus"
26+
)
27+
28+
type netCredentials struct {
29+
FriendlyName string `json:"friendly_name"`
30+
Required bool `json:"required"`
31+
SecretName string `json:"secret_name"`
32+
Sensitive bool `json:"sensitive"`
33+
}
34+
35+
// List command is used to list
36+
// the devices of Arduino IoT Cloud.
37+
func Show(ctx context.Context, deviceId string, cred *config.Credentials) (*DeviceInfo, []netCredentials, error) {
38+
iotClient, err := iot.NewClient(cred)
39+
if err != nil {
40+
return nil, nil, err
41+
}
42+
43+
logrus.Infof("Show device %s", deviceId)
44+
45+
foundDevice, err := iotClient.DeviceShow(ctx, deviceId)
46+
if err != nil {
47+
return nil, nil, err
48+
}
49+
device, err := getDeviceInfo(foundDevice)
50+
if err != nil {
51+
return nil, nil, err
52+
}
53+
54+
net := []netCredentials{}
55+
if device.ConnectionType != nil {
56+
netCredentialsArray, err := iotClient.DeviceNetworkCredentials(ctx, deviceId, *foundDevice.ConnectionType)
57+
if err != nil {
58+
return nil, net, err
59+
}
60+
for _, netCred := range netCredentialsArray {
61+
net = append(net, netCredentials(netCred))
62+
}
63+
}
64+
65+
return device, nil, nil
66+
}

0 commit comments

Comments
 (0)