mirror of
https://github.com/Dokploy/cli.git
synced 2026-06-15 20:25:22 +02:00
feat: add database create
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
"chalk": "^5.3.0",
|
"chalk": "^5.3.0",
|
||||||
"cli-table3": "^0.6.5",
|
"cli-table3": "^0.6.5",
|
||||||
"inquirer": "^9.2.23",
|
"inquirer": "^9.2.23",
|
||||||
|
"slugify": "^1.6.6",
|
||||||
"superjson": "^2.2.1"
|
"superjson": "^2.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@@ -26,6 +26,9 @@ dependencies:
|
|||||||
inquirer:
|
inquirer:
|
||||||
specifier: ^9.2.23
|
specifier: ^9.2.23
|
||||||
version: 9.2.23
|
version: 9.2.23
|
||||||
|
slugify:
|
||||||
|
specifier: ^1.6.6
|
||||||
|
version: 1.6.6
|
||||||
superjson:
|
superjson:
|
||||||
specifier: ^2.2.1
|
specifier: ^2.2.1
|
||||||
version: 2.2.1
|
version: 2.2.1
|
||||||
@@ -4784,6 +4787,11 @@ packages:
|
|||||||
astral-regex: 2.0.0
|
astral-regex: 2.0.0
|
||||||
is-fullwidth-code-point: 3.0.0
|
is-fullwidth-code-point: 3.0.0
|
||||||
|
|
||||||
|
/slugify@1.6.6:
|
||||||
|
resolution: {integrity: sha512-h+z7HKHYXj6wJU+AnS/+IH8Uh9fdcX1Lrhg1/VMdf9PwoBQXFcXiAdsy2tSK0P6gKwJLXp02r90ahUCqHk9rrw==}
|
||||||
|
engines: {node: '>=8.0.0'}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/snake-case@3.0.4:
|
/snake-case@3.0.4:
|
||||||
resolution: {integrity: sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==}
|
resolution: {integrity: sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==}
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export default class AppCreate extends Command {
|
|||||||
name: project.name,
|
name: project.name,
|
||||||
value: project.projectId,
|
value: project.projectId,
|
||||||
})),
|
})),
|
||||||
message: "Select a project to create the application in:",
|
message: "Select a project to create the database in:",
|
||||||
name: "selectedProject",
|
name: "selectedProject",
|
||||||
type: "list",
|
type: "list",
|
||||||
},
|
},
|
||||||
@@ -68,16 +68,32 @@ export default class AppCreate extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const databases = ["postgres", "mysql", "redis", "mariadb", "mongo"];
|
||||||
|
|
||||||
|
const databaseSelect = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: databases.map((database: any) => ({
|
||||||
|
name: database,
|
||||||
|
value: database,
|
||||||
|
})),
|
||||||
|
message: "Select a database to create the application in:",
|
||||||
|
name: "selectedDatabase",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const urlSelected = `${auth.url}/api/trpc/${databaseSelect.selectedDatabase}.create`;
|
||||||
|
|
||||||
// Solicitar detalles de la nueva aplicación
|
// Solicitar detalles de la nueva aplicación
|
||||||
const appDetails = await inquirer.prompt([
|
const appDetails = await inquirer.prompt([
|
||||||
{
|
{
|
||||||
message: "Enter the application name:",
|
message: "Enter the database name:",
|
||||||
name: "appName",
|
name: "appName",
|
||||||
type: "input",
|
type: "input",
|
||||||
validate: (input) => (input ? true : "Application name is required"),
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
message: "Enter the application description (optional):",
|
message: "Enter the database description (optional):",
|
||||||
name: "appDescription",
|
name: "appDescription",
|
||||||
type: "input",
|
type: "input",
|
||||||
},
|
},
|
||||||
@@ -87,32 +103,30 @@ export default class AppCreate extends Command {
|
|||||||
|
|
||||||
// Crear la aplicación en el proyecto seleccionado
|
// Crear la aplicación en el proyecto seleccionado
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
// const response = await axios.post(
|
||||||
`${auth.url}/api/trpc/application.create`,
|
// `${auth.url}/api/trpc/application.create`,
|
||||||
{
|
// {
|
||||||
json: {
|
// json: {
|
||||||
description: appDescription,
|
// description: appDescription,
|
||||||
name: appName,
|
// name: appName,
|
||||||
projectId,
|
// projectId,
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
headers: {
|
// headers: {
|
||||||
Authorization: `Bearer ${auth.token}`,
|
// Authorization: `Bearer ${auth.token}`,
|
||||||
"Content-Type": "application/json",
|
// "Content-Type": "application/json",
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
);
|
// );
|
||||||
|
// if (!response.data.result.data.json) {
|
||||||
if (!response.data.result.data.json) {
|
// this.error(chalk.red("Error creating application"));
|
||||||
this.error(chalk.red("Error creating application"));
|
// }
|
||||||
}
|
// this.log(
|
||||||
|
// chalk.green(
|
||||||
this.log(
|
// `Application '${appName}' created successfully in project ID '${projectId}'.`,
|
||||||
chalk.green(
|
// ),
|
||||||
`Application '${appName}' created successfully in project ID '${projectId}'.`,
|
// );
|
||||||
),
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// @ts-expect-error TODO: Fix this
|
// @ts-expect-error TODO: Fix this
|
||||||
this.error(chalk.red(`Failed to create application: ${error.message}`));
|
this.error(chalk.red(`Failed to create application: ${error.message}`));
|
||||||
|
|||||||
121
src/commands/database/create.ts
Normal file
121
src/commands/database/create.ts
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
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 DatabaseCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> app create"];
|
||||||
|
|
||||||
|
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(DatabaseCreate);
|
||||||
|
|
||||||
|
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 create the application in:",
|
||||||
|
name: "selectedProject",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
projectId = answers.selectedProject;
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Solicitar detalles de la nueva aplicación
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the database name:",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Application name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the application description (optional):",
|
||||||
|
name: "appDescription",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const { appDescription, appName } = appDetails;
|
||||||
|
|
||||||
|
// Crear la aplicación en el proyecto seleccionado
|
||||||
|
try {
|
||||||
|
const response = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/database.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
description: appDescription,
|
||||||
|
name: appName,
|
||||||
|
projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating application"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(
|
||||||
|
`Application '${appName}' created successfully in project ID '${projectId}'.`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to create application: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
144
src/commands/database/mariadb/create.ts
Normal file
144
src/commands/database/mariadb/create.ts
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
import { Command, Flags } from "@oclif/core";
|
||||||
|
import axios from "axios";
|
||||||
|
import chalk from "chalk";
|
||||||
|
import inquirer from "inquirer";
|
||||||
|
|
||||||
|
import { slugify } from "../../../utils/slug.js";
|
||||||
|
import { readAuthConfig } from "../../../utils/utils.js";
|
||||||
|
|
||||||
|
export default class DatabaseMariadbCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> mariadb create"];
|
||||||
|
|
||||||
|
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(DatabaseMariadbCreate);
|
||||||
|
|
||||||
|
const { projectId } = flags;
|
||||||
|
if (!projectId) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { project } = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: projects.map((project: any) => ({
|
||||||
|
name: project.name,
|
||||||
|
value: project,
|
||||||
|
})),
|
||||||
|
message: "Select a project to create the database in:",
|
||||||
|
name: "project",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the name:",
|
||||||
|
name: "name",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database name:",
|
||||||
|
name: "databaseName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the database description (optional):",
|
||||||
|
name: "description",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database Root Password (optional):",
|
||||||
|
name: "databaseRootPassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database password (optional):",
|
||||||
|
name: "databasePassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mariadb:4",
|
||||||
|
message: "Docker Image (default: mariadb:4):",
|
||||||
|
name: "dockerImage",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mariadb",
|
||||||
|
message: "Database User: (default: mariadb):",
|
||||||
|
name: "databaseUser",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appName = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
default: `${slugify(project.name)}-${appDetails.name}`,
|
||||||
|
message: "Enter the App name: (optional):",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const responseDatabase = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/mariadb.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
...appDetails,
|
||||||
|
appName: appName.appName,
|
||||||
|
projectId: project.projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!responseDatabase.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating database"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(`Database '${appDetails.name}' created successfully.`),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
138
src/commands/database/mongo/create.ts
Normal file
138
src/commands/database/mongo/create.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import { Command, Flags } from "@oclif/core";
|
||||||
|
import axios from "axios";
|
||||||
|
import chalk from "chalk";
|
||||||
|
import inquirer from "inquirer";
|
||||||
|
|
||||||
|
import { slugify } from "../../../utils/slug.js";
|
||||||
|
import { readAuthConfig } from "../../../utils/utils.js";
|
||||||
|
|
||||||
|
export default class DatabaseMongoCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> mongo create"];
|
||||||
|
|
||||||
|
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(DatabaseMongoCreate);
|
||||||
|
|
||||||
|
const { projectId } = flags;
|
||||||
|
if (!projectId) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { project } = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: projects.map((project: any) => ({
|
||||||
|
name: project.name,
|
||||||
|
value: project,
|
||||||
|
})),
|
||||||
|
message: "Select a project to create the database in:",
|
||||||
|
name: "project",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the name:",
|
||||||
|
name: "name",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database name:",
|
||||||
|
name: "databaseName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the database description (optional):",
|
||||||
|
name: "description",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database password (optional):",
|
||||||
|
name: "databasePassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mongo:6",
|
||||||
|
message: "Docker Image (default: mongo:6):",
|
||||||
|
name: "dockerImage",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mongo",
|
||||||
|
message: "Database User: (default: mongo):",
|
||||||
|
name: "databaseUser",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appName = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
default: `${slugify(project.name)}-${appDetails.name}`,
|
||||||
|
message: "Enter the App name: (optional):",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "App name is required"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const responseDatabase = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/mongo.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
...appDetails,
|
||||||
|
appName: appName.appName,
|
||||||
|
projectId: project.projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!responseDatabase.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating database"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(`Database '${appDetails.name}' created successfully.`),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
143
src/commands/database/mysql/create.ts
Normal file
143
src/commands/database/mysql/create.ts
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
import { Command, Flags } from "@oclif/core";
|
||||||
|
import axios from "axios";
|
||||||
|
import chalk from "chalk";
|
||||||
|
import inquirer from "inquirer";
|
||||||
|
|
||||||
|
import { slugify } from "../../../utils/slug.js";
|
||||||
|
import { readAuthConfig } from "../../../utils/utils.js";
|
||||||
|
|
||||||
|
export default class DatabaseMysqlCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> mysql create"];
|
||||||
|
|
||||||
|
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(DatabaseMysqlCreate);
|
||||||
|
|
||||||
|
const { projectId } = flags;
|
||||||
|
if (!projectId) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { project } = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: projects.map((project: any) => ({
|
||||||
|
name: project.name,
|
||||||
|
value: project,
|
||||||
|
})),
|
||||||
|
message: "Select a project to create the database in:",
|
||||||
|
name: "project",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the name:",
|
||||||
|
name: "name",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database name:",
|
||||||
|
name: "databaseName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the database description (optional):",
|
||||||
|
name: "description",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database Root Password (optional):",
|
||||||
|
name: "databaseRootPassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database password (optional):",
|
||||||
|
name: "databasePassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mysql:8",
|
||||||
|
message: "Docker Image (default: mysql:8):",
|
||||||
|
name: "dockerImage",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "mysql",
|
||||||
|
message: "Database User: (default: mysql):",
|
||||||
|
name: "databaseUser",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appName = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
default: `${slugify(project.name)}-${appDetails.name}`,
|
||||||
|
message: "Enter the App name: (optional):",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const responseDatabase = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/mysql.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
...appDetails,
|
||||||
|
appName: appName.appName,
|
||||||
|
projectId: project.projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!responseDatabase.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating database"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(`Database '${appDetails.name}' created successfully.`),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
140
src/commands/database/postgres/create.ts
Normal file
140
src/commands/database/postgres/create.ts
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
import { Command, Flags } from "@oclif/core";
|
||||||
|
import axios from "axios";
|
||||||
|
import chalk from "chalk";
|
||||||
|
import inquirer from "inquirer";
|
||||||
|
|
||||||
|
import { slugify } from "../../../utils/slug.js";
|
||||||
|
import { readAuthConfig } from "../../../utils/utils.js";
|
||||||
|
|
||||||
|
export default class DatabasePostgresCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> postgres create"];
|
||||||
|
|
||||||
|
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(DatabasePostgresCreate);
|
||||||
|
|
||||||
|
const { projectId } = flags;
|
||||||
|
if (!projectId) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { project } = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: projects.map((project: any) => ({
|
||||||
|
name: project.name,
|
||||||
|
value: project,
|
||||||
|
})),
|
||||||
|
message: "Select a project to create the database in:",
|
||||||
|
name: "project",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the name:",
|
||||||
|
name: "name",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database name:",
|
||||||
|
name: "databaseName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the database description (optional):",
|
||||||
|
name: "description",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database password (optional):",
|
||||||
|
name: "databasePassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "postgres:15",
|
||||||
|
message: "Docker Image (default: postgres:15):",
|
||||||
|
name: "dockerImage",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
default: "postgres",
|
||||||
|
message: "Database User: (default: postgres):",
|
||||||
|
name: "databaseUser",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appName = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
default: `${slugify(project.name)}-${appDetails.name}`,
|
||||||
|
message: "Enter the App name: (optional):",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const responseDatabase = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/postgres.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
...appDetails,
|
||||||
|
appName: appName.appName,
|
||||||
|
projectId: project.projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!responseDatabase.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating database"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(`Database '${appDetails.name}' created successfully.`),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
125
src/commands/database/redis/create.ts
Normal file
125
src/commands/database/redis/create.ts
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
import { Command, Flags } from "@oclif/core";
|
||||||
|
import axios from "axios";
|
||||||
|
import chalk from "chalk";
|
||||||
|
import inquirer from "inquirer";
|
||||||
|
|
||||||
|
import { slugify } from "../../../utils/slug.js";
|
||||||
|
import { readAuthConfig } from "../../../utils/utils.js";
|
||||||
|
|
||||||
|
export default class DatabaseRedisCreate extends Command {
|
||||||
|
static description = "Create a new database within a project.";
|
||||||
|
|
||||||
|
static examples = ["$ <%= config.bin %> redis create"];
|
||||||
|
|
||||||
|
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(DatabaseRedisCreate);
|
||||||
|
const { projectId } = flags;
|
||||||
|
if (!projectId) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { project } = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
choices: projects.map((project: any) => ({
|
||||||
|
name: project.name,
|
||||||
|
value: project,
|
||||||
|
})),
|
||||||
|
message: "Select a project to create the database in:",
|
||||||
|
name: "project",
|
||||||
|
type: "list",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appDetails = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
message: "Enter the name:",
|
||||||
|
name: "name",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Enter the database description (optional):",
|
||||||
|
name: "description",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Database password (optional):",
|
||||||
|
name: "databasePassword",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: "redis:7",
|
||||||
|
message: "Docker Image (default: redis:7):",
|
||||||
|
name: "dockerImage",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const appName = await inquirer.prompt([
|
||||||
|
{
|
||||||
|
default: `${slugify(project.name)}-${appDetails.name}`,
|
||||||
|
message: "Enter the App name: (optional):",
|
||||||
|
name: "appName",
|
||||||
|
type: "input",
|
||||||
|
validate: (input) => (input ? true : "Database name is required"),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const responseDatabase = await axios.post(
|
||||||
|
`${auth.url}/api/trpc/redis.create`,
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
...appDetails,
|
||||||
|
appName: appName.appName,
|
||||||
|
projectId: project.projectId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${auth.token}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!responseDatabase.data.result.data.json) {
|
||||||
|
this.error(chalk.red("Error creating database"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(
|
||||||
|
chalk.green(`Database '${appDetails.name}' created successfully.`),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-expect-error TODO: Fix this
|
||||||
|
this.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/utils/slug.ts
Normal file
14
src/utils/slug.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import slug from "./slugify.js";
|
||||||
|
|
||||||
|
export const slugify = (text: string | undefined) => {
|
||||||
|
if (!text) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanedText = text.trim().replaceAll(/[^\d\sA-Za-z]/g, "");
|
||||||
|
return slug(cleanedText, {
|
||||||
|
lower: true,
|
||||||
|
strict: true,
|
||||||
|
trim: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
3
src/utils/slugify.ts
Normal file
3
src/utils/slugify.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import slugify from "slugify";
|
||||||
|
|
||||||
|
export default slugify as unknown as typeof slugify.default;
|
||||||
14
test/commands/database/create.test.ts
Normal file
14
test/commands/database/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:create', () => {
|
||||||
|
it('runs database:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
14
test/commands/database/mariadb/create.test.ts
Normal file
14
test/commands/database/mariadb/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:mariadb:create', () => {
|
||||||
|
it('runs database:mariadb:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mariadb:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:mariadb:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mariadb:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
14
test/commands/database/mongo/create.test.ts
Normal file
14
test/commands/database/mongo/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:mongo:create', () => {
|
||||||
|
it('runs database:mongo:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mongo:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:mongo:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mongo:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
14
test/commands/database/mysql/create.test.ts
Normal file
14
test/commands/database/mysql/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:mysql:create', () => {
|
||||||
|
it('runs database:mysql:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mysql:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:mysql:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:mysql:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
14
test/commands/database/postgres/create.test.ts
Normal file
14
test/commands/database/postgres/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:postgres:create', () => {
|
||||||
|
it('runs database:postgres:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:postgres:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:postgres:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:postgres:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
14
test/commands/database/redis/create.test.ts
Normal file
14
test/commands/database/redis/create.test.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {runCommand} from '@oclif/test'
|
||||||
|
import {expect} from 'chai'
|
||||||
|
|
||||||
|
describe('database:redis:create', () => {
|
||||||
|
it('runs database:redis:create cmd', async () => {
|
||||||
|
const {stdout} = await runCommand('database:redis:create')
|
||||||
|
expect(stdout).to.contain('hello world')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('runs database:redis:create --name oclif', async () => {
|
||||||
|
const {stdout} = await runCommand('database:redis:create --name oclif')
|
||||||
|
expect(stdout).to.contain('hello oclif')
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user