feat: add delete

This commit is contained in:
Mauricio Siu
2024-06-12 23:15:00 -06:00
parent 99451d416c
commit fbf6b7f5cc
10 changed files with 835 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
export default class DatabaseMariadbDelete extends Command {
static description = "Delete an application from a project.";
static examples = [
"$ <%= config.bin %> mariadb delete",
"$ <%= config.bin %> mariadb delete -p <projectId>",
];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMariadbDelete);
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the mariadb database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.mariadb.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.mariadb.map((app: any) => ({
name: app.name,
value: app.mariadbId,
})),
message: "Select the mariadb database to delete:",
name: "selectedApp",
type: "list",
},
]);
const mariadbId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mysql database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/mariadb.remove`,
{
json: {
mariadbId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
}
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}`));
}
}
}

View File

@@ -0,0 +1,153 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
export default class DatabaseMongoDelete extends Command {
static description = "Delete an application from a project.";
static examples = [
"$ <%= config.bin %> mongo delete",
"$ <%= config.bin %> mongo delete -p <projectId>",
];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMongoDelete);
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the mongo database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.mongo.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.mongo.map((app: any) => ({
name: app.name,
value: app.mongoId,
})),
message: "Select the mongo database to delete:",
name: "selectedApp",
type: "list",
},
]);
const mongoId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mongo database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/mongo.remove`,
{
json: {
mongoId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
}
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}`));
}
}
}

View File

@@ -0,0 +1,153 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
export default class DatabaseMysqlDelete extends Command {
static description = "Delete an application from a project.";
static examples = [
"$ <%= config.bin %> mysql delete",
"$ <%= config.bin %> mysql delete -p <projectId>",
];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseMysqlDelete);
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
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",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.mysql.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.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
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mysql database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/mysql.remove`,
{
json: {
mysqlId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
}
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}`));
}
}
}

View File

@@ -0,0 +1,153 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
export default class DatabasePostgresDelete extends Command {
static description = "Delete an application from a project.";
static examples = [
"$ <%= config.bin %> postgres delete",
"$ <%= config.bin %> postgres delete -p <projectId>",
];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabasePostgresDelete);
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the postgres database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.postgres.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.postgres.map((app: any) => ({
name: app.name,
value: app.postgresId,
})),
message: "Select the postgres database to delete:",
name: "selectedApp",
type: "list",
},
]);
const postgresId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this postgres database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/postgres.remove`,
{
json: {
postgresId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
}
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}`));
}
}
}

View File

@@ -0,0 +1,153 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import { readAuthConfig } from "../../../utils/utils.js";
export default class DatabaseRedisDelete extends Command {
static description = "Delete an application from a project.";
static examples = [
"$ <%= config.bin %> redis delete",
"$ <%= config.bin %> redis delete -p <projectId>",
];
static flags = {
projectId: Flags.string({
char: "p",
description: "ID of the project",
required: false,
}),
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(DatabaseRedisDelete);
let { projectId } = flags;
if (!projectId) {
// Obtener la lista de proyectos y permitir la selección
console.log(chalk.blue.bold("\n Listing all Projects \n"));
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
this.log(chalk.yellow("No projects found."));
return;
}
// Permitir al usuario seleccionar un proyecto
const answers = await inquirer.prompt([
{
choices: projects.map((project: any) => ({
name: project.name,
value: project.projectId,
})),
message: "Select a project to delete the mariadb database from:",
name: "selectedProject",
type: "list",
},
]);
projectId = answers.selectedProject;
} catch (error) {
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
}
try {
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
this.error(chalk.red("Error fetching applications"));
}
const apps = response.data.result.data.json;
if (apps.redis.length === 0) {
this.log(chalk.yellow("No applications found in this project."));
return;
}
// Permitir al usuario seleccionar una aplicación
const appAnswers = await inquirer.prompt([
{
choices: apps.redis.map((app: any) => ({
name: app.name,
value: app.redisId,
})),
message: "Select the redis database to delete:",
name: "selectedApp",
type: "list",
},
]);
const redisId = appAnswers.selectedApp;
// Confirmar eliminación
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to delete this mysql database?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.log(chalk.yellow("Application deletion cancelled."));
return;
}
// Eliminar la aplicación seleccionada
const deleteResponse = await axios.post(
`${auth.url}/api/trpc/redis.remove`,
{
json: {
redisId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (!deleteResponse.data.result.data.json) {
this.error(chalk.red("Error deleting application"));
}
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}`));
}
}
}

View File

@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
describe('database:mariadb:delete', () => {
it('runs database:mariadb:delete cmd', async () => {
const {stdout} = await runCommand('database:mariadb:delete')
expect(stdout).to.contain('hello world')
})
it('runs database:mariadb:delete --name oclif', async () => {
const {stdout} = await runCommand('database:mariadb:delete --name oclif')
expect(stdout).to.contain('hello oclif')
})
})

View File

@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
describe('database:mongo:delete', () => {
it('runs database:mongo:delete cmd', async () => {
const {stdout} = await runCommand('database:mongo:delete')
expect(stdout).to.contain('hello world')
})
it('runs database:mongo:delete --name oclif', async () => {
const {stdout} = await runCommand('database:mongo:delete --name oclif')
expect(stdout).to.contain('hello oclif')
})
})

View File

@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
describe('database:mysql:delete', () => {
it('runs database:mysql:delete cmd', async () => {
const {stdout} = await runCommand('database:mysql:delete')
expect(stdout).to.contain('hello world')
})
it('runs database:mysql:delete --name oclif', async () => {
const {stdout} = await runCommand('database:mysql:delete --name oclif')
expect(stdout).to.contain('hello oclif')
})
})

View File

@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
describe('database:postgres:delete', () => {
it('runs database:postgres:delete cmd', async () => {
const {stdout} = await runCommand('database:postgres:delete')
expect(stdout).to.contain('hello world')
})
it('runs database:postgres:delete --name oclif', async () => {
const {stdout} = await runCommand('database:postgres:delete --name oclif')
expect(stdout).to.contain('hello oclif')
})
})

View File

@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'
describe('database:redis:delete', () => {
it('runs database:redis:delete cmd', async () => {
const {stdout} = await runCommand('database:redis:delete')
expect(stdout).to.contain('hello world')
})
it('runs database:redis:delete --name oclif', async () => {
const {stdout} = await runCommand('database:redis:delete --name oclif')
expect(stdout).to.contain('hello oclif')
})
})