2021-07-15 13:41:38 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2021 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.
|
|
|
|
*/
|
|
|
|
|
2023-07-19 16:14:06 +00:00
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
2021-12-19 20:48:13 +00:00
|
|
|
import { flags } from '@oclif/command';
|
2021-07-15 13:41:38 +00:00
|
|
|
|
2021-12-19 20:48:13 +00:00
|
|
|
import Command from '../command';
|
|
|
|
import * as cf from '../utils/common-flags';
|
2022-07-20 16:34:50 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from '../utils/lazy';
|
2021-12-19 20:48:13 +00:00
|
|
|
import type { DataSetOutputOptions } from '../framework';
|
|
|
|
|
2023-07-19 16:14:06 +00:00
|
|
|
interface ExtendedApplication extends ApplicationWithDeviceTypeSlug {
|
2021-12-19 20:48:13 +00:00
|
|
|
device_count: number;
|
|
|
|
online_devices: number;
|
|
|
|
device_type?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FlagsDef extends DataSetOutputOptions {
|
|
|
|
help: void;
|
|
|
|
verbose?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class FleetsCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
List all fleets.
|
|
|
|
|
|
|
|
List all your balena fleets.
|
|
|
|
|
|
|
|
For detailed information on a particular fleet, use
|
|
|
|
\`balena fleet <fleet>\`
|
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = ['$ balena fleets'];
|
|
|
|
|
|
|
|
public static usage = 'fleets';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2022-07-20 13:50:15 +00:00
|
|
|
...cf.dataSetOutputFlags,
|
2021-12-19 20:48:13 +00:00
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
public static primary = true;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { flags: options } = this.parse<FlagsDef, {}>(FleetsCmd);
|
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2023-07-19 16:14:06 +00:00
|
|
|
const pineOptions = {
|
|
|
|
$select: ['id', 'app_name', 'slug'],
|
|
|
|
$expand: {
|
|
|
|
is_for__device_type: { $select: 'slug' },
|
|
|
|
owns__device: { $select: 'is_online' },
|
|
|
|
},
|
|
|
|
} satisfies BalenaSdk.PineOptions<BalenaSdk.Application>;
|
2021-12-19 20:48:13 +00:00
|
|
|
// Get applications
|
2021-12-21 00:02:45 +00:00
|
|
|
const applications =
|
2023-07-19 16:14:06 +00:00
|
|
|
(await balena.models.application.getAllDirectlyAccessible(
|
|
|
|
pineOptions,
|
|
|
|
)) as Array<
|
|
|
|
BalenaSdk.PineTypedResult<BalenaSdk.Application, typeof pineOptions>
|
|
|
|
> as ExtendedApplication[];
|
2021-12-19 20:48:13 +00:00
|
|
|
|
|
|
|
// Add extended properties
|
|
|
|
applications.forEach((application) => {
|
|
|
|
application.device_count = application.owns__device?.length ?? 0;
|
|
|
|
application.online_devices =
|
|
|
|
application.owns__device?.filter((d) => d.is_online).length || 0;
|
|
|
|
application.device_type = application.is_for__device_type[0].slug;
|
|
|
|
});
|
|
|
|
|
2022-07-20 16:34:50 +00:00
|
|
|
await this.outputData(
|
|
|
|
applications,
|
|
|
|
[
|
|
|
|
'id',
|
|
|
|
'app_name',
|
|
|
|
'slug',
|
|
|
|
'device_type',
|
|
|
|
'device_count',
|
|
|
|
'online_devices',
|
|
|
|
],
|
|
|
|
options,
|
|
|
|
);
|
2021-12-19 20:48:13 +00:00
|
|
|
}
|
|
|
|
}
|