2020-06-17 13:46:25 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { flags } from '@oclif/command';
|
|
|
|
import Command from '../../command';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
|
|
|
import { expandForAppName } from '../../utils/helpers';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
|
2021-07-15 13:41:38 +00:00
|
|
|
import {
|
|
|
|
applicationIdInfo,
|
|
|
|
appToFleetFlagMsg,
|
|
|
|
appToFleetOutputMsg,
|
|
|
|
jsonInfo,
|
|
|
|
warnify,
|
|
|
|
} from '../../utils/messages';
|
|
|
|
import { isV13 } from '../../utils/version';
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
import type { Application } from 'balena-sdk';
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
interface ExtendedDevice extends DeviceWithDeviceType {
|
2020-06-17 13:46:25 +00:00
|
|
|
dashboard_url?: string;
|
2020-10-27 21:12:49 +00:00
|
|
|
application_name?: string | null;
|
|
|
|
device_type?: string | null;
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
application?: string;
|
|
|
|
app?: string;
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet?: string;
|
2020-06-17 13:46:25 +00:00
|
|
|
help: void;
|
2020-08-07 14:08:20 +00:00
|
|
|
json: boolean;
|
2021-07-15 13:41:38 +00:00
|
|
|
v13: boolean;
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class DevicesCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
List all devices.
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
List all of your devices.
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Devices can be filtered by fleet with the \`--fleet\` option.
|
2020-08-07 14:08:20 +00:00
|
|
|
|
2020-12-10 12:30:17 +00:00
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
|
|
|
|
|
|
|
${jsonInfo.split('\n').join('\n\t\t')}
|
2020-06-17 13:46:25 +00:00
|
|
|
`;
|
|
|
|
public static examples = [
|
|
|
|
'$ balena devices',
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena devices --fleet MyFleet',
|
|
|
|
'$ balena devices -f myorg/myfleet',
|
2020-06-17 13:46:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = 'devices';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2021-07-15 13:41:38 +00:00
|
|
|
...(isV13()
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
application: {
|
|
|
|
...cf.application,
|
|
|
|
exclusive: ['app', 'fleet', 'v13'],
|
|
|
|
},
|
|
|
|
app: { ...cf.app, exclusive: ['application', 'fleet', 'v13'] },
|
|
|
|
}),
|
|
|
|
fleet: { ...cf.fleet, exclusive: ['app', 'application'] },
|
2020-12-10 12:30:17 +00:00
|
|
|
json: cf.json,
|
2020-06-17 13:46:25 +00:00
|
|
|
help: cf.help,
|
2021-07-15 13:41:38 +00:00
|
|
|
v13: cf.v13,
|
2020-06-17 13:46:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public static primary = true;
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
protected useAppWord = false;
|
|
|
|
protected hasWarned = false;
|
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
public async run() {
|
|
|
|
const { flags: options } = this.parse<FlagsDef, {}>(DevicesCmd);
|
2021-07-15 13:41:38 +00:00
|
|
|
this.useAppWord = !options.fleet && !options.v13 && !isV13();
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
if (
|
|
|
|
(options.application || options.app) &&
|
|
|
|
!options.json &&
|
|
|
|
process.stderr.isTTY
|
|
|
|
) {
|
|
|
|
this.hasWarned = true;
|
|
|
|
console.error(warnify(appToFleetFlagMsg));
|
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
// Consolidate application options
|
2021-07-15 13:41:38 +00:00
|
|
|
options.application ||= options.app || options.fleet;
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
let devices;
|
2020-06-17 13:46:25 +00:00
|
|
|
|
|
|
|
if (options.application != null) {
|
2020-12-10 12:30:17 +00:00
|
|
|
const { getApplication } = await import('../../utils/sdk');
|
|
|
|
const application = await getApplication(balena, options.application);
|
2020-07-31 14:35:20 +00:00
|
|
|
devices = (await balena.models.device.getAllByApplication(
|
2020-12-10 12:30:17 +00:00
|
|
|
application.id,
|
2020-06-17 13:46:25 +00:00
|
|
|
expandForAppName,
|
2020-07-31 14:35:20 +00:00
|
|
|
)) as ExtendedDevice[];
|
2020-06-17 13:46:25 +00:00
|
|
|
} else {
|
2020-07-31 14:35:20 +00:00
|
|
|
devices = (await balena.models.device.getAll(
|
|
|
|
expandForAppName,
|
|
|
|
)) as ExtendedDevice[];
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 16:34:53 +00:00
|
|
|
devices = devices.map(function (device) {
|
2020-06-17 13:46:25 +00:00
|
|
|
device.dashboard_url = balena.models.device.getDashboardUrl(device.uuid);
|
|
|
|
|
2021-07-20 13:57:00 +00:00
|
|
|
const belongsToApplication =
|
|
|
|
device.belongs_to__application as Application[];
|
2020-10-27 21:12:49 +00:00
|
|
|
device.application_name = belongsToApplication?.[0]?.app_name || null;
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2020-12-03 20:54:08 +00:00
|
|
|
device.uuid = options.json ? device.uuid : device.uuid.slice(0, 7);
|
2020-07-31 14:35:20 +00:00
|
|
|
|
2020-10-27 21:12:49 +00:00
|
|
|
device.device_type = device.is_of__device_type?.[0]?.slug || null;
|
2020-06-17 13:46:25 +00:00
|
|
|
return device;
|
|
|
|
});
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
const jName = this.useAppWord ? 'application_name' : 'fleet_name';
|
|
|
|
const tName = this.useAppWord ? 'APPLICATION NAME' : 'FLEET';
|
2020-08-07 14:08:20 +00:00
|
|
|
const fields = [
|
|
|
|
'id',
|
|
|
|
'uuid',
|
|
|
|
'device_name',
|
|
|
|
'device_type',
|
2021-07-15 13:41:38 +00:00
|
|
|
options.json
|
|
|
|
? `application_name => ${jName}`
|
|
|
|
: `application_name => ${tName}`,
|
2020-08-07 14:08:20 +00:00
|
|
|
'status',
|
|
|
|
'is_online',
|
|
|
|
'supervisor_version',
|
|
|
|
'os_version',
|
|
|
|
'dashboard_url',
|
|
|
|
];
|
2021-07-15 13:41:38 +00:00
|
|
|
|
2020-08-07 14:08:20 +00:00
|
|
|
if (options.json) {
|
2021-07-15 13:41:38 +00:00
|
|
|
const { pickAndRename } = await import('../../utils/helpers');
|
|
|
|
const mapped = devices.map((device) => pickAndRename(device, fields));
|
|
|
|
console.log(JSON.stringify(mapped, null, 4));
|
2020-08-07 14:08:20 +00:00
|
|
|
} else {
|
2021-07-15 13:41:38 +00:00
|
|
|
if (!this.hasWarned && this.useAppWord && process.stderr.isTTY) {
|
|
|
|
console.error(warnify(appToFleetOutputMsg));
|
|
|
|
}
|
|
|
|
const _ = await import('lodash');
|
2020-10-27 21:12:49 +00:00
|
|
|
console.log(
|
|
|
|
getVisuals().table.horizontal(
|
|
|
|
devices.map((dev) => _.mapValues(dev, (val) => val ?? 'N/a')),
|
|
|
|
fields,
|
|
|
|
),
|
|
|
|
);
|
2020-08-07 14:08:20 +00:00
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
}
|
|
|
|
}
|