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

@@ -19,119 +19,212 @@ export default class DatabaseMysqlCreate extends Command {
description: "ID of the project",
required: false,
}),
name: Flags.string({
char: "n",
description: "Database name",
required: false,
}),
databaseName: Flags.string({
description: "MySQL database name",
required: false,
}),
description: Flags.string({
char: "d",
description: "Database description",
required: false,
}),
databaseRootPassword: Flags.string({
description: "Database root password",
required: false,
}),
databasePassword: Flags.string({
description: "Database password",
required: false,
}),
databaseUser: Flags.string({
description: "Database user",
default: "mysql",
}),
dockerImage: Flags.string({
description: "Docker image",
default: "mysql:8",
}),
skipConfirm: Flags.boolean({
char: "y",
description: "Skip confirmation prompt",
default: false,
}),
appName: Flags.string({
description: "App name",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMysqlCreate);
let {
projectId,
name,
databaseName,
description,
databaseRootPassword,
databasePassword,
databaseUser,
dockerImage,
appName
} = flags;
let { projectId } = flags;
if (!projectId) {
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !name || !databaseName || !appName || !databasePassword || !databaseRootPassword) {
console.log(chalk.blue.bold("\n Listing all Projects \n"));
const projects = await getProjects(auth, this);
const { project } = await inquirer.prompt<Answers>([
{
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to create the MySQL database in:",
name: "project",
type: "list",
},
]);
projectId = project.projectId;
const dbDetails = await inquirer.prompt([
{
message: "Enter the name:",
name: "name",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Database name:",
name: "databaseName",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
},
{
message: "Enter the database description (optional):",
name: "description",
type: "input",
},
{
message: "Database Root Password (optional):",
name: "databaseRootPassword",
type: "password",
},
{
message: "Database password (optional):",
name: "databasePassword",
type: "password",
},
{
default: "mysql:8",
message: "Docker Image (default: mysql:8):",
name: "dockerImage",
type: "input",
},
{
default: "mysql",
message: "Database User: (default: mysql):",
name: "databaseUser",
type: "input",
},
]);
const appName = await inquirer.prompt([
{
default: `${slugify(project.name)}-${dbDetails.name}`,
message: "Enter the App name:",
name: "appName",
type: "input",
validate: (input) => (input ? true : "App name is required"),
},
]);
try {
const response = await axios.post(
`${auth.url}/api/trpc/mysql.create`,
if (!projectId) {
const { project } = await inquirer.prompt<Answers>([
{
json: {
...dbDetails,
appName: appName.appName,
projectId: project.projectId,
},
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to create the MySQL instance in:",
name: "project",
type: "list",
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!response.data.result.data.json) {
this.error(chalk.red("Error creating MySQL database"));
}
this.log(
chalk.green(
`MySQL database '${dbDetails.name}' created successfully.`,
),
);
} catch (error) {
this.error(
// @ts-ignore
chalk.red(`Failed to create MySQL database: ${error.message}`),
);
]);
projectId = project.projectId;
}
if (!name || !databaseName || !appName || !databasePassword || !databaseRootPassword) {
const dbDetails = await inquirer.prompt([
{
message: "Enter the name:",
name: "name",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
default: name,
},
{
message: "Database name:",
name: "databaseName",
type: "input",
validate: (input) => (input ? true : "Database name is required"),
default: databaseName,
},
{
message: "Enter the database description (optional):",
name: "description",
type: "input",
default: description,
},
{
message: "Database Root Password:",
name: "databaseRootPassword",
type: "password",
default: databaseRootPassword,
},
{
message: "Database password:",
name: "databasePassword",
type: "password",
default: databasePassword,
},
{
default: dockerImage || "mysql:8",
message: "Docker Image (default: mysql:8):",
name: "dockerImage",
type: "input",
},
{
default: databaseUser || "mysql",
message: "Database User: (default: mysql):",
name: "databaseUser",
type: "input",
},
]);
name = dbDetails.name;
databaseName = dbDetails.databaseName;
description = dbDetails.description;
databaseRootPassword = dbDetails.databaseRootPassword;
databasePassword = dbDetails.databasePassword;
dockerImage = dbDetails.dockerImage;
databaseUser = dbDetails.databaseUser;
const appNamePrompt = await inquirer.prompt([
{
default: appName || `${slugify(name)}`,
message: "Enter the App name:",
name: "appName",
type: "input",
validate: (input) => (input ? true : "App name is required"),
},
]);
appName = appNamePrompt.appName;
}
}
// Confirmar si no se especifica --skipConfirm
if (!flags.skipConfirm) {
const confirm = await inquirer.prompt([
{
type: 'confirm',
name: 'proceed',
message: 'Do you want to create this MySQL instance?',
default: false,
},
]);
if (!confirm.proceed) {
this.error(chalk.yellow("MySQL creation cancelled."));
return;
}
}
try {
console.log(JSON.stringify({
name,
databaseName,
description,
databaseRootPassword,
databasePassword,
databaseUser,
dockerImage,
appName,
projectId,
}, null, 2));
const response = await axios.post(
`${auth.url}/api/trpc/mysql.create`,
{
json: {
name,
databaseName,
description,
databaseRootPassword,
databasePassword,
databaseUser,
dockerImage,
appName,
projectId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!response.data.result.data.json) {
this.error(chalk.red("Error creating MySQL instance", response.data.result.data.json));
}
this.log(chalk.green(`MySQL instance '${name}' created successfully.`));
} catch (error: any) {
this.error(chalk.red(`Error creating MySQL instance: ${error.message}`));
}
}
}

View File

@@ -20,79 +20,84 @@ export default class DatabaseMysqlDelete extends Command {
description: "ID of the project",
required: false,
}),
mysqlId: Flags.string({
char: "i",
description: "ID of the MySQL database",
required: false,
}),
skipConfirm: Flags.boolean({
char: "y",
description: "Skip confirmation",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMysqlDelete);
let { projectId } = flags;
let { projectId, mysqlId } = flags;
if (!projectId) {
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !mysqlId) {
console.log(chalk.blue.bold("\n Listing all Projects \n"));
const projects = await getProjects(auth, this);
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
if (!projectId) {
const answers = await inquirer.prompt([
{
choices: projects.map((project) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the MySQL instance from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
}
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the MySQL database from:",
name: "selectedProject",
type: "list",
},
]);
const projectSelected = await getProject(projectId, auth, this);
projectId = answers.selectedProject;
if (!projectSelected.mysql || projectSelected.mysql.length === 0) {
this.error(chalk.yellow("No MySQL instances found in this project."));
}
if (!mysqlId) {
const dbAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mysql.map((db) => ({
name: db.name,
value: db.mysqlId,
})),
message: "Select the MySQL instance to delete:",
name: "selectedDb",
type: "list",
},
]);
mysqlId = dbAnswers.selectedDb;
}
}
try {
const project = await getProject(projectId, auth, this);
if (!project.mysql || project.mysql.length === 0) {
this.log(chalk.yellow("No MySQL databases found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: project.mysql.map((app: any) => ({
name: app.name,
value: app.mysqlId,
})),
message: "Select the MySQL database to delete:",
name: "selectedApp",
type: "list",
},
]);
const mysqlId = appAnswers.selectedApp;
// Confirmar eliminación
// Confirmar si no se especifica --skipConfirm
if (!flags.skipConfirm) {
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mysql database?",
message: "Are you sure you want to delete this MySQL instance?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
this.error(chalk.yellow("MySQL deletion cancelled."));
}
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
try {
const response = await axios.post(
`${auth.url}/api/trpc/mysql.remove`,
{
json: {
@@ -107,14 +112,12 @@ export default class DatabaseMysqlDelete extends Command {
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
if (!response.data.result.data.json) {
this.error(chalk.red("Error deleting MySQL instance"));
}
this.log(chalk.green("Application deleted successfully."));
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to delete application: ${error.message}`));
this.log(chalk.green("MySQL instance deleted successfully."));
} catch (error: any) {
this.error(chalk.red(`Error deleting MySQL instance: ${error.message}`));
}
}
}

View File

@@ -13,77 +13,90 @@ export default class DatabaseMysqlDeploy extends Command {
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMysqlDeploy);
let { projectId, mysqlId } = flags;
console.log(chalk.blue.bold("\n Listing all Projects \n"));
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !mysqlId) {
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 MySQL 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 deploy the mysql in:",
name: "project",
type: "list",
},
]);
const projectSelected = await getProject(projectId, auth, this);
const projectId = project.projectId;
if (projectSelected.mysql.length === 0) {
this.error(chalk.yellow("No MySQL instances found in this project."));
}
const projectSelected = await getProject(projectId, auth, this);
if (projectSelected.mysql.length === 0) {
this.error(chalk.yellow("No mysql found in this project."));
if (!mysqlId) {
const dbAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mysql.map((db) => ({
name: db.name,
value: db.mysqlId,
})),
message: "Select the MySQL instance to deploy:",
name: "selectedDb",
type: "list",
},
]);
mysqlId = dbAnswers.selectedDb;
}
}
const appAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mysql.map((app) => ({
name: app.name,
value: app.mysqlId,
})),
message: "Select the mysql to deploy:",
name: "selectedApp",
type: "list",
},
]);
const mysqlId = appAnswers.selectedApp;
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to deploy this mysql?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.error(chalk.yellow("mysql deployment cancelled."));
}
const response = await axios.post(
`${auth.url}/api/trpc/mysql.deploy`,
{
json: {
mysqlId,
// 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 MySQL instance?",
name: "confirmDeploy",
type: "confirm",
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
]);
if (response.status !== 200) {
this.error(chalk.red("Error deploying mysql"));
if (!confirmAnswers.confirmDeploy) {
this.error(chalk.yellow("MySQL deployment cancelled."));
}
}
try {
const response = await axios.post(
`${auth.url}/api/trpc/mysql.deploy`,
{
json: {
mysqlId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (response.status !== 200) {
this.error(chalk.red("Error deploying MySQL instance"));
}
this.log(chalk.green("MySQL instance deployed successfully."));
} catch (error: any) {
this.error(chalk.red(`Error deploying MySQL instance: ${error.message}`));
}
this.log(chalk.green("Mysql deployed successful."));
}
}

View File

@@ -13,77 +13,90 @@ export default class DatabaseMysqlStop extends Command {
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMysqlStop);
let { projectId, mysqlId } = flags;
console.log(chalk.blue.bold("\n Listing all Projects \n"));
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !mysqlId) {
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 MySQL 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 mysql in:",
name: "project",
type: "list",
},
]);
const projectSelected = await getProject(projectId, auth, this);
const projectId = project.projectId;
if (projectSelected.mysql.length === 0) {
this.error(chalk.yellow("No MySQL instances found in this project."));
}
const projectSelected = await getProject(projectId, auth, this);
if (projectSelected.mysql.length === 0) {
this.error(chalk.yellow("No mysql found in this project."));
if (!mysqlId) {
const dbAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mysql.map((db) => ({
name: db.name,
value: db.mysqlId,
})),
message: "Select the MySQL instance to stop:",
name: "selectedDb",
type: "list",
},
]);
mysqlId = dbAnswers.selectedDb;
}
}
const appAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.mysql.map((app) => ({
name: app.name,
value: app.mysqlId,
})),
message: "Select the mysql to stop:",
name: "selectedApp",
type: "list",
},
]);
const mysqlId = appAnswers.selectedApp;
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to stop this mysql?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.error(chalk.yellow("mysql stop cancelled."));
}
const response = await axios.post(
`${auth.url}/api/trpc/mysql.stop`,
{
json: {
mysqlId,
// 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 MySQL instance?",
name: "confirmStop",
type: "confirm",
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
]);
if (response.status !== 200) {
this.error(chalk.red("Error stopping mysql"));
if (!confirmAnswers.confirmStop) {
this.error(chalk.yellow("MySQL stop cancelled."));
}
}
try {
const response = await axios.post(
`${auth.url}/api/trpc/mysql.stop`,
{
json: {
mysqlId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (response.status !== 200) {
this.error(chalk.red("Error stopping MySQL instance"));
}
this.log(chalk.green("MySQL instance stopped successfully."));
} catch (error: any) {
this.error(chalk.red(`Error stopping MySQL instance: ${error.message}`));
}
this.log(chalk.green("Mysql stop successful."));
}
}