import { Command } from "@oclif/core"; import chalk from "chalk"; import inquirer from "inquirer"; import axios from "axios"; import { getProject, getProjects } from "../../../utils/shared.js"; import { readAuthConfig } from "../../../utils/utils.js"; import type { Answers } from "../../app/create.js"; export default class DatabaseMongoStop extends Command { static description = "Stop an mongo from a project."; static examples = ["$ <%= config.bin %> mongo stop"]; public async run(): Promise { const auth = await readAuthConfig(this); console.log(chalk.blue.bold("\n Listing all Projects \n")); const projects = await getProjects(auth, this); const { project } = await inquirer.prompt([ { choices: projects.map((project) => ({ name: project.name, value: project, })), message: "Select a project to stop the mongo in:", name: "project", type: "list", }, ]); const projectId = project.projectId; const projectSelected = await getProject(projectId, auth, this); if (projectSelected.mongo.length === 0) { this.error(chalk.yellow("No mongo found in this project.")); } const appAnswers = await inquirer.prompt([ { // @ts-ignore choices: projectSelected.mongo.map((app) => ({ name: app.name, value: app.mongoId, })), message: "Select the mongo to stop:", name: "selectedApp", type: "list", }, ]); const mongoId = appAnswers.selectedApp; const confirmAnswers = await inquirer.prompt([ { default: false, message: "Are you sure you want to stop this mongo?", name: "confirmDelete", type: "confirm", }, ]); if (!confirmAnswers.confirmDelete) { this.error(chalk.yellow("mongo stop cancelled.")); } const response = await axios.post( `${auth.url}/api/trpc/mongo.stop`, { json: { mongoId, }, }, { headers: { Authorization: `Bearer ${auth.token}`, "Content-Type": "application/json", }, }, ); if (response.status !== 200) { this.error(chalk.red("Error stopping mongo")); } this.log(chalk.green("Mongo stop successful.")); } }