Skip to content

Commit fbf6b7f

Browse files
committed
feat: add delete
1 parent 99451d4 commit fbf6b7f

File tree

10 files changed

+835
-0
lines changed

10 files changed

+835
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { Command, Flags } from "@oclif/core";
2+
import axios from "axios";
3+
import chalk from "chalk";
4+
import inquirer from "inquirer";
5+
6+
import { readAuthConfig } from "../../../utils/utils.js";
7+
8+
export default class DatabaseMariadbDelete extends Command {
9+
static description = "Delete an application from a project.";
10+
11+
static examples = [
12+
"$ <%= config.bin %> mariadb delete",
13+
"$ <%= config.bin %> mariadb delete -p <projectId>",
14+
];
15+
16+
static flags = {
17+
projectId: Flags.string({
18+
char: "p",
19+
description: "ID of the project",
20+
required: false,
21+
}),
22+
};
23+
24+
public async run(): Promise<void> {
25+
const auth = await readAuthConfig(this);
26+
27+
const { flags } = await this.parse(DatabaseMariadbDelete);
28+
let { projectId } = flags;
29+
30+
if (!projectId) {
31+
// Obtener la lista de proyectos y permitir la selección
32+
console.log(chalk.blue.bold("\n Listing all Projects \n"));
33+
34+
try {
35+
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
36+
headers: {
37+
Authorization: `Bearer ${auth.token}`,
38+
"Content-Type": "application/json",
39+
},
40+
});
41+
42+
if (!response.data.result.data.json) {
43+
this.error(chalk.red("Error fetching projects"));
44+
}
45+
46+
const projects = response.data.result.data.json;
47+
48+
if (projects.length === 0) {
49+
this.log(chalk.yellow("No projects found."));
50+
return;
51+
}
52+
53+
// Permitir al usuario seleccionar un proyecto
54+
const answers = await inquirer.prompt([
55+
{
56+
choices: projects.map((project: any) => ({
57+
name: project.name,
58+
value: project.projectId,
59+
})),
60+
message: "Select a project to delete the mariadb database from:",
61+
name: "selectedProject",
62+
type: "list",
63+
},
64+
]);
65+
66+
projectId = answers.selectedProject;
67+
} catch (error) {
68+
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
69+
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
70+
}
71+
}
72+
73+
try {
74+
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
75+
headers: {
76+
Authorization: `Bearer ${auth.token}`,
77+
"Content-Type": "application/json",
78+
},
79+
params: {
80+
input: JSON.stringify({
81+
json: { projectId },
82+
}),
83+
},
84+
});
85+
86+
if (!response.data.result.data.json) {
87+
this.error(chalk.red("Error fetching applications"));
88+
}
89+
90+
const apps = response.data.result.data.json;
91+
92+
if (apps.mariadb.length === 0) {
93+
this.log(chalk.yellow("No applications found in this project."));
94+
return;
95+
}
96+
97+
// Permitir al usuario seleccionar una aplicación
98+
const appAnswers = await inquirer.prompt([
99+
{
100+
choices: apps.mariadb.map((app: any) => ({
101+
name: app.name,
102+
value: app.mariadbId,
103+
})),
104+
message: "Select the mariadb database to delete:",
105+
name: "selectedApp",
106+
type: "list",
107+
},
108+
]);
109+
110+
const mariadbId = appAnswers.selectedApp;
111+
112+
// Confirmar eliminación
113+
const confirmAnswers = await inquirer.prompt([
114+
{
115+
default: false,
116+
message: "Are you sure you want to delete this mysql database?",
117+
name: "confirmDelete",
118+
type: "confirm",
119+
},
120+
]);
121+
122+
if (!confirmAnswers.confirmDelete) {
123+
this.log(chalk.yellow("Application deletion cancelled."));
124+
return;
125+
}
126+
127+
// Eliminar la aplicación seleccionada
128+
const deleteResponse = await axios.post(
129+
`${auth.url}/api/trpc/mariadb.remove`,
130+
{
131+
json: {
132+
mariadbId,
133+
},
134+
},
135+
{
136+
headers: {
137+
Authorization: `Bearer ${auth.token}`,
138+
"Content-Type": "application/json",
139+
},
140+
},
141+
);
142+
143+
if (!deleteResponse.data.result.data.json) {
144+
this.error(chalk.red("Error deleting application"));
145+
}
146+
147+
this.log(chalk.green("Application deleted successfully."));
148+
} catch (error) {
149+
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
150+
this.error(chalk.red(`Failed to delete application: ${error.message}`));
151+
}
152+
}
153+
}

