mirror of
https://github.com/Dokploy/cli.git
synced 2026-06-25 09:05:20 +02:00
feat: enhance CLI commands with improved flags and error handling
This commit introduces several improvements across multiple CLI commands: - Added new flags for more flexible command usage - Implemented interactive mode for commands when flags are not provided - Improved error handling and messaging - Added support for environment variable authentication - Standardized confirmation prompts and error messages - Enhanced type safety with TypeScript improvements
This commit is contained in:
@@ -1,89 +1,120 @@
|
||||
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 { Command, Flags } from "@oclif/core";
|
||||
import { readAuthConfig } from "../../../utils/utils.js";
|
||||
import chalk from "chalk";
|
||||
import { getProject, getProjects } from "../../../utils/shared.js";
|
||||
import inquirer from "inquirer";
|
||||
import type { Answers } from "../../app/create.js";
|
||||
import axios from "axios";
|
||||
|
||||
export default class DatabasePostgresStop extends Command {
|
||||
static description = "Stop an postgres from a project.";
|
||||
static description = "Stop a PostgreSQL instance in a project.";
|
||||
|
||||
static examples = ["$ <%= config.bin %> postgres stop"];
|
||||
|
||||
static flags = {
|
||||
projectId: Flags.string({
|
||||
char: "p",
|
||||
description: "ID of the project",
|
||||
required: false,
|
||||
}),
|
||||
postgresId: Flags.string({
|
||||
char: "d",
|
||||
description: "ID of the PostgreSQL instance to stop",
|
||||
required: false,
|
||||
}),
|
||||
skipConfirm: Flags.boolean({
|
||||
char: "y",
|
||||
description: "Skip confirmation prompt",
|
||||
default: false,
|
||||
}),
|
||||
};
|
||||
|
||||
public async run(): Promise<void> {
|
||||
const auth = await readAuthConfig(this);
|
||||
const { flags } = await this.parse(DatabasePostgresStop);
|
||||
let { projectId, postgresId } = flags;
|
||||
|
||||
console.log(chalk.blue.bold("\n Listing all Projects \n"));
|
||||
// Modo interactivo si no se proporcionan los flags necesarios
|
||||
if (!projectId || !postgresId) {
|
||||
console.log(chalk.blue.bold("\n Listing all Projects \n"));
|
||||
const projects = await getProjects(auth, this);
|
||||
|
||||
const projects = await getProjects(auth, this);
|
||||
if (!projectId) {
|
||||
const { project } = await inquirer.prompt<Answers>([
|
||||
{
|
||||
choices: projects.map((project) => ({
|
||||
name: project.name,
|
||||
value: project,
|
||||
})),
|
||||
message: "Select a project to stop the PostgreSQL instance from:",
|
||||
name: "project",
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
projectId = project.projectId;
|
||||
}
|
||||
|
||||
const { project } = await inquirer.prompt<Answers>([
|
||||
{
|
||||
choices: projects.map((project) => ({
|
||||
name: project.name,
|
||||
value: project,
|
||||
})),
|
||||
message: "Select a project to stop the postgres in:",
|
||||
name: "project",
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
const projectSelected = await getProject(projectId, auth, this);
|
||||
|
||||
const projectId = project.projectId;
|
||||
if (projectSelected.postgres.length === 0) {
|
||||
this.error(chalk.yellow("No PostgreSQL instances found in this project."));
|
||||
}
|
||||
|
||||
const projectSelected = await getProject(projectId, auth, this);
|
||||
|
||||
if (projectSelected.postgres.length === 0) {
|
||||
this.error(chalk.yellow("No postgres found in this project."));
|
||||
if (!postgresId) {
|
||||
const dbAnswers = await inquirer.prompt([
|
||||
{
|
||||
// @ts-ignore
|
||||
choices: projectSelected.postgres.map((db) => ({
|
||||
name: db.name,
|
||||
value: db.postgresId,
|
||||
})),
|
||||
message: "Select the PostgreSQL instance to stop:",
|
||||
name: "selectedDb",
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
postgresId = dbAnswers.selectedDb;
|
||||
}
|
||||
}
|
||||
|
||||
const appAnswers = await inquirer.prompt([
|
||||
{
|
||||
// @ts-ignore
|
||||
choices: projectSelected.postgres.map((app) => ({
|
||||
name: app.name,
|
||||
value: app.postgresId,
|
||||
})),
|
||||
message: "Select the postgres to stop:",
|
||||
name: "selectedApp",
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
|
||||
const postgresId = appAnswers.selectedApp;
|
||||
|
||||
const confirmAnswers = await inquirer.prompt([
|
||||
{
|
||||
default: false,
|
||||
message: "Are you sure you want to stop this postgres?",
|
||||
name: "confirmDelete",
|
||||
type: "confirm",
|
||||
},
|
||||
]);
|
||||
|
||||
if (!confirmAnswers.confirmDelete) {
|
||||
this.error(chalk.yellow("postgres stop cancelled."));
|
||||
}
|
||||
|
||||
const response = await axios.post(
|
||||
`${auth.url}/api/trpc/postgres.stop`,
|
||||
{
|
||||
json: {
|
||||
postgresId,
|
||||
// Confirmar si no se especifica --skipConfirm
|
||||
if (!flags.skipConfirm) {
|
||||
const confirmAnswers = await inquirer.prompt([
|
||||
{
|
||||
default: false,
|
||||
message: "Are you sure you want to stop this PostgreSQL instance?",
|
||||
name: "confirmStop",
|
||||
type: "confirm",
|
||||
},
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${auth.token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
]);
|
||||
|
||||
if (response.status !== 200) {
|
||||
this.error(chalk.red("Error stopping postgres"));
|
||||
if (!confirmAnswers.confirmStop) {
|
||||
this.error(chalk.yellow("PostgreSQL stop cancelled."));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${auth.url}/api/trpc/postgres.stop`,
|
||||
{
|
||||
json: {
|
||||
postgresId,
|
||||
},
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${auth.token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (response.status !== 200) {
|
||||
this.error(chalk.red("Error stopping PostgreSQL instance"));
|
||||
}
|
||||
this.log(chalk.green("PostgreSQL instance stopped successfully."));
|
||||
} catch (error: any) {
|
||||
this.error(chalk.red(`Error stopping PostgreSQL instance: ${error.message}`));
|
||||
}
|
||||
this.log(chalk.green("Postgres stop successful."));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user