import * as chalk from 'chalk'; import {command, help, namespace, option, param} from 'oo-cli'; import {die} from '../../lib/die'; import {formatError} from '../../lib/formatError'; import {Rivendell} from '../../lib/Rivendell'; import Webhook = Rivendell.Webhook; @namespace('directory') export class ListSourceFunctionsCommand { @param @help('The App ID (e.g. my_app)') public appId!: string; @param @help('The Tracker ID of the installed account') public trackerId!: string; @option('a') @help('The availability zone that will be targeted (default: us)') private availability: string = ''; @command('list-source-functions') @help('List the source functions exposed by an app installation') public async listSourceFunctions() { try { const appInstallation = await Rivendell.fetchAppInstallation(this.appId, this.trackerId, this.availability); const webhooks = await Rivendell.searchWebhooks({ appInstallId: appInstallation.id, includeInternal: true, includeDisabled: true }, this.availability); const sourceWebhooks = webhooks.filter((w) => { return w.attributes.dataSyncId?.length !== 0; }); this.render(sourceWebhooks); } catch (e: any) { die(formatError(e)); } } private render(webhooks: Webhook[]) { if (webhooks.length === 0) { console.log(chalk.yellow(`${this.appId} install for ${this.trackerId} does not have source functions.`)); } else { const longest = Math.max(...webhooks.map((webhook) => webhook.url.length)); webhooks.forEach((webhook) => { const attrs = webhook.attributes!; const info = []; info.push(`data-sync-id: ${attrs.dataSyncId}`); if (attrs.internal) { info.push('internal'); } if (!attrs.enabled) { info.push('disabled'); } if (info.length > 0) { console.log(webhook.url.padEnd(longest + 2), `(${info.join(', ')})`); } else { console.log(webhook.url); } }); } } }