Skip to content

Commit f1616b3

Browse files
committed
feat: add info list project
1 parent bec0dfa commit f1616b3

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

src/commands/project/info.ts

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 ProjectInfo extends Command {
9+
static description =
10+
"Get detailed information about a project, including the number of applications and databases.";
11+
12+
static examples = [
13+
"$ <%= config.bin %> project info",
14+
"$ <%= config.bin %> project info -p <projectId>",
15+
];
16+
17+
static flags = {
18+
projectId: Flags.string({
19+
char: "p",
20+
description: "ID of the project",
21+
required: false,
22+
}),
23+
};
24+
25+
public async run(): Promise<void> {
26+
const auth = await readAuthConfig(this);
27+
28+
const { flags } = await this.parse(ProjectInfo);
29+
30+
if (flags.projectId) {
31+
// Si se proporciona un projectId, mostrar directamente la información del proyecto
32+
await this.showProjectInfo(auth, flags.projectId);
33+
} else {
34+
// Obtener la lista de proyectos y permitir la selección
35+
console.log(chalk.blue.bold("\n Listing all Projects \n"));
36+
37+
try {
38+
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
39+
headers: {
40+
Authorization: `Bearer ${auth.token}`,
41+
"Content-Type": "application/json",
42+
},
43+
});
44+
45+
if (!response.data.result.data.json) {
46+
this.error(chalk.red("Error fetching projects"));
47+
}
48+
49+
const projects = response.data.result.data.json;
50+
51+
if (projects.length === 0) {
52+
this.log(chalk.yellow("No projects found."));
53+
return;
54+
}
55+
56+
// Permitir al usuario seleccionar un proyecto
57+
const answers = await inquirer.prompt([
58+
{
59+
choices: projects.map((project: any) => ({
60+
name: project.name,
61+
value: project.projectId,
62+
})),
63+
message: "Select a project to view details:",
64+
name: "selectedProject",
65+
type: "list",
66+
},
67+
]);
68+
69+
const selectedProjectId = answers.selectedProject;
70+
71+
await this.showProjectInfo(auth, selectedProjectId);
72+
} catch (error) {
73+
// @ts-expect-error hola
74+
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
75+
}
76+
}
77+
}
78+
79+
private async showProjectInfo(
80+
auth: { token: string; url: string },
81+
projectId: string,
82+
) {
83+
console.log(
84+
chalk.blue.bold(`\n Information for Project ID: ${projectId} \n`),
85+
);
86+
87+
try {
88+
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
89+
headers: {
90+
Authorization: `Bearer ${auth.token}`,
91+
"Content-Type": "application/json",
92+
},
93+
params: {
94+
input: JSON.stringify({
95+
json: { projectId },
96+
}),
97+
},
98+
});
99+
100+
if (!response.data.result.data.json) {
101+
this.error(chalk.red("Error fetching project information"));
102+
}
103+
104+
const projectInfo = response.data.result.data.json;
105+
106+
this.log(chalk.green(`Project Name: ${projectInfo.name}`));
107+
this.log(
108+
chalk.green(
109+
`Description: ${projectInfo?.description || "No description"}`,
110+
),
111+
);
112+
this.log(
113+
chalk.green(
114+
`Number of Applications: ${projectInfo.applications.length}`,
115+
),
116+
);
117+
this.log(
118+
chalk.green(
119+
`Number of Compose Services: ${projectInfo.compose.length}`,
120+
),
121+
);
122+
this.log(
123+
chalk.green(
124+
`Number of MariaDB Databases: ${projectInfo.mariadb.length}`,
125+
),
126+
);
127+
this.log(
128+
chalk.green(`Number of MongoDB Databases: ${projectInfo.mongo.length}`),
129+
);
130+
this.log(
131+
chalk.green(`Number of MySQL Databases: ${projectInfo.mysql.length}`),
132+
);
133+
this.log(
134+
chalk.green(
135+
`Number of PostgreSQL Databases: ${projectInfo.postgres.length}`,
136+
),
137+
);
138+
this.log(
139+
chalk.green(`Number of Redis Databases: ${projectInfo.redis.length}`),
140+
);
141+
142+
if (projectInfo.applications.length > 0) {
143+
this.log(chalk.blue("\nApplications:"));
144+
projectInfo.applications.forEach((app: any, index: number) => {
145+
this.log(` ${index + 1}. ${app.name}`);
146+
});
147+
}
148+
149+
if (projectInfo.compose.length > 0) {
150+
this.log(chalk.blue("\nCompose Services:"));
151+
projectInfo.compose.forEach((service: any, index: number) => {
152+
this.log(` ${index + 1}. ${service.name}`);
153+
});
154+
}
155+
156+
if (projectInfo.mariadb.length > 0) {
157+
this.log(chalk.blue("\nMariaDB Databases:"));
158+
projectInfo.mariadb.forEach((db: any, index: number) => {
159+
this.log(` ${index + 1}. ${db.name}`);
160+
});
161+
}
162+
163+
if (projectInfo.mongo.length > 0) {
164+
this.log(chalk.blue("\nMongoDB Databases:"));
165+
projectInfo.mongo.forEach((db: any, index: number) => {
166+
this.log(` ${index + 1}. ${db.name}`);
167+
});
168+
}
169+
170+
if (projectInfo.mysql.length > 0) {
171+
this.log(chalk.blue("\nMySQL Databases:"));
172+
projectInfo.mysql.forEach((db: any, index: number) => {
173+
this.log(` ${index + 1}. ${db.name}`);
174+
});
175+
}
176+
177+
if (projectInfo.postgres.length > 0) {
178+
this.log(chalk.blue("\nPostgreSQL Databases:"));
179+
projectInfo.postgres.forEach((db: any, index: number) => {
180+
this.log(` ${index + 1}. ${db.name}`);
181+
});
182+
}
183+
184+
if (projectInfo.redis.length > 0) {
185+
this.log(chalk.blue("\nRedis Databases:"));
186+
projectInfo.redis.forEach((db: any, index: number) => {
187+
this.log(` ${index + 1}. ${db.name}`);
188+
});
189+
}
190+
} catch (error) {
191+
this.error(
192+
// @ts-expect-error hola
193+
chalk.red(`Failed to fetch project information: ${error.message}`),
194+
);
195+
}
196+
}
197+
}

test/commands/project/info.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {runCommand} from '@oclif/test'
2+
import {expect} from 'chai'
3+
4+
describe('project:info', () => {
5+
it('runs project:info cmd', async () => {
6+
const {stdout} = await runCommand('project:info')
7+
expect(stdout).to.contain('hello world')
8+
})
9+
10+
it('runs project:info --name oclif', async () => {
11+
const {stdout} = await runCommand('project:info --name oclif')
12+
expect(stdout).to.contain('hello oclif')
13+
})
14+
})

0 commit comments

Comments
 (0)