Skip to content

Commit 7d74d1e

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Show tags in list commands (#57)
Add tags information in 'thing list' and 'device list' commands
1 parent 499ea9f commit 7d74d1e

File tree

9 files changed

+129
-25
lines changed

9 files changed

+129
-25
lines changed

cli/device/list.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package device
1919

2020
import (
2121
"os"
22+
"strings"
2223

2324
"github.com/arduino/arduino-cli/cli/errorcodes"
2425
"github.com/arduino/arduino-cli/cli/feedback"
@@ -75,14 +76,15 @@ func (r listResult) String() string {
7576
return "No devices found."
7677
}
7778
t := table.New()
78-
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber")
79+
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Tags")
7980
for _, device := range r.devices {
8081
t.AddRow(
8182
device.Name,
8283
device.ID,
8384
device.Board,
8485
device.FQBN,
8586
device.Serial,
87+
strings.Join(device.Tags, ","),
8688
)
8789
}
8890
return t.Render()

cli/thing/list.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ func (r result) String() string {
9393
}
9494
t := table.New()
9595

96-
h := []interface{}{"Name", "ID", "Device"}
96+
h := []interface{}{"Name", "ID", "Device", "Tags"}
9797
if listFlags.variables {
9898
h = append(h, "Variables")
9999
}
100100
t.SetHeader(h...)
101101

102102
for _, thing := range r.things {
103103
r := []interface{}{thing.Name, thing.ID, thing.DeviceID}
104+
r = append(r, strings.Join(thing.Tags, ","))
104105
if listFlags.variables {
105106
r = append(r, strings.Join(thing.Variables, ", "))
106107
}

command/device/device.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 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+
"github.com/arduino/arduino-cloud-cli/command/tag"
22+
iotclient "github.com/arduino/iot-client-go"
23+
)
24+
25+
// DeviceInfo contains the most interesting
26+
// parameters of an Arduino IoT Cloud device.
27+
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+
}
35+
36+
func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
37+
// Retrieve device tags
38+
tags, err := tag.TagsInfo(device.Tags)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
dev := &DeviceInfo{
44+
Name: device.Name,
45+
ID: device.Id,
46+
Board: device.Type,
47+
Serial: device.Serial,
48+
FQBN: device.Fqbn,
49+
Tags: tags,
50+
}
51+
return dev, nil
52+
}

command/device/list.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,12 @@
1818
package device
1919

2020
import (
21+
"fmt"
22+
2123
"github.com/arduino/arduino-cloud-cli/internal/config"
2224
"github.com/arduino/arduino-cloud-cli/internal/iot"
2325
)
2426

25-
// DeviceInfo contains the most interesting
26-
// parameters of an Arduino IoT Cloud device.
27-
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-
}
34-
3527
// ListParams contains the optional parameters needed
3628
// to filter the devices to be listed.
3729
type ListParams struct {
@@ -57,14 +49,11 @@ func List(params *ListParams) ([]DeviceInfo, error) {
5749

5850
var devices []DeviceInfo
5951
for _, foundDev := range foundDevices {
60-
dev := DeviceInfo{
61-
Name: foundDev.Name,
62-
ID: foundDev.Id,
63-
Board: foundDev.Type,
64-
Serial: foundDev.Serial,
65-
FQBN: foundDev.Fqbn,
52+
dev, err := getDeviceInfo(&foundDev)
53+
if err != nil {
54+
return nil, fmt.Errorf("parsing device %s from cloud: %w", foundDev.Id, err)
6655
}
67-
devices = append(devices, dev)
56+
devices = append(devices, *dev)
6857
}
6958

7059
return devices, nil

command/tag/tag.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 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 tag
19+
20+
import "fmt"
21+
22+
// TagsInfo transforms tags into user-readable strings
23+
// An error is returned if a tag value is not a string
24+
func TagsInfo(tags map[string]interface{}) ([]string, error) {
25+
var str []string
26+
for key, value := range tags {
27+
valStr, ok := value.(string)
28+
if !ok {
29+
return nil, fmt.Errorf("value of tag `%s` should be of type `string` but is of type `%T`", key, value)
30+
}
31+
str = append(str, key+"="+valStr)
32+
}
33+
return str, nil
34+
}

command/thing/clone.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func Clone(params *CloneParams) (*ThingInfo, error) {
5454
return nil, err
5555
}
5656

57-
return getThingInfo(newThing), nil
57+
t, err := getThingInfo(newThing)
58+
if err != nil {
59+
return nil, fmt.Errorf("parsing thing %s from cloud: %w", newThing.Id, err)
60+
}
61+
return t, nil
5862
}
5963

6064
func retrieve(client iot.Client, thingID string) (*iotclient.ThingCreate, error) {

command/thing/create.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package thing
1919

2020
import (
2121
"errors"
22+
"fmt"
2223

2324
"github.com/arduino/arduino-cloud-cli/internal/config"
2425
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -62,5 +63,9 @@ func Create(params *CreateParams) (*ThingInfo, error) {
6263
return nil, err
6364
}
6465

65-
return getThingInfo(newThing), nil
66+
t, err := getThingInfo(newThing)
67+
if err != nil {
68+
return nil, fmt.Errorf("parsing the new thing %s from cloud: %w", newThing.Id, err)
69+
}
70+
return t, nil
6671
}

command/thing/list.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package thing
1919

2020
import (
21+
"fmt"
22+
2123
"github.com/arduino/arduino-cloud-cli/internal/config"
2224
"github.com/arduino/arduino-cloud-cli/internal/iot"
2325
)
@@ -50,7 +52,10 @@ func List(params *ListParams) ([]ThingInfo, error) {
5052

5153
var things []ThingInfo
5254
for _, foundThing := range foundThings {
53-
info := getThingInfo(&foundThing)
55+
info, err := getThingInfo(&foundThing)
56+
if err != nil {
57+
return nil, fmt.Errorf("parsing thing %s from cloud: %w", foundThing.Id, err)
58+
}
5459
things = append(things, *info)
5560
}
5661

command/thing/thing.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package thing
1919

20-
import iotclient "github.com/arduino/iot-client-go"
20+
import (
21+
"github.com/arduino/arduino-cloud-cli/command/tag"
22+
iotclient "github.com/arduino/iot-client-go"
23+
)
2124

2225
// ThingInfo contains the main parameters of
2326
// an Arduino IoT Cloud thing.
@@ -26,18 +29,27 @@ type ThingInfo struct {
2629
ID string `json:"id"`
2730
DeviceID string `json:"device-id"`
2831
Variables []string `json:"variables"`
32+
Tags []string `json:"tags,omitempty"`
2933
}
3034

31-
func getThingInfo(thing *iotclient.ArduinoThing) *ThingInfo {
35+
func getThingInfo(thing *iotclient.ArduinoThing) (*ThingInfo, error) {
36+
// Process thing variables
3237
var vars []string
3338
for _, p := range thing.Properties {
3439
vars = append(vars, p.Name)
3540
}
41+
// Process thing tags
42+
tags, err := tag.TagsInfo(thing.Tags)
43+
if err != nil {
44+
return nil, err
45+
}
46+
3647
info := &ThingInfo{
3748
Name: thing.Name,
3849
ID: thing.Id,
3950
DeviceID: thing.DeviceId,
4051
Variables: vars,
52+
Tags: tags,
4153
}
42-
return info
54+
return info, nil
4355
}

0 commit comments

Comments
 (0)