All files / src/commands/applications applications.list.command.js

22% Statements 22/100
100% Branches 1/1
0% Functions 0/2
22% Lines 22/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101193x 193x 193x 193x 193x 193x 193x 193x 193x 193x       193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                                                                                                       193x 193x  
import { formatTable } from '../../format-table.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as AppConfig from '../../models/app_configuration.js';
import * as Application from '../../models/application.js';
import * as Organisation from '../../models/organisation.js';
import { truncateWithEllipsis } from '../../models/utils.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
 
function getPropertyMaxWidth(array, propertyName) {
  return Math.max(...array.map((o) => o[propertyName].length));
}
 
export const applicationsListCommand = defineCommand({
  description: 'List all applications',
  since: '3.8.0',
  options: {
    org: orgaIdOrNameOption,
    format: humanJsonOutputFormatOption,
  },
  args: [],
  async handler(options) {
    const { org: orgaIdOrName, format } = options;

    const linkedApps = await AppConfig.loadApplicationConf().then((conf) => conf.apps);

    const ownerId =
      orgaIdOrName != null && orgaIdOrName.orga_name !== '' ? await Organisation.getId(orgaIdOrName) : null;

    const allAppsPerOrg = await Application.getAllApps(ownerId);

    // For each app, we try to find a corresponding local alias in the configuration file
    const allAppsWithAliasPerOrg = allAppsPerOrg.map((org) => {
      const applicationsWithAlias = org.applications.map((app) => {
        const foundAlias = linkedApps.find((a) => a.app_id === app.app_id)?.alias;
        return { ...app, alias: foundAlias ?? '' };
      });
      return { ...org, applications: applicationsWithAlias };
    });

    switch (format) {
      case 'json': {
        Logger.printJson(allAppsWithAliasPerOrg);
        break;
      }
      case 'human':
      default: {
        const headers = ['APPLICATION ID', 'NAME', 'TYPE', 'ZONE', 'LOCAL ALIAS'];
        const appNameWidth = 42;

        // We calculate the maximum width of each column to format the table
        const allApps = allAppsWithAliasPerOrg.flatMap((org) => org.applications);
        const columnWidths = [
          getPropertyMaxWidth(allApps, 'app_id'),
          appNameWidth,
          getPropertyMaxWidth(allApps, 'type'),
          getPropertyMaxWidth(allApps, 'zone'),
          getPropertyMaxWidth(allApps, 'alias'),
        ];

        allAppsWithAliasPerOrg.forEach((org) => {
          Logger.println();

          const applicationsPlural = org.applications.length !== 1 ? 'applications' : 'application';
          const punctuation = org.applications.length > 0 ? ':' : '';

          Logger.println(
            styleText(
              'blue',
              `• Organization '${org.name}' (${org.id}) with ${org.applications.length} ${applicationsPlural}${punctuation}`,
            ),
          );

          if (org.applications.length > 0) {
            Logger.println();
            Logger.println(
              formatTable(
                [
                  headers,
                  ...org.applications.map((app) => [
                    app.app_id,
                    truncateWithEllipsis(appNameWidth, app.name),
                    app.type,
                    app.zone,
                    app.alias,
                  ]),
                ],

                columnWidths,
              ),
            );
          }
        });

        break;
      }
    }
  },
});