Skip to content

Commit a7a5e2e

Browse files
committed
feat: add environment selection to database commands
- Introduced environmentId flag to all database commands (create, delete, deploy, stop) for specifying the environment. - Implemented interactive prompts for selecting project and environment if flags are not provided. - Enhanced error handling for cases with no available environments or database instances. - Updated type definitions to include Database for better type safety.
1 parent 68786fb commit a7a5e2e

File tree

21 files changed

+914
-209
lines changed

21 files changed

+914
-209
lines changed

src/commands/database/mariadb/create.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import axios from "axios";
33
import chalk from "chalk";
44
import inquirer from "inquirer";
55
import { readAuthConfig } from "../../../utils/utils.js";
6-
import { getProjects } from "../../../utils/shared.js";
6+
import { getProjects, type Database } from "../../../utils/shared.js";
77
import { slugify } from "../../../utils/slug.js";
88
import type { Answers } from "../../app/create.js";
99

@@ -18,6 +18,11 @@ export default class DatabaseMariadbCreate extends Command {
1818
description: "ID of the project",
1919
required: false,
2020
}),
21+
environmentId: Flags.string({
22+
char: "e",
23+
description: "ID of the environment",
24+
required: false,
25+
}),
2126
name: Flags.string({
2227
char: "n",
2328
description: "Database name",
@@ -64,6 +69,7 @@ export default class DatabaseMariadbCreate extends Command {
6469
const { flags } = await this.parse(DatabaseMariadbCreate);
6570
let {
6671
projectId,
72+
environmentId,
6773
name,
6874
databaseName,
6975
description,
@@ -75,10 +81,13 @@ export default class DatabaseMariadbCreate extends Command {
7581
} = flags;
7682

7783
// Modo interactivo si no se proporcionan los flags necesarios
78-
if (!projectId || !name || !databaseName || !appName) {
84+
if (!projectId || !environmentId || !name || !databaseName || !appName) {
7985
console.log(chalk.blue.bold("\n Listing all Projects \n"));
8086
const projects = await getProjects(auth, this);
8187

88+
let selectedProject;
89+
90+
// 1. Seleccionar proyecto
8291
if (!projectId) {
8392
const { project } = await inquirer.prompt<Answers>([
8493
{
@@ -91,7 +100,30 @@ export default class DatabaseMariadbCreate extends Command {
91100
type: "list",
92101
},
93102
]);
103+
selectedProject = project;
94104
projectId = project.projectId;
105+
} else {
106+
selectedProject = projects.find(p => p.projectId === projectId);
107+
}
108+
109+
// 2. Seleccionar environment del proyecto
110+
if (!environmentId) {
111+
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
112+
this.error(chalk.yellow("No environments found in this project."));
113+
}
114+
115+
const { environment } = await inquirer.prompt([
116+
{
117+
choices: selectedProject.environments.map((env) => ({
118+
name: `${env.name} (${env.description})`,
119+
value: env,
120+
})),
121+
message: "Select an environment:",
122+
name: "environment",
123+
type: "list",
124+
},
125+
]);
126+
environmentId = environment.environmentId;
95127
}
96128

97129
if (!name || !databaseName || !appName) {
@@ -195,6 +227,7 @@ export default class DatabaseMariadbCreate extends Command {
195227
dockerImage,
196228
appName,
197229
projectId,
230+
environmentId,
198231
},
199232
},
200233
{

src/commands/database/mariadb/delete.ts

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import axios from "axios";
33
import chalk from "chalk";
44
import inquirer from "inquirer";
55

6-
import { getProject, getProjects } from "../../../utils/shared.js";
6+
import { getProject, getProjects, type Database } from "../../../utils/shared.js";
77
import { readAuthConfig } from "../../../utils/utils.js";
88

99
export default class DatabaseMariadbDelete extends Command {
@@ -18,6 +18,11 @@ export default class DatabaseMariadbDelete extends Command {
1818
description: "ID of the project",
1919
required: false,
2020
}),
21+
environmentId: Flags.string({
22+
char: "e",
23+
description: "ID of the environment",
24+
required: false,
25+
}),
2126
mariadbId: Flags.string({
2227
char: "m",
2328
description: "ID of the MariaDB instance to delete",
@@ -33,12 +38,16 @@ export default class DatabaseMariadbDelete extends Command {
3338
public async run(): Promise<void> {
3439
const auth = await readAuthConfig(this);
3540
const { flags } = await this.parse(DatabaseMariadbDelete);
36-
let { projectId, mariadbId } = flags;
41+
let { projectId, environmentId, mariadbId } = flags;
3742

38-
if (!projectId || !mariadbId) {
43+
if (!projectId || !environmentId || !mariadbId) {
3944
console.log(chalk.blue.bold("\n Listing all Projects \n"));
4045
const projects = await getProjects(auth, this);
4146

47+
let selectedProject;
48+
let selectedEnvironment;
49+
50+
// 1. Seleccionar proyecto
4251
if (!projectId) {
4352
const answers = await inquirer.prompt([
4453
{
@@ -51,30 +60,54 @@ export default class DatabaseMariadbDelete extends Command {
5160
type: "list",
5261
},
5362
]);
63+
selectedProject = projects.find(p => p.projectId === answers.selectedProject);
5464
projectId = answers.selectedProject;
65+
} else {
66+
selectedProject = projects.find(p => p.projectId === projectId);
5567
}
5668

57-
const projectSelected = await getProject(projectId, auth, this);
69+
// 2. Seleccionar environment del proyecto
70+
if (!environmentId) {
71+
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
72+
this.error(chalk.yellow("No environments found in this project."));
73+
}
5874

59-
if (!projectSelected.mariadb || projectSelected.mariadb.length === 0) {
60-
this.error(chalk.yellow("No MariaDB instances found in this project."));
61-
}
75+
const { environment } = await inquirer.prompt([
76+
{
77+
choices: selectedProject.environments.map((env) => ({
78+
name: `${env.name} (${env.description})`,
79+
value: env,
80+
})),
81+
message: "Select an environment:",
82+
name: "environment",
83+
type: "list",
84+
},
85+
]);
86+
selectedEnvironment = environment;
87+
environmentId = environment.environmentId;
88+
} else {
89+
selectedEnvironment = selectedProject?.environments?.find(e => e.environmentId === environmentId);
90+
}
6291

92+
// 3. Seleccionar MariaDB del environment
6393
if (!mariadbId) {
94+
if (!selectedEnvironment?.mariadb || selectedEnvironment.mariadb.length === 0) {
95+
this.error(chalk.yellow("No MariaDB instances found in this environment."));
96+
}
97+
6498
const dbAnswers = await inquirer.prompt([
65-
{
66-
// @ts-ignore
67-
choices: projectSelected.mariadb.map((db) => ({
68-
name: db.name,
69-
value: db.mariadbId,
70-
})),
71-
message: "Select the MariaDB instance to delete:",
72-
name: "selectedDb",
73-
type: "list",
74-
},
75-
]);
76-
mariadbId = dbAnswers.selectedDb;
77-
}
99+
{
100+
choices: selectedEnvironment.mariadb.map((db: Database) => ({
101+
name: db.name,
102+
value: db.mariadbId,
103+
})),
104+
message: "Select the MariaDB instance to delete:",
105+
name: "selectedDb",
106+
type: "list",
107+
},
108+
]);
109+
mariadbId = dbAnswers.selectedDb;
110+
}
78111
}
79112

80113
if (!flags.skipConfirm) {

src/commands/database/mariadb/deploy.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Command, Flags } from "@oclif/core";
22
import { readAuthConfig } from "../../../utils/utils.js";
33
import chalk from "chalk";
4-
import { getProject, getProjects } from "../../../utils/shared.js";
4+
import { getProject, getProjects, type Database } from "../../../utils/shared.js";
55
import inquirer from "inquirer";
66
import type { Answers } from "../../app/create.js";
77
import axios from "axios";
@@ -17,6 +17,11 @@ export default class DatabaseMariadbDeploy extends Command {
1717
description: "ID of the project",
1818
required: false,
1919
}),
20+
environmentId: Flags.string({
21+
char: "e",
22+
description: "ID of the environment",
23+
required: false,
24+
}),
2025
mariadbId: Flags.string({
2126
char: "m",
2227
description: "ID of the MariaDB instance to deploy",
@@ -32,13 +37,17 @@ export default class DatabaseMariadbDeploy extends Command {
3237
public async run(): Promise<void> {
3338
const auth = await readAuthConfig(this);
3439
const { flags } = await this.parse(DatabaseMariadbDeploy);
35-
let { projectId, mariadbId } = flags;
40+
let { projectId, environmentId, mariadbId } = flags;
3641

3742
// Modo interactivo si no se proporcionan los flags necesarios
38-
if (!projectId || !mariadbId) {
43+
if (!projectId || !environmentId || !mariadbId) {
3944
console.log(chalk.blue.bold("\n Listing all Projects \n"));
4045
const projects = await getProjects(auth, this);
4146

47+
let selectedProject;
48+
let selectedEnvironment;
49+
50+
// 1. Seleccionar proyecto
4251
if (!projectId) {
4352
const { project } = await inquirer.prompt<Answers>([
4453
{
@@ -51,20 +60,44 @@ export default class DatabaseMariadbDeploy extends Command {
5160
type: "list",
5261
},
5362
]);
63+
selectedProject = project;
5464
projectId = project.projectId;
65+
} else {
66+
selectedProject = projects.find(p => p.projectId === projectId);
5567
}
5668

57-
const projectSelected = await getProject(projectId, auth, this);
69+
// 2. Seleccionar environment del proyecto
70+
if (!environmentId) {
71+
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
72+
this.error(chalk.yellow("No environments found in this project."));
73+
}
5874

59-
if (projectSelected.mariadb.length === 0) {
60-
this.error(chalk.yellow("No MariaDB instances found in this project."));
75+
const { environment } = await inquirer.prompt([
76+
{
77+
choices: selectedProject.environments.map((env) => ({
78+
name: `${env.name} (${env.description})`,
79+
value: env,
80+
})),
81+
message: "Select an environment:",
82+
name: "environment",
83+
type: "list",
84+
},
85+
]);
86+
selectedEnvironment = environment;
87+
environmentId = environment.environmentId;
88+
} else {
89+
selectedEnvironment = selectedProject?.environments?.find(e => e.environmentId === environmentId);
6190
}
6291

92+
// 3. Seleccionar MariaDB del environment
6393
if (!mariadbId) {
94+
if (!selectedEnvironment?.mariadb || selectedEnvironment.mariadb.length === 0) {
95+
this.error(chalk.yellow("No MariaDB instances found in this environment."));
96+
}
97+
6498
const dbAnswers = await inquirer.prompt([
6599
{
66-
// @ts-ignore
67-
choices: projectSelected.mariadb.map((db) => ({
100+
choices: selectedEnvironment.mariadb.map((db: Database) => ({
68101
name: db.name,
69102
value: db.mariadbId,
70103
})),

src/commands/database/mariadb/stop.ts

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command, Flags } from "@oclif/core";
22
import chalk from "chalk";
33
import inquirer from "inquirer";
44
import axios from "axios";
5-
import { getProject, getProjects } from "../../../utils/shared.js";
5+
import { getProject, getProjects, type Database } from "../../../utils/shared.js";
66
import { readAuthConfig } from "../../../utils/utils.js";
77
import type { Answers } from "../../app/create.js";
88

@@ -17,6 +17,11 @@ export default class DatabaseMariadbStop extends Command {
1717
description: "ID of the project",
1818
required: false,
1919
}),
20+
environmentId: Flags.string({
21+
char: "e",
22+
description: "ID of the environment",
23+
required: false,
24+
}),
2025
mariadbId: Flags.string({
2126
char: "m",
2227
description: "ID of the MariaDB instance to stop",
@@ -32,13 +37,17 @@ export default class DatabaseMariadbStop extends Command {
3237
public async run(): Promise<void> {
3338
const auth = await readAuthConfig(this);
3439
const { flags } = await this.parse(DatabaseMariadbStop);
35-
let { projectId, mariadbId } = flags;
40+
let { projectId, environmentId, mariadbId } = flags;
3641

3742
// Modo interactivo si no se proporcionan los flags necesarios
38-
if (!projectId || !mariadbId) {
43+
if (!projectId || !environmentId || !mariadbId) {
3944
console.log(chalk.blue.bold("\n Listing all Projects \n"));
4045
const projects = await getProjects(auth, this);
4146

47+
let selectedProject;
48+
let selectedEnvironment;
49+
50+
// 1. Seleccionar proyecto
4251
if (!projectId) {
4352
const { project } = await inquirer.prompt<Answers>([
4453
{
@@ -51,30 +60,54 @@ export default class DatabaseMariadbStop extends Command {
5160
type: "list",
5261
},
5362
]);
63+
selectedProject = project;
5464
projectId = project.projectId;
65+
} else {
66+
selectedProject = projects.find(p => p.projectId === projectId);
5567
}
5668

57-
const projectSelected = await getProject(projectId, auth, this);
69+
// 2. Seleccionar environment del proyecto
70+
if (!environmentId) {
71+
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
72+
this.error(chalk.yellow("No environments found in this project."));
73+
}
5874

59-
if (projectSelected.mariadb.length === 0) {
60-
this.error(chalk.yellow("No MariaDB instances found in this project."));
61-
}
75+
const { environment } = await inquirer.prompt([
76+
{
77+
choices: selectedProject.environments.map((env) => ({
78+
name: `${env.name} (${env.description})`,
79+
value: env,
80+
})),
81+
message: "Select an environment:",
82+
name: "environment",
83+
type: "list",
84+
},
85+
]);
86+
selectedEnvironment = environment;
87+
environmentId = environment.environmentId;
88+
} else {
89+
selectedEnvironment = selectedProject?.environments?.find(e => e.environmentId === environmentId);
90+
}
6291

92+
// 3. Seleccionar MariaDB del environment
6393
if (!mariadbId) {
94+
if (!selectedEnvironment?.mariadb || selectedEnvironment.mariadb.length === 0) {
95+
this.error(chalk.yellow("No MariaDB instances found in this environment."));
96+
}
97+
6498
const dbAnswers = await inquirer.prompt([
65-
{
66-
// @ts-ignore
67-
choices: projectSelected.mariadb.map((db) => ({
68-
name: db.name,
69-
value: db.mariadbId,
70-
})),
71-
message: "Select the MariaDB instance to stop:",
72-
name: "selectedDb",
73-
type: "list",
74-
},
75-
]);
76-
mariadbId = dbAnswers.selectedDb;
77-
}
99+
{
100+
choices: selectedEnvironment.mariadb.map((db: Database) => ({
101+
name: db.name,
102+
value: db.mariadbId,
103+
})),
104+
message: "Select the MariaDB instance to stop:",
105+
name: "selectedDb",
106+
type: "list",
107+
},
108+
]);
109+
mariadbId = dbAnswers.selectedDb;
110+
}
78111
}
79112

80113
// Confirmar si no se especifica --skipConfirm

0 commit comments

Comments
 (0)