2019-12-26 22:59:44 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2021-05-14 23:58:15 +00:00
|
|
|
* Copyright 2016-2021 Balena Ltd.
|
2019-12-26 22:59:44 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-03-19 08:45:57 +00:00
|
|
|
import { flags } from '@oclif/command';
|
2019-12-26 22:59:44 +00:00
|
|
|
import * as _ from 'lodash';
|
2020-03-19 08:45:57 +00:00
|
|
|
import Command from '../../command';
|
2019-12-26 22:59:44 +00:00
|
|
|
|
|
|
|
import * as cf from '../../utils/common-flags';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
|
2019-12-26 22:59:44 +00:00
|
|
|
import { CommandHelp } from '../../utils/oclif-utils';
|
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
help: void;
|
|
|
|
json?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class DevicesSupportedCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
|
|
|
|
|
|
|
List the supported device types (like 'raspberrypi3' or 'intel-nuc').
|
2021-12-19 22:40:26 +00:00
|
|
|
|
2020-01-03 23:13:20 +00:00
|
|
|
The --json option is recommended when scripting the output of this command,
|
|
|
|
because the JSON format is less likely to change and it better represents data
|
|
|
|
types like lists and empty strings (for example, the ALIASES column contains a
|
|
|
|
list of zero or more values). The 'jq' utility may be helpful in shell scripts
|
|
|
|
(https://stedolan.github.io/jq/manual/).
|
2019-12-26 22:59:44 +00:00
|
|
|
`;
|
|
|
|
public static examples = [
|
|
|
|
'$ balena devices supported',
|
2021-12-19 22:40:26 +00:00
|
|
|
'$ balena devices supported --json',
|
2019-12-26 22:59:44 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = (
|
|
|
|
'devices supported ' +
|
|
|
|
new CommandHelp({ args: DevicesSupportedCmd.args }).defaultUsage()
|
|
|
|
).trim();
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
help: cf.help,
|
|
|
|
json: flags.boolean({
|
|
|
|
char: 'j',
|
|
|
|
description: 'produce JSON output instead of tabular output',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { flags: options } = this.parse<FlagsDef, {}>(DevicesSupportedCmd);
|
2021-05-14 23:58:15 +00:00
|
|
|
const [dts, configDTs] = await Promise.all([
|
|
|
|
getBalenaSdk().models.deviceType.getAllSupported({
|
|
|
|
$expand: { is_of__cpu_architecture: { $select: 'slug' } },
|
|
|
|
$select: ['slug', 'name'],
|
|
|
|
}),
|
|
|
|
getBalenaSdk().models.config.getDeviceTypes(),
|
|
|
|
]);
|
|
|
|
const dtsBySlug = _.keyBy(dts, (dt) => dt.slug);
|
|
|
|
const configDTsBySlug = _.keyBy(configDTs, (dt) => dt.slug);
|
|
|
|
interface DT {
|
|
|
|
slug: string;
|
|
|
|
aliases: string[];
|
|
|
|
arch: string;
|
|
|
|
name: string;
|
2019-12-26 22:59:44 +00:00
|
|
|
}
|
2021-05-14 23:58:15 +00:00
|
|
|
let deviceTypes: DT[] = [];
|
2021-12-19 22:40:26 +00:00
|
|
|
for (const slug of Object.keys(dtsBySlug)) {
|
2021-05-14 23:58:15 +00:00
|
|
|
const configDT: Partial<typeof configDTs[0]> =
|
|
|
|
configDTsBySlug[slug] || {};
|
|
|
|
const aliases = (configDT.aliases || []).filter(
|
|
|
|
(alias) => alias !== slug,
|
|
|
|
);
|
2021-12-19 22:40:26 +00:00
|
|
|
const dt: Partial<typeof dts[0]> = dtsBySlug[slug] || {};
|
2021-05-14 23:58:15 +00:00
|
|
|
deviceTypes.push({
|
|
|
|
slug,
|
|
|
|
aliases: options.json ? aliases : [aliases.join(', ')],
|
2021-12-19 22:40:26 +00:00
|
|
|
arch: (dt.is_of__cpu_architecture as any)?.[0]?.slug || 'n/a',
|
|
|
|
name: dt.name || 'N/A',
|
2021-05-14 23:58:15 +00:00
|
|
|
});
|
|
|
|
}
|
2021-12-19 22:40:26 +00:00
|
|
|
const fields = ['slug', 'aliases', 'arch', 'name'];
|
2021-05-14 23:58:15 +00:00
|
|
|
deviceTypes = _.sortBy(deviceTypes, fields);
|
2019-12-26 22:59:44 +00:00
|
|
|
if (options.json) {
|
|
|
|
console.log(JSON.stringify(deviceTypes, null, 4));
|
|
|
|
} else {
|
2020-02-28 13:06:14 +00:00
|
|
|
const visuals = getVisuals();
|
2019-12-26 22:59:44 +00:00
|
|
|
const output = await visuals.table.horizontal(deviceTypes, fields);
|
|
|
|
console.log(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|