Skip to content

Commit 5483ab3

Browse files
authored
Device filtering by status (#158)
1 parent 5ca90df commit 5483ab3

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

cli/device/list.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
type listFlags struct {
3636
tags map[string]string
37+
status string
3738
deviceIds string
3839
}
3940

@@ -58,6 +59,7 @@ func initListCommand() *cobra.Command {
5859
"List only devices that match the provided tags.",
5960
)
6061
listCommand.Flags().StringVarP(&flags.deviceIds, "device-ids", "d", "", "Comma separated list of Device IDs")
62+
listCommand.Flags().StringVarP(&flags.status, "device-status", "s", "", "List only devices according to the provided status [ONLINE|OFFLINE|UNKNOWN]")
6163
return listCommand
6264
}
6365

@@ -68,8 +70,11 @@ func runListCommand(flags *listFlags) error {
6870
if err != nil {
6971
return fmt.Errorf("retrieving credentials: %w", err)
7072
}
73+
if flags.status != "" && flags.status != "ONLINE" && flags.status != "OFFLINE" && flags.status != "UNKNOWN" {
74+
return fmt.Errorf("invalid status: %s", flags.status)
75+
}
7176

72-
params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds}
77+
params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds, Status: flags.status}
7378
devs, err := device.List(context.TODO(), params, cred)
7479
if err != nil {
7580
return err

command/device/list.go

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
type ListParams struct {
3232
Tags map[string]string // If tags are provided, only devices that have all these tags are listed.
3333
DeviceIds string // If ids are provided, only devices with these ids are listed.
34+
Status string // If status is provided, only devices with this status are listed.
3435
}
3536

3637
// List command is used to list
@@ -62,6 +63,9 @@ func List(ctx context.Context, params *ListParams, cred *config.Credentials) ([]
6263
if err != nil {
6364
return nil, fmt.Errorf("parsing device %s from cloud: %w", foundDev.Id, err)
6465
}
66+
if params.Status != "" && dev.Status != nil && *dev.Status != params.Status {
67+
continue
68+
}
6569
devices = append(devices, *dev)
6670
}
6771

internal/template/extract.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24-
"io/ioutil"
2524
"os"
2625

2726
iotclient "github.com/arduino/iot-client-go/v2"
@@ -48,6 +47,16 @@ func FromThing(thing *iotclient.ArduinoThing) map[string]interface{} {
4847
}
4948
template["variables"] = props
5049

50+
if thing.Tags != nil {
51+
tags := []map[string]any{}
52+
for k, v := range thing.Tags {
53+
tag := make(map[string]any)
54+
tag[k] = v
55+
tags = append(tags, tag)
56+
}
57+
template["tags"] = tags
58+
}
59+
5160
return template
5261
}
5362

@@ -119,7 +128,7 @@ func ToFile(template map[string]interface{}, outfile string, format string) erro
119128
return errors.New("format is not valid: only 'json' and 'yaml' are supported")
120129
}
121130

122-
err = ioutil.WriteFile(outfile, file, os.FileMode(0644))
131+
err = os.WriteFile(outfile, file, os.FileMode(0644))
123132
if err != nil {
124133
return fmt.Errorf("%s: %w", "cannot write outfile: ", err)
125134
}

0 commit comments

Comments
 (0)