Files
api/app/core/modules/info/get-app-count.ts
Alexis Tyler 4e1b0bd72c chore: lint
2021-01-28 15:45:14 +10:30

48 lines
1.1 KiB
TypeScript

/*!
* Copyright 2019-2020 Lime Technology Inc. All rights reserved.
* Written by: Alexis Tyler
*/
import { CoreResult, CoreContext } from '../../types';
import { docker, ensurePermission } from '../../utils';
/**
* Two arrays containing the installed and started containers.
*
* @param installed The amount of installed containers.
* @param started The amount of running containers.
* @interface Result
* @extends {CoreResult}
*/
interface Result extends CoreResult {
json: {
installed: number;
started: number;
};
}
/**
* Get count of docker containers
*/
export const getAppCount = async function (context: Readonly<CoreContext>): Promise<Result> {
const { user } = context;
// Check permissions
ensurePermission(user, {
resource: 'docker/container',
action: 'read',
possession: 'any'
});
const installed = await docker.listContainers({ all: true }).catch(() => []).then(containers => containers.length);
const started = await docker.listContainers().catch(() => []).then(containers => containers.length);
return {
text: `Installed: ${installed} \nStarted: ${started}`,
json: {
installed,
started
}
};
};