@pompeii-labs/cli
Version:
Magma CLI
52 lines (51 loc) • 1.7 kB
JavaScript
import { getOrgs } from "../api.js";
import { select } from "@inquirer/prompts";
import chalk from "chalk";
import { storeAuthSession, getAuthSession } from "../auth.js";
async function switchOrg() {
try {
const authSession = await getAuthSession();
if (!authSession) {
console.error(chalk.red("Not authenticated with Magma. Please run `magma login`."));
process.exit(1);
}
const orgs = await getOrgs();
if (orgs.length === 0) {
console.error(
chalk.red(
"No organizations found for this account. Please create an organization on magmadeploy.com and try again."
)
);
process.exit(1);
}
console.log(chalk.dim(`Current organization: ${chalk.bold(authSession.org.name)}
`));
const orgId = await select({
message: "Select an organization to use:",
choices: orgs.map((org2) => ({
name: org2.name,
value: org2.id
}))
});
if (!orgId) {
console.error(chalk.red("Failed to select an organization. Please try again."));
process.exit(1);
}
const org = orgs.find((org2) => org2.id === orgId);
if (!org) {
console.error(chalk.red("Failed to select an organization. Please try again."));
process.exit(1);
}
await storeAuthSession({ ...authSession, org });
console.log(chalk.green(`\u{1F30B} Successfully switched to ${chalk.bold(org.name)}`));
} catch (error) {
console.error(chalk.red("Failed to switch organization. Please try again."));
process.exit(1);
}
}
function switchCommand(program) {
program.command("switch").description("Switch to a different organization.").action(switchOrg);
}
export {
switchCommand
};