Merge pull request #2032 from balena-io/misc-bugfixes

Misc bugfixes
This commit is contained in:
bulldozer-balena[bot] 2020-09-10 12:07:33 +00:00 committed by GitHub
commit e755d9f03f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 61 deletions

View File

@ -17,7 +17,7 @@
import { flags } from '@oclif/command'; import { flags } from '@oclif/command';
import type { IArg } from '@oclif/parser/lib/args'; import type { IArg } from '@oclif/parser/lib/args';
import type { Application } from 'balena-sdk'; import type { Application, BalenaSDK } from 'balena-sdk';
import Command from '../../command'; import Command from '../../command';
import * as cf from '../../utils/common-flags'; import * as cf from '../../utils/common-flags';
import { expandForAppName } from '../../utils/helpers'; import { expandForAppName } from '../../utils/helpers';
@ -59,7 +59,6 @@ export default class DeviceMoveCmd extends Command {
name: 'uuid', name: 'uuid',
description: description:
'comma-separated list (no blank spaces) of device UUIDs to be moved', 'comma-separated list (no blank spaces) of device UUIDs to be moved',
parse: (dev) => tryAsInteger(dev),
required: true, required: true,
}, },
]; ];
@ -81,14 +80,17 @@ export default class DeviceMoveCmd extends Command {
const balena = getBalenaSdk(); const balena = getBalenaSdk();
// Consolidate application options
options.application = options.application || options.app; options.application = options.application || options.app;
delete options.app; delete options.app;
const devices = await Promise.all( // Parse ids string into array of correct types
params.uuid const deviceIds: Array<string | number> = params.uuid
.split(',') .split(',')
.map( .map((id) => tryAsInteger(id));
// Get devices
const devices = await Promise.all(
deviceIds.map(
(uuid) => (uuid) =>
balena.models.device.get(uuid, expandForAppName) as Promise< balena.models.device.get(uuid, expandForAppName) as Promise<
ExtendedDevice ExtendedDevice
@ -96,6 +98,7 @@ export default class DeviceMoveCmd extends Command {
), ),
); );
// Map application name for each device
for (const device of devices) { for (const device of devices) {
const belongsToApplication = device.belongs_to__application as Application[]; const belongsToApplication = device.belongs_to__application as Application[];
device.application_name = belongsToApplication?.[0] device.application_name = belongsToApplication?.[0]
@ -104,10 +107,26 @@ export default class DeviceMoveCmd extends Command {
} }
// Get destination application // Get destination application
let application; const application =
if (options.application) { options.application ||
application = options.application; (await this.interactivelySelectApplication(balena, devices));
} else {
// Move each device
for (const uuid of deviceIds) {
try {
await balena.models.device.move(uuid, tryAsInteger(application));
console.info(`${uuid} was moved to ${application}`);
} catch (err) {
console.info(`${err.message}, uuid: ${uuid}`);
process.exitCode = 1;
}
}
}
async interactivelySelectApplication(
balena: BalenaSDK,
devices: ExtendedDevice[],
) {
const [deviceDeviceTypes, deviceTypes] = await Promise.all([ const [deviceDeviceTypes, deviceTypes] = await Promise.all([
Promise.all( Promise.all(
devices.map((device) => devices.map((device) =>
@ -133,7 +152,7 @@ export default class DeviceMoveCmd extends Command {
const patterns = await import('../../utils/patterns'); const patterns = await import('../../utils/patterns');
try { try {
application = await patterns.selectApplication( const application = await patterns.selectApplication(
(app) => (app) =>
compatibleDeviceTypes.some( compatibleDeviceTypes.some(
(dt) => dt.slug === app.is_for__device_type[0].slug, (dt) => dt.slug === app.is_for__device_type[0].slug,
@ -142,6 +161,7 @@ export default class DeviceMoveCmd extends Command {
devices.some((device) => device.application_name !== app.app_name), devices.some((device) => device.application_name !== app.app_name),
true, true,
); );
return application;
} catch (err) { } catch (err) {
if (deviceDeviceTypes.length) { if (deviceDeviceTypes.length) {
throw new ExpectedError( throw new ExpectedError(
@ -151,15 +171,4 @@ export default class DeviceMoveCmd extends Command {
throw err; throw err;
} }
} }
for (const uuid of params.uuid.split(',')) {
try {
await balena.models.device.move(uuid, tryAsInteger(application));
console.info(`${uuid} was moved to ${application}`);
} catch (err) {
console.info(`${err.message}, uuid: ${uuid}`);
process.exitCode = 1;
}
}
}
} }

View File

@ -51,7 +51,6 @@ export default class DeviceRmCmd extends Command {
name: 'uuid', name: 'uuid',
description: description:
'comma-separated list (no blank spaces) of device UUIDs to be removed', 'comma-separated list (no blank spaces) of device UUIDs to be removed',
parse: (dev) => tryAsInteger(dev),
required: true, required: true,
}, },
]; ];
@ -85,7 +84,7 @@ export default class DeviceRmCmd extends Command {
// Remove // Remove
for (const uuid of params.uuid.split(',')) { for (const uuid of params.uuid.split(',')) {
try { try {
await balena.models.device.remove(uuid); await balena.models.device.remove(tryAsInteger(uuid));
} catch (err) { } catch (err) {
console.info(`${err.message}, uuid: ${uuid}`); console.info(`${err.message}, uuid: ${uuid}`);
process.exitCode = 1; process.exitCode = 1;

View File

@ -42,6 +42,7 @@ export default class OsVersionsCmd extends Command {
{ {
name: 'type', name: 'type',
description: 'device type', description: 'device type',
required: true,
}, },
]; ];