src/commands/database/mongo/delete.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { Command, Flags } from "@oclif/core";
2+
import axios from "axios";
3+
import chalk from "chalk";
4+
import inquirer from "inquirer";
5+
6+
import { readAuthConfig } from "../../../utils/utils.js";
7+
8+
export default class DatabaseMongoDelete extends Command {
9+
static description = "Delete an application from a project.";
10+
11+
static examples = [
12+
"$ <%= config.bin %> mongo delete",
13+
"$ <%= config.bin %> mongo delete -p <projectId>",
14+
];
15+
16+
static flags = {
17+
projectId: Flags.string({
18+
char: "p",
19+
description: "ID of the project",
20+
required: false,
21+
}),
22+
};
23+
24+
public async run(): Promise<void> {
25+
const auth = await readAuthConfig(this);
26+
27+
const { flags } = await this.parse(DatabaseMongoDelete);
28+
let { projectId } = flags;
29+
30+
if (!projectId) {
31+
// Obtener la lista de proyectos y permitir la selección
32+
console.log(chalk.blue.bold("\n Listing all Projects \n"));
33+
34+
try {
35+
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
36+
headers: {
37+
Authorization: `Bearer ${auth.token}`,
38+
"Content-Type": "application/json",
39+
},
40+
});
41+
42+
if (!response.data.result.data.json) {
43+
this.error(chalk.red("Error fetching projects"));
44+
}
45+
46+
const projects = response.data.result.data.json;
47+
48+
if (projects.length === 0) {
49+
this.log(chalk.yellow("No projects found."));
50+
return;
51+
}
52+
53+
// Permitir al usuario seleccionar un proyecto
54+
const answers = await inquirer.prompt([
55+
{
56+
choices: projects.map((project: any) => ({
57+
name: project.name,
58+
value: project.projectId,
59+
})),
60+
message: "Select a project to delete the mongo database from:",
61+
name: "selectedProject",
62+
type: "list",
63+
},
64+
]);
65+
66+
projectId = answers.selectedProject;
67+
} catch (error) {
68+
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
69+
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
70+
}
71+
}
72+
73+
try {
74+
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
75+
headers: {
76+
Authorization: `Bearer ${auth.token}`,
77+
"Content-Type": "application/json",
78+
},
79+
params: {
80+
input: JSON.stringify({
81+
json: { projectId },
82+
}),
83+
},
84+
});
85+
86+
if (!response.data.result.data.json) {
87+
this.error(chalk.red("Error fetching applications"));
88+
}
89+
90+
const apps = response.data.result.data.json;
91+
92+
if (apps.mongo.length === 0) {
93+
this.log(chalk.yellow("No applications found in this project."));
94+
return;
95+
}
96+
97+
// Permitir al usuario seleccionar una aplicación
98+
const appAnswers = await inquirer.prompt([
99+
{
100+
choices: apps.mongo.map((app: any) => ({
101+
name: app.name,
102+
value: app.mongoId,
103+
})),
104+
message: "Select the mongo database to delete:",
105+
name: "selectedApp",
106+
type: "list",
107+
},
108+
]);
109+
110+
const mongoId = appAnswers.selectedApp;
111+
112+
// Confirmar eliminación
113+
const confirmAnswers = await inquirer.prompt([
114+
{
115+
default: false,
116+
message: "Are you sure you want to delete this mongo database?",
117+
name: "confirmDelete",
118+
type: "confirm",
119+
},
120+
]);
121+
122+
if (!confirmAnswers.confirmDelete) {
123+
this.log(chalk.yellow("Application deletion cancelled."));
124+
return;
125+
}
126+
127+
// Eliminar la aplicación seleccionada
128+
const deleteResponse = await axios.post(
129+
`${auth.url}/api/trpc/mongo.remove`,
130+
{
131+
json: {
132+
mongoId,
133+
},
134+
},
135+
{
136+
headers: {
137+
Authorization: `Bearer ${auth.token}`,
138+
"Content-Type": "application/json",
139+
},
140+
},
141+
);
142+
143+
if (!deleteResponse.data.result.data.json) {
144+
this.error(chalk.red("Error deleting application"));
145+
}
146+
147+
this.log(chalk.green("Application deleted successfully."));
148+
} catch (error) {
149+
// @ts-expect-error - TS2339: Property 'data' does not exist on type 'AxiosError<any>'.
150+
this.error(chalk.red(`Failed to delete application: ${error.message}`));
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)