All files / src/commands/database database.backups.command.js

35.08% Statements 20/57
100% Branches 1/1
0% Functions 0/1
35.08% Lines 20/57

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 58193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                           193x 193x  
import { getBackups } from '@clevercloud/client/esm/api/v2/backups.js';
import { formatTable } from '../../format-table.js';
import { defineCommand } from '../../lib/define-command.js';
import { Logger } from '../../logger.js';
import { resolveAddon } from '../../models/ids-resolver.js';
import { sendToApi } from '../../models/send-to-api.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { databaseIdArg } from './database.args.js';
 
export const databaseBackupsCommand = defineCommand({
  description: 'List available database backups',
  since: '2.10.0',
  options: {
    org: { ...orgaIdOrNameOption, deprecated: 'organisation is now resolved automatically' },
    format: humanJsonOutputFormatOption,
  },
  args: [databaseIdArg],
  async handler(options, addonIdOrRealId) {
    const { format } = options;

    const { ownerId, addonId, realId } = await resolveAddon(addonIdOrRealId);

    const backups = await getBackups({ ownerId, ref: realId }).then(sendToApi);

    if (backups.length === 0 && format === 'human') {
      Logger.println('There are no backups yet');
      return;
    }

    const sortedBackups = backups.sort((a, b) => a.creation_date.localeCompare(b.creation_date));

    switch (format) {
      case 'json': {
        const formattedBackups = sortedBackups.map((backup) => {
          return {
            addonId: addonId,
            backupId: backup.backup_id,
            creationDate: backup.creation_date,
            downloadUrl: backup.download_url,
            ownerId: ownerId,
            realId: realId,
            status: backup.status,
          };
        });
        Logger.printJson(formattedBackups);
        break;
      }
      case 'human': {
        const formattedLines = sortedBackups.map((backup) => [backup.backup_id, backup.creation_date, backup.status]);

        const head = ['BACKUP ID', 'CREATION DATE', 'STATUS'];

        Logger.println(formatTable([head, ...formattedLines]));
      }
    }
  },
});