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.
|
|
|
|
*/
|
2023-09-06 14:39:31 +00:00
|
|
|
import { Flags } from '@oclif/core';
|
2022-10-17 14:01:33 +00:00
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
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';
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static flags = {
|
2019-12-26 22:59:44 +00:00
|
|
|
help: cf.help,
|
2023-09-06 14:39:31 +00:00
|
|
|
json: Flags.boolean({
|
2019-12-26 22:59:44 +00:00
|
|
|
char: 'j',
|
|
|
|
description: 'produce JSON output instead of tabular output',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
public async run() {
|
2023-09-06 14:39:31 +00:00
|
|
|
const { flags: options } = await this.parse(DevicesSupportedCmd);
|
2022-10-17 14:01:33 +00:00
|
|
|
const pineOptions = {
|
2023-03-27 13:39:09 +00:00
|
|
|
$select: ['slug', 'name'],
|
2022-10-17 14:01:33 +00:00
|
|
|
$expand: {
|
|
|
|
is_of__cpu_architecture: { $select: 'slug' },
|
|
|
|
device_type_alias: {
|
|
|
|
$select: 'is_referenced_by__alias',
|
2023-03-27 13:39:09 +00:00
|
|
|
$orderby: { is_referenced_by__alias: 'asc' },
|
2022-10-17 14:01:33 +00:00
|
|
|
},
|
|
|
|
},
|
2023-03-27 13:39:09 +00:00
|
|
|
} satisfies BalenaSdk.PineOptions<BalenaSdk.DeviceType>;
|
2022-10-17 14:01:33 +00:00
|
|
|
const dts = (await getBalenaSdk().models.deviceType.getAllSupported(
|
|
|
|
pineOptions,
|
|
|
|
)) as Array<
|
|
|
|
BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>
|
|
|
|
>;
|
2021-05-14 23:58:15 +00:00
|
|
|
interface DT {
|
|
|
|
slug: string;
|
|
|
|
aliases: string[];
|
|
|
|
arch: string;
|
|
|
|
name: string;
|
2019-12-26 22:59:44 +00:00
|
|
|
}
|
2022-10-17 14:01:33 +00:00
|
|
|
let deviceTypes = dts.map((dt): DT => {
|
|
|
|
const aliases = dt.device_type_alias
|
|
|
|
.map((dta) => dta.is_referenced_by__alias)
|
|
|
|
.filter((alias) => alias !== dt.slug);
|
|
|
|
return {
|
|
|
|
slug: dt.slug,
|
2021-05-14 23:58:15 +00:00
|
|
|
aliases: options.json ? aliases : [aliases.join(', ')],
|
2022-10-17 14:01:33 +00:00
|
|
|
arch: dt.is_of__cpu_architecture[0]?.slug || 'n/a',
|
2021-12-19 22:40:26 +00:00
|
|
|
name: dt.name || 'N/A',
|
2022-10-17 14:01:33 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|