@pompeii-labs/cli
Version:
Magma CLI
54 lines (53 loc) • 1.63 kB
JavaScript
import chalk from "chalk";
import { isAuthenticated } from "../auth.js";
import { linkGithubRepo, listGithubRepos } from "../api.js";
import { select } from "@inquirer/prompts";
import fs from "fs";
function linkCommand(program) {
program.command("link").description(
"Link an agent to a GitHub repository. This will allow automations like agent autodeployments when you make changes to your repo!"
).action(async () => {
try {
if (!isAuthenticated()) {
console.log(
chalk.red("Not authenticated with Magma. Please run `magma login`.")
);
process.exit(1);
}
if (!fs.existsSync(".magma/.cache")) {
console.log(
chalk.red(
"This agent has not been deployed. Please run `magma deploy` first."
)
);
process.exit(1);
}
const cache = JSON.parse(fs.readFileSync(".magma/.cache", "utf8"));
const repos = await listGithubRepos();
const selectedRepo = await select({
message: "Select a repository to link:",
choices: repos.map((repo) => ({
name: repo.full_name,
value: repo.full_name,
private: repo.private
})),
pageSize: 15,
loop: false
});
await linkGithubRepo(
repos.find((repo) => repo.full_name === selectedRepo),
cache.id
);
console.log(chalk.green(`
Successfully linked ${selectedRepo}!`));
process.exit(0);
} catch (error) {
console.error(chalk.red(`
\u274C Failed to login: ${error.message}`));
process.exit(1);
}
});
}
export {
linkCommand
};