Skip to content

Commit 09a5df8

Browse files
committed
Show thing tags in list command
1 parent 0bae4ec commit 09a5df8

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

cli/thing/list.go

Lines changed: 2 additions & 1 deletion
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/thing/clone.go

Lines changed: 5 additions & 1 deletion
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.Thing, error) {

command/thing/create.go

Lines changed: 6 additions & 1 deletion
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("getting the new thing %s from cloud: %w", newThing.Id, err)
69+
}
70+
return t, nil
6671
}

command/thing/list.go

Lines changed: 6 additions & 1 deletion
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("getting thing %s from cloud: %w", foundThing.Id, err)
58+
}
5459
things = append(things, *info)
5560
}
5661

command/thing/thing.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package thing
1919

20-
import iotclient "github.com/arduino/iot-client-go"
20+
import (
21+
"fmt"
22+
23+
iotclient "github.com/arduino/iot-client-go"
24+
)
2125

2226
// ThingInfo contains the main parameters of
2327
// an Arduino IoT Cloud thing.
@@ -26,18 +30,31 @@ type ThingInfo struct {
2630
ID string `json:"id"`
2731
DeviceID string `json:"device-id"`
2832
Variables []string `json:"variables"`
33+
Tags []string `json:"tags,omitempty"`
2934
}
3035

31-
func getThingInfo(thing *iotclient.ArduinoThing) *ThingInfo {
36+
func getThingInfo(thing *iotclient.ArduinoThing) (*ThingInfo, error) {
37+
// Retrieve thing variables
3238
var vars []string
3339
for _, p := range thing.Properties {
3440
vars = append(vars, p.Name)
3541
}
42+
// Retrieve thing tags
43+
var tags []string
44+
for key, value := range thing.Tags {
45+
if valStr, ok := value.(string); ok {
46+
tags = append(tags, key+": "+valStr)
47+
} else {
48+
return nil, fmt.Errorf("value of tag `%s` should be of type `string` but is of type `%T`", key, value)
49+
}
50+
}
51+
3652
info := &ThingInfo{
3753
Name: thing.Name,
3854
ID: thing.Id,
3955
DeviceID: thing.DeviceId,
4056
Variables: vars,
57+
Tags: tags,
4158
}
42-
return info
59+
return info, nil
4360
}

command/thing/thing_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package thing
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
iotclient "github.com/arduino/iot-client-go"
8+
)
9+
10+
func TestGetThingInfo(t *testing.T) {
11+
thingTagsValid := &iotclient.ArduinoThing{Tags: map[string]interface{}{
12+
"location": "rome",
13+
"room": "101",
14+
}}
15+
thingTagsNotValid := &iotclient.ArduinoThing{Tags: map[string]interface{}{
16+
"location": "rome",
17+
"room": 101,
18+
}}
19+
20+
thing, err := getThingInfo(thingTagsValid)
21+
if err != nil {
22+
t.Error("unexpected error")
23+
}
24+
if len(thing.Tags) != 2 {
25+
fmt.Println(len(thing.Tags))
26+
t.Error("expected two tags")
27+
}
28+
29+
_, err = getThingInfo(thingTagsNotValid)
30+
if err == nil {
31+
t.Error("an error was expected because tags are not valid")
32+
}
33+
}

0 commit comments

Comments
 (0)