2020-04-22 16:08:11 +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';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { getBalenaSdk, getVisuals, stripIndent } from '../utils/lazy';
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
interface ExtendedApplication extends ApplicationWithDeviceType {
|
2020-04-22 16:08:11 +00:00
|
|
|
device_count?: number;
|
|
|
|
online_devices?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
help: void;
|
2020-12-07 13:35:57 +00:00
|
|
|
verbose: boolean;
|
2020-04-22 16:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class AppsCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
|
|
|
List all applications.
|
|
|
|
|
|
|
|
list all your balena applications.
|
|
|
|
|
|
|
|
For detailed information on a particular application,
|
2020-12-10 12:30:17 +00:00
|
|
|
use \`balena app <application>\` instead.
|
|
|
|
`;
|
|
|
|
|
2020-04-22 16:08:11 +00:00
|
|
|
public static examples = ['$ balena apps'];
|
|
|
|
|
|
|
|
public static usage = 'apps';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
help: cf.help,
|
2020-05-13 14:45:22 +00:00
|
|
|
verbose: flags.boolean({
|
2020-12-07 13:35:57 +00:00
|
|
|
default: false,
|
2020-05-13 14:45:22 +00:00
|
|
|
char: 'v',
|
2020-12-07 13:58:23 +00:00
|
|
|
description: 'No-op since release v12.0.0',
|
2020-05-13 14:45:22 +00:00
|
|
|
}),
|
2020-04-22 16:08:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
public static primary = true;
|
|
|
|
|
|
|
|
public async run() {
|
2020-12-10 22:13:30 +00:00
|
|
|
this.parse<FlagsDef, {}>(AppsCmd);
|
2020-04-22 16:08:11 +00:00
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
|
|
|
// Get applications
|
2020-07-31 14:35:20 +00:00
|
|
|
const applications = (await balena.models.application.getAll({
|
|
|
|
$select: ['id', 'app_name', 'slug'],
|
|
|
|
$expand: {
|
|
|
|
is_for__device_type: { $select: 'slug' },
|
|
|
|
owns__device: { $select: 'is_online' },
|
2020-04-22 16:08:11 +00:00
|
|
|
},
|
2020-07-31 14:35:20 +00:00
|
|
|
})) as ExtendedApplication[];
|
2020-04-22 16:08:11 +00:00
|
|
|
|
2020-07-08 17:21:37 +00:00
|
|
|
const _ = await import('lodash');
|
2020-04-22 16:08:11 +00:00
|
|
|
// Add extended properties
|
2020-06-15 22:53:07 +00:00
|
|
|
applications.forEach((application) => {
|
2020-07-08 16:34:53 +00:00
|
|
|
application.device_count = application.owns__device?.length ?? 0;
|
2020-06-15 22:53:07 +00:00
|
|
|
application.online_devices = _.sumBy(application.owns__device, (d) =>
|
2020-04-22 16:08:11 +00:00
|
|
|
d.is_online === true ? 1 : 0,
|
|
|
|
);
|
2020-07-31 14:35:20 +00:00
|
|
|
// @ts-expect-error
|
|
|
|
application.device_type = application.is_for__device_type[0].slug;
|
2020-04-22 16:08:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Display
|
|
|
|
console.log(
|
|
|
|
getVisuals().table.horizontal(applications, [
|
|
|
|
'id',
|
|
|
|
'app_name',
|
2020-12-10 22:13:30 +00:00
|
|
|
'slug',
|
2020-04-22 16:08:11 +00:00
|
|
|
'device_type',
|
|
|
|
'online_devices',
|
|
|
|
'device_count',
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|