mirror of
https://github.com/Dokploy/cli.git
synced 2026-07-10 08:25:22 +02:00
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.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Command, Flags } from "@oclif/core";
|
||||
import { readAuthConfig } from "../../../utils/utils.js";
|
||||
import chalk from "chalk";
|
||||
import { getProject, getProjects } from "../../../utils/shared.js";
|
||||
import { getProject, getProjects, type Database } from "../../../utils/shared.js";
|
||||
import inquirer from "inquirer";
|
||||
import type { Answers } from "../../app/create.js";
|
||||
import axios from "axios";
|
||||
@@ -17,6 +17,11 @@ export default class DatabaseMariadbDeploy extends Command {
|
||||
description: "ID of the project",
|
||||
required: false,
|
||||
}),
|
||||
environmentId: Flags.string({
|
||||
char: "e",
|
||||
description: "ID of the environment",
|
||||
required: false,
|
||||
}),
|
||||
mariadbId: Flags.string({
|
||||
char: "m",
|
||||
description: "ID of the MariaDB instance to deploy",
|
||||
@@ -32,13 +37,17 @@ export default class DatabaseMariadbDeploy extends Command {
|
||||
public async run(): Promise<void> {
|
||||
const auth = await readAuthConfig(this);
|
||||
const { flags } = await this.parse(DatabaseMariadbDeploy);
|
||||
let { projectId, mariadbId } = flags;
|
||||
let { projectId, environmentId, mariadbId } = flags;
|
||||
|
||||
// Modo interactivo si no se proporcionan los flags necesarios
|
||||
if (!projectId || !mariadbId) {
|
||||
if (!projectId || !environmentId || !mariadbId) {
|
||||
console.log(chalk.blue.bold("\n Listing all Projects \n"));
|
||||
const projects = await getProjects(auth, this);
|
||||
|
||||
let selectedProject;
|
||||
let selectedEnvironment;
|
||||
|
||||
// 1. Seleccionar proyecto
|
||||
if (!projectId) {
|
||||
const { project } = await inquirer.prompt<Answers>([
|
||||
{
|
||||
@@ -51,20 +60,44 @@ export default class DatabaseMariadbDeploy extends Command {
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
selectedProject = project;
|
||||
projectId = project.projectId;
|
||||
} else {
|
||||
selectedProject = projects.find(p => p.projectId === projectId);
|
||||
}
|
||||
|
||||
const projectSelected = await getProject(projectId, auth, this);
|
||||
// 2. Seleccionar environment del proyecto
|
||||
if (!environmentId) {
|
||||
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
|
||||
this.error(chalk.yellow("No environments found in this project."));
|
||||
}
|
||||
|
||||
if (projectSelected.mariadb.length === 0) {
|
||||
this.error(chalk.yellow("No MariaDB instances found in this project."));
|
||||
const { environment } = await inquirer.prompt([
|
||||
{
|
||||
choices: selectedProject.environments.map((env) => ({
|
||||
name: `${env.name} (${env.description})`,
|
||||
value: env,
|
||||
})),
|
||||
message: "Select an environment:",
|
||||
name: "environment",
|
||||
type: "list",
|
||||
},
|
||||
]);
|
||||
selectedEnvironment = environment;
|
||||
environmentId = environment.environmentId;
|
||||
} else {
|
||||
selectedEnvironment = selectedProject?.environments?.find(e => e.environmentId === environmentId);
|
||||
}
|
||||
|
||||
// 3. Seleccionar MariaDB del environment
|
||||
if (!mariadbId) {
|
||||
if (!selectedEnvironment?.mariadb || selectedEnvironment.mariadb.length === 0) {
|
||||
this.error(chalk.yellow("No MariaDB instances found in this environment."));
|
||||
}
|
||||
|
||||
const dbAnswers = await inquirer.prompt([
|
||||
{
|
||||
// @ts-ignore
|
||||
choices: projectSelected.mariadb.map((db) => ({
|
||||
choices: selectedEnvironment.mariadb.map((db: Database) => ({
|
||||
name: db.name,
|
||||
value: db.mariadbId,
|
||||
})),
|
||||
|
||||
Reference in New Issue
Block a user