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:
Mauricio Siu
2025-02-23 21:29:45 -06:00
parent 7cb5369f15
commit 10cfe23763
26 changed files with 2483 additions and 1621 deletions

View File

@@ -1,4 +1,4 @@
import { Command } from "@oclif/core";
import { Command, Flags } from "@oclif/core";
import { readAuthConfig } from "../../../utils/utils.js";
import chalk from "chalk";
import { getProject, getProjects } from "../../../utils/shared.js";
@@ -11,79 +11,110 @@ export default class DatabaseMariadbDeploy extends Command {
static examples = ["$ <%= config.bin %> app deploy"];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
mariadbId: Flags.string({
char: "m",
description: "ID of the MariaDB instance to deploy",
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(DatabaseMariadbDeploy);
let { projectId, mariadbId } = flags;
console.log(chalk.blue.bold("\n Listing all Projects \n"));
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !mariadbId) {
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 deploy the MariaDB in:",
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 deploy the mariadb in:",
name: "project",
type: "list",
},
]);
const projectSelected = await getProject(projectId, auth, this);
const projectId = project.projectId;
if (projectSelected.mariadb.length === 0) {
this.error(chalk.yellow("No MariaDB instances found in this project."));
}
const projectSelected = await getProject(projectId, auth, this);
if (projectSelected.mariadb.length === 0) {
this.error(chalk.yellow("No mariadb found in this project."));
if (!mariadbId) {
const dbAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mariadb.map((db) => ({
name: db.name,
value: db.mariadbId,
})),
message: "Select the MariaDB instance to deploy:",
name: "selectedDb",
type: "list",
},
]);
mariadbId = dbAnswers.selectedDb;
}
}
const appAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mariadb.map((app) => ({
name: app.name,
value: app.mariadbId,
})),
message: "Select the mariadb to deploy:",
name: "selectedApp",
type: "list",
},
]);
const mariadbId = appAnswers.selectedApp;
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to deploy this mariadb?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.error(chalk.yellow("mariadb deployment cancelled."));
}
const response = await axios.post(
`${auth.url}/api/trpc/mariadb.deploy`,
{
json: {
mariadbId,
// Confirmar si no se especifica --skipConfirm
if (!flags.skipConfirm) {
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to deploy this MariaDB instance?",
name: "confirmDeploy",
type: "confirm",
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
]);
if (response.status !== 200) {
this.error(chalk.red("Error deploying mariadb"));
if (!confirmAnswers.confirmDeploy) {
this.error(chalk.yellow("MariaDB deployment cancelled."));
}
}
try {
const response = await axios.post(
`${auth.url}/api/trpc/mariadb.deploy`,
{
json: {
mariadbId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (response.status !== 200) {
this.error(chalk.red("Error deploying MariaDB instance"));
}
this.log(chalk.green("MariaDB instance deployed successfully."));
} catch (error: any) {
this.error(chalk.red(`Error deploying MariaDB instance: ${error.message}`));
}
this.log(chalk.green("Mariadb deploy successful."));
}
}