@pompeii-labs/cli
Version:
Magma CLI
84 lines (83 loc) • 3.29 kB
JavaScript
import chalk from "chalk";
import fs from "fs";
import path from "path";
import { setEnv, getEnv, unsetEnv } from "../api.js";
import { magma } from "../ui.js";
async function getEnvCmd(key) {
try {
if (!fs.existsSync(".magma/.cache")) {
throw new Error(
"No magma cache found in the current directory. Have you deployed your agent yet?"
);
}
const config = JSON.parse(fs.readFileSync(".magma/.cache", "utf8"));
const value = await getEnv(config.id, key);
if (!value) {
console.log(`${key} not found`);
}
console.log(chalk.green(`${key}=${value}`));
} catch (error) {
console.error(chalk.red(`Failed to get environment variable: ${error.message}`));
process.exit(1);
}
}
async function setEnvCmd(keyValue) {
try {
if (!fs.existsSync(".magma/.cache")) {
throw new Error(
"No magma cache found in the current directory. Have you deployed your agent yet?"
);
}
const config = JSON.parse(fs.readFileSync(".magma/.cache", "utf8"));
let env = [];
if (keyValue) {
const [key, ...valueParts] = keyValue.split("=");
const value = valueParts.join("=");
if (!value) {
console.error(chalk.red("Invalid format. Use KEY=VALUE"));
process.exit(1);
}
env = [{ key, value }];
} else {
const envContents = fs.readFileSync(path.join(process.cwd(), ".env"), "utf8");
env = envContents.split("\n").map((f) => f.trim()).filter((f) => !!f && !f.startsWith("#")).map((f) => f.split("=")).map(([key, value]) => ({ key, value }));
}
await setEnv(config.id, env);
console.log(chalk.green(`Successfully set ${env.length} environment variables`));
console.log();
console.log(chalk.dim(env.map((e) => `${e.key}=${e.value}`).join("\n")));
console.log();
console.log(
chalk.green(
`To apply these changes, you must redeploy your agent using ${magma("magma deploy")}`
)
);
} catch (error) {
console.error(chalk.red(`Failed to set environment variable: ${error.message}`));
process.exit(1);
}
}
async function unsetEnvCmd(key) {
try {
if (!fs.existsSync(".magma/.cache")) {
throw new Error(
"No magma cache found in the current directory. Have you deployed your agent yet?"
);
}
const config = JSON.parse(fs.readFileSync(".magma/.cache", "utf8"));
await unsetEnv(config.id, key);
console.log(chalk.green(`Successfully unset environment variable ${key}`));
} catch (error) {
console.error(chalk.red(`Failed to unset environment variable: ${error.message}`));
process.exit(1);
}
}
function envCommand(program) {
const env = program.command("env").description("Manage environment variables for your Magma agents");
env.command("set").argument("[key_value]", "Key value pair in format KEY=VALUE").description("Set an environment variable. If no argument, lists all variables").action(setEnvCmd);
env.command("get").argument("<key>", "The environment variable key to retrieve").description("Get the value of an environment variable").action(getEnvCmd);
env.command("unset").argument("<key>", "The environment variable key to unset").description("Unset an environment variable").action(unsetEnvCmd);
}
export {
envCommand
};