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

View File

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