Skip to content

Commit 30d5810

Browse files
committed
Device filtering by status
1 parent 5ca90df commit 30d5810

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cli/device/list.go

Lines changed: 6 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

0 commit comments

Comments
 (0)