import * as chalk from 'chalk'; import {command, help, namespace, option, param} from 'oo-cli'; import * as table from 'text-table'; import {die} from '../../lib/die'; import {styledStringLength} from '../../lib/StringUtils'; import {Rivendell} from '../../lib/Rivendell'; import {formatError} from '../../lib/formatError'; import SortDirection = Rivendell.SortDirection; import AppInstallation = Rivendell.AppInstallation; @namespace('directory') export class ListInstallsCommand { @param @help('The App ID and optional version (e.g. my_app or my_app@1.0.0)') public appVersion!: string; @option('a') @help('The availability zone that will be targeted (default: us)') private availability: string = ''; @command('list-installs') @help('List installations of a specific app version') public async listInstalls() { const [appId, version] = this.appVersion.split('@'); try { const appInstallations = await Rivendell.searchAppInstallations({ appId, version, sorts: [{ field: 'trackerId', direction: SortDirection.DESC }] }, this.availability ); this.render(version, appInstallations); } catch (e: any) { die(formatError(e)); } } private render(version: string, appInstallations: AppInstallation[]) { const header = this.getHeaders(version).map((item) => chalk.grey(item)); const rows = appInstallations.map((install) => this.getRow(install, version)); const t = table( [ header, ...rows ], { hsep: ' '.repeat(8), stringLength: styledStringLength } ); console.log(`\n${t}\n`); } private getHeaders(version: string): string[] { const headers = ['Tracker', 'Created At', 'Updated At']; if (!version) { headers.splice(1, 0, 'Version'); } return headers; } private getRow(install: AppInstallation, version: string): Array<{}> { const row = [install.trackerId, install.createdAt, install.updatedAt]; if (!version) { row.splice(1, 0, install.version); } return row; } }