From 30d5810a44ffe27e3f7601843f3fea39cfedc0d6 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Mon, 19 Aug 2024 12:42:26 +0200 Subject: [PATCH 1/2] Device filtering by status --- cli/device/list.go | 7 ++++++- command/device/list.go | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/device/list.go b/cli/device/list.go index d7799a0d..d7d9597f 100644 --- a/cli/device/list.go +++ b/cli/device/list.go @@ -34,6 +34,7 @@ import ( type listFlags struct { tags map[string]string + status string deviceIds string } @@ -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 } @@ -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 diff --git a/command/device/list.go b/command/device/list.go index ba1ffd43..46004a21 100644 --- a/command/device/list.go +++ b/command/device/list.go @@ -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 @@ -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) } From a7c130afb23d124458e3f27d22a02dff03d8cb67 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Mon, 19 Aug 2024 14:14:05 +0200 Subject: [PATCH 2/2] Added tags in template extraction --- internal/template/extract.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/template/extract.go b/internal/template/extract.go index ecbe3163..1585c577 100644 --- a/internal/template/extract.go +++ b/internal/template/extract.go @@ -21,7 +21,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" iotclient "github.com/arduino/iot-client-go/v2" @@ -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 } @@ -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) }