Skip to content

Device filtering by status #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

type listFlags struct {
tags map[string]string
status string
deviceIds string
}

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

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

params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds}
params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds, Status: flags.status}
devs, err := device.List(context.TODO(), params, cred)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions command/device/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
type ListParams struct {
Tags map[string]string // If tags are provided, only devices that have all these tags are listed.
DeviceIds string // If ids are provided, only devices with these ids are listed.
Status string // If status is provided, only devices with this status are listed.
}

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

Expand Down
13 changes: 11 additions & 2 deletions internal/template/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"

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

if thing.Tags != nil {
tags := []map[string]any{}
for k, v := range thing.Tags {
tag := make(map[string]any)
tag[k] = v
tags = append(tags, tag)
}
template["tags"] = tags
}

return template
}

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

err = ioutil.WriteFile(outfile, file, os.FileMode(0644))
err = os.WriteFile(outfile, file, os.FileMode(0644))
if err != nil {
return fmt.Errorf("%s: %w", "cannot write outfile: ", err)
}
Expand Down
Loading