diff --git a/doc/cli.markdown b/doc/cli.markdown index 9b76442f..92d62014 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -174,7 +174,7 @@ Users are encouraged to regularly update the balena CLI to the latest version. - [device reboot <uuid>](#device-reboot-uuid) - [device register <application>](#device-register-application) - [device rename <uuid> [newname]](#device-rename-uuid-newname) - - [device rm <uuid>](#device-rm-uuid) + - [device rm <uuid(s)>](#device-rm-uuid-s) - [device shutdown <uuid>](#device-shutdown-uuid) - [devices](#devices) - [devices supported](#devices-supported) @@ -638,9 +638,9 @@ the new name for the device ### Options -## device rm <uuid> +## device rm <uuid(s)> -Remove a device from balena. +Remove one or more devices from balena. Note this command asks for confirmation interactively. You can avoid this by passing the `--yes` option. @@ -648,13 +648,14 @@ You can avoid this by passing the `--yes` option. Examples: $ balena device rm 7cf02a6 + $ balena device rm 7cf02a6,dc39e52 $ balena device rm 7cf02a6 --yes ### Arguments #### UUID -the uuid of the device to remove +comma-separated list (no blank spaces) of device UUIDs to be removed ### Options diff --git a/lib/actions-oclif/device/rm.ts b/lib/actions-oclif/device/rm.ts index 2d8518dd..786f3ff8 100644 --- a/lib/actions-oclif/device/rm.ts +++ b/lib/actions-oclif/device/rm.ts @@ -33,28 +33,30 @@ interface ArgsDef { export default class DeviceRmCmd extends Command { public static description = stripIndent` - Remove a device. + Remove one or more devices. - Remove a device from balena. + Remove one or more devices from balena. Note this command asks for confirmation interactively. You can avoid this by passing the \`--yes\` option. `; public static examples = [ '$ balena device rm 7cf02a6', + '$ balena device rm 7cf02a6,dc39e52', '$ balena device rm 7cf02a6 --yes', ]; public static args: Array> = [ { name: 'uuid', - description: 'the uuid of the device to remove', + description: + 'comma-separated list (no blank spaces) of device UUIDs to be removed', parse: (dev) => tryAsInteger(dev), required: true, }, ]; - public static usage = 'device rm '; + public static usage = 'device rm '; public static flags: flags.Input = { yes: cf.yes, @@ -72,12 +74,23 @@ export default class DeviceRmCmd extends Command { const patterns = await import('../../utils/patterns'); // Confirm + const uuids = params.uuid.split(','); await patterns.confirm( options.yes, - 'Are you sure you want to delete the device?', + uuids.length > 1 + ? `Are you sure you want to delete ${uuids.length} devices?` + : `Are you sure you want to delete device ${uuids[0]} ?`, ); // Remove - await balena.models.device.remove(params.uuid); + for (const uuid of params.uuid.split(',')) { + try { + await balena.models.device.remove(uuid); + } catch (err) { + console.info(`${err.message}, uuid: ${uuid}`); + process.exitCode = 1; + continue; + } + } } } diff --git a/tests/commands/help.spec.ts b/tests/commands/help.spec.ts index 27f92be0..36ab885a 100644 --- a/tests/commands/help.spec.ts +++ b/tests/commands/help.spec.ts @@ -64,7 +64,7 @@ Additional commands: device reboot restart a device device register register a device device rename [newname] rename a device - device rm remove a device + device rm remove one or more devices device shutdown shutdown a device devices supported list the supported device types (like 'raspberrypi3' or 'intel-nuc') env add [value] add an environment or config variable to an application, device or service