UNPKG

@ace-sdk/cli

Version:

ACE CLI - Command-line tool for intelligent pattern learning and playbook management

101 lines 3.66 kB
/** * Switch-org Command - Change default organization * @package @ace-sdk/cli */ import chalk from 'chalk'; import { getCurrentUser, isAuthenticated, setDefaultOrg, getEffectiveOrgId } from '@ace-sdk/core'; import { globalOptions } from '../cli.js'; export async function switchOrgCommand(orgNameOrId) { // Check if logged in if (!isAuthenticated()) { if (globalOptions.json) { console.log(JSON.stringify({ error: true, message: 'Not logged in' })); } else { console.log(chalk.yellow('Not logged in')); console.log(chalk.gray('Run'), chalk.white('ce-ace login'), chalk.gray('to authenticate')); } process.exit(1); } const user = getCurrentUser(); if (!user) { if (globalOptions.json) { console.log(JSON.stringify({ error: true, message: 'Cannot switch org with legacy token' })); } else { console.log(chalk.yellow('Cannot switch org with legacy token')); console.log(chalk.gray('Run'), chalk.white('ce-ace login'), chalk.gray('to upgrade to user authentication')); } process.exit(1); } const orgs = user.organizations || []; // Find org by name or ID (case-insensitive name matching) const targetOrg = orgs.find(o => o.org_id === orgNameOrId || o.name.toLowerCase() === orgNameOrId.toLowerCase()); if (!targetOrg) { if (globalOptions.json) { console.log(JSON.stringify({ error: true, message: `Organization '${orgNameOrId}' not found`, available: orgs.map(o => ({ name: o.name, id: o.org_id })) })); } else { console.log(chalk.red(`Organization '${orgNameOrId}' not found`)); console.log(); console.log(chalk.gray('Available organizations:')); for (const org of orgs) { console.log(` ${chalk.white(org.name)} ${chalk.gray(`(${org.org_id})`)}`); } } process.exit(1); } // Check if already default const currentOrgId = getEffectiveOrgId(); if (currentOrgId === targetOrg.org_id) { if (globalOptions.json) { console.log(JSON.stringify({ success: true, message: 'Already the default organization', org: { name: targetOrg.name, id: targetOrg.org_id } })); } else { console.log(chalk.yellow(`${targetOrg.name} is already the default organization`)); } return; } try { setDefaultOrg(targetOrg.org_id); if (globalOptions.json) { console.log(JSON.stringify({ success: true, message: `Switched to ${targetOrg.name}`, org: { name: targetOrg.name, id: targetOrg.org_id } })); } else { console.log(chalk.green('✓'), `Switched to ${chalk.cyan(targetOrg.name)}`); console.log(chalk.gray(' All subsequent requests will use this organization')); } } catch (error) { if (globalOptions.json) { console.error(JSON.stringify({ error: true, message: error instanceof Error ? error.message : String(error) })); } else { console.error(chalk.red('Failed to switch organization:'), error instanceof Error ? error.message : String(error)); } process.exit(1); } } //# sourceMappingURL=switch-org.js.map