Skip to content

Commit feae909

Browse files
committed
Added device filter support
1 parent 5983aae commit feae909

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

cli/device/list.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import (
3333
)
3434

3535
type listFlags struct {
36-
tags map[string]string
36+
tags map[string]string
37+
deviceIds string
3738
}
3839

3940
func initListCommand() *cobra.Command {
@@ -56,6 +57,7 @@ func initListCommand() *cobra.Command {
5657
"Comma-separated list of tags with format <key>=<value>.\n"+
5758
"List only devices that match the provided tags.",
5859
)
60+
listCommand.Flags().StringVarP(&flags.deviceIds, "device-ids", "d", "", "Comma separated list of Device IDs")
5961
return listCommand
6062
}
6163

@@ -67,7 +69,7 @@ func runListCommand(flags *listFlags) error {
6769
return fmt.Errorf("retrieving credentials: %w", err)
6870
}
6971

70-
params := &device.ListParams{Tags: flags.tags}
72+
params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds}
7173
devs, err := device.List(context.TODO(), params, cred)
7274
if err != nil {
7375
return err

command/device/list.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package device
2020
import (
2121
"context"
2222
"fmt"
23+
"slices"
24+
"strings"
2325

2426
"github.com/arduino/arduino-cloud-cli/config"
2527
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -28,7 +30,8 @@ import (
2830
// ListParams contains the optional parameters needed
2931
// to filter the devices to be listed.
3032
type ListParams struct {
31-
Tags map[string]string // If tags are provided, only devices that have all these tags are listed.
33+
Tags map[string]string // If tags are provided, only devices that have all these tags are listed.
34+
DeviceIds string // If ids are provided, only devices with these ids are listed.
3235
}
3336

3437
// List command is used to list
@@ -43,9 +46,19 @@ func List(ctx context.Context, params *ListParams, cred *config.Credentials) ([]
4346
if err != nil {
4447
return nil, err
4548
}
49+
var deviceIdFilter []string
50+
if params.DeviceIds != "" {
51+
deviceIdFilter = strings.Split(params.DeviceIds, ",")
52+
for i := range deviceIdFilter {
53+
deviceIdFilter[i] = strings.TrimSpace(deviceIdFilter[i])
54+
}
55+
}
4656

4757
var devices []DeviceInfo
4858
for _, foundDev := range foundDevices {
59+
if len(deviceIdFilter) > 0 && !slices.Contains(deviceIdFilter, foundDev.Id) {
60+
continue
61+
}
4962
dev, err := getDeviceInfo(&foundDev)
5063
if err != nil {
5164
return nil, fmt.Errorf("parsing device %s from cloud: %w", foundDev.Id, err)

example/tools/ota/ota_mass_upload.sh

+45-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ checkExecutable () {
1212
fi
1313
}
1414

15+
printHelp () {
16+
echo ""
17+
echo "Usage: $0 [-t <tag> | -d <device ids>] -f <firmwarefile> [-o <waittime in seconds - default 600>] [-v <new firmware version for tagging updated devices>]"
18+
echo ""
19+
echo "Examples -----------------"
20+
echo " perform ota on devices with firmware=v1 tag"
21+
echo " $0 -t firmware=v1 -f myfirmware.bin"
22+
echo " perform ota on devices with firmware=v1 tag and apply new tag firmware=v2 to updated devices, waiting for 1200 seconds"
23+
echo " $0 -t firmware=v1 -f myfirmware.bin -v firmware=v2 -w 1200"
24+
echo " perform ota on two specified devices"
25+
echo " $0 -d 261ec96a-38ba-4520-96e6-2447c4163e9b,8b10acdb-b722-4068-8e4d-d1c1b7302df4 -f myfirmware.bin"
26+
echo ""
27+
exit 1
28+
}
29+
1530
# Check dependencies...
1631
checkExecutable "arduino-cloud-cli"
1732
checkExecutable "jq"
@@ -23,33 +38,48 @@ checkExecutable "paste"
2338
waittime=600
2439
newtagversion=""
2540

26-
while getopts t:v:f:o: flag
41+
while getopts t:v:f:o:d: flag
2742
do
2843
case "${flag}" in
2944
t) tag=${OPTARG};;
3045
v) newtagversion=${OPTARG};;
3146
f) firmwarefile=${OPTARG};;
3247
o) waittime=${OPTARG};;
48+
d) deviceids=${OPTARG};;
3349
esac
3450
done
3551

36-
if [[ "$firmwarefile" == "" || "$tag" == "" || "$waittime" == "" ]]; then
37-
echo "Usage: $0 -t <tag> -f <firmwarefile> [-o <waittime>] [-v <new firmware version for tagging>]"
38-
exit 1
52+
if [[ "$firmwarefile" == "" || "$waittime" == "" ]]; then
53+
printHelp
54+
fi
55+
if [[ "$tag" == "" && "$deviceids" == "" ]]; then
56+
printHelp
3957
fi
4058

41-
echo "Starting OTA process for devices with tag \"$tag\" using firmware \"$firmwarefile\""
42-
echo ""
59+
if [[ "$deviceids" == "" ]]; then
60+
echo "Starting OTA process for devices with tag \"$tag\" using firmware \"$firmwarefile\""
61+
echo ""
62+
63+
devicelistjson=$(arduino-cloud-cli device list --tags $tag --format json)
64+
else
65+
echo "Starting OTA process for devices \"$deviceids\" using firmware \"$firmwarefile\""
66+
echo ""
67+
68+
devicelistjson=$(arduino-cloud-cli device list -d $deviceids --format json)
69+
fi
4370

44-
devicelistjson=$(arduino-cloud-cli device list --tags $tag --format json)
4571
devicecount=$(echo $devicelistjson | jq '.[] | .id' | wc -l)
4672

4773
if [ "$devicecount" -gt 0 ]; then
48-
echo "Found $devicecount devices with tag \"$tag\""
74+
echo "Found $devicecount devices"
4975
echo ""
50-
arduino-cloud-cli device list --tags $tag
76+
if [[ "$deviceids" == "" ]]; then
77+
arduino-cloud-cli device list --tags $tag
78+
else
79+
arduino-cloud-cli device list -d $deviceids
80+
fi
5181
else
52-
echo "No devices found with tag \"$tag\""
82+
echo "No device found"
5383
exit 1
5484
fi
5585

@@ -66,7 +96,11 @@ fi
6696
fqbn=$(echo $devicelistjson | jq -r '.[] | .fqbn' | sort | uniq | head -n 1)
6797

6898
echo "Sending OTA request to detected boards of type $fqbn..."
69-
otastartedout=$(arduino-cloud-cli ota mass-upload --device-tags $tag --file $firmwarefile -b $fqbn --format json)
99+
if [[ "$deviceids" == "" ]]; then
100+
otastartedout=$(arduino-cloud-cli ota mass-upload --device-tags $tag --file $firmwarefile -b $fqbn --format json)
101+
else
102+
otastartedout=$(arduino-cloud-cli ota mass-upload -d $deviceids --file $firmwarefile -b $fqbn --format json)
103+
fi
70104
if [ $? -ne 0 ]; then
71105
echo "Detected error during OTA process. Exiting..."
72106
exit 1

0 commit comments

Comments
 (0)