UNPKG

@ably/cli

Version:

Ably CLI for Pub/Sub, Chat and Spaces

55 lines (54 loc) 2 kB
import { Args } from "@oclif/core"; import { ControlBaseCommand } from "../../control-base-command.js"; export default class AppsSwitch extends ControlBaseCommand { static args = { appId: Args.string({ description: "ID of the app to switch to", required: false, }), }; static description = "Switch to a different Ably app"; static examples = [ "<%= config.bin %> <%= command.id %> APP_ID", "<%= config.bin %> <%= command.id %>", ]; static flags = { ...ControlBaseCommand.globalFlags, }; async run() { const { args } = await this.parse(AppsSwitch); const controlApi = this.createControlApi({}); // If app ID is provided, switch directly if (args.appId) { await this.switchToApp(args.appId, controlApi); return; } // Otherwise, show interactive selection this.log("Select an app to switch to:"); const selectedApp = await this.interactiveHelper.selectApp(controlApi); if (selectedApp) { // Save the app info and set as current this.configManager.setCurrentApp(selectedApp.id); this.configManager.storeAppInfo(selectedApp.id, { appName: selectedApp.name, }); this.log(`Switched to app: ${selectedApp.name} (${selectedApp.id})`); } else { this.log("App switch cancelled."); } } async switchToApp(appId, controlApi) { try { // Verify the app exists const app = await controlApi.getApp(appId); // Save app info and set as current this.configManager.setCurrentApp(appId); this.configManager.storeAppInfo(appId, { appName: app.name }); this.log(`Switched to app: ${app.name} (${app.id})`); } catch { this.error(`App with ID "${appId}" not found or access denied.`); } } }