mirror of
https://github.com/Dokploy/cli.git
synced 2026-06-26 01:25:21 +02:00
refactor: add commands
This commit is contained in:
81
src/utils/shared.ts
Normal file
81
src/utils/shared.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import type { Command } from "@oclif/core";
|
||||
|
||||
import axios from "axios";
|
||||
import chalk from "chalk";
|
||||
|
||||
import type { AuthConfig } from "./utils.js";
|
||||
|
||||
export type Project = {
|
||||
adminId: string;
|
||||
name: string;
|
||||
projectId?: string | undefined;
|
||||
description?: string | undefined;
|
||||
};
|
||||
|
||||
export const getProjects = async (
|
||||
auth: AuthConfig,
|
||||
command: Command,
|
||||
): Promise<Project[]> => {
|
||||
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) {
|
||||
command.error(chalk.red("Error fetching projects"));
|
||||
}
|
||||
|
||||
const projects = response.data.result.data.json;
|
||||
|
||||
if (projects.length === 0) {
|
||||
command.log(chalk.yellow("No projects found."));
|
||||
return [];
|
||||
}
|
||||
|
||||
return projects;
|
||||
} catch {
|
||||
// @ts-expect-error TODO: Fix this
|
||||
command.error(chalk.red(`Failed to fetch project list: ${error.message}`));
|
||||
}
|
||||
};
|
||||
|
||||
export const getProject = async (
|
||||
projectId: string | undefined,
|
||||
auth: AuthConfig,
|
||||
command: Command,
|
||||
) => {
|
||||
try {
|
||||
if (!projectId) {
|
||||
command.error(chalk.red("Project ID is required"));
|
||||
}
|
||||
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${auth.token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
params: {
|
||||
input: JSON.stringify({
|
||||
json: { projectId },
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.data.result.data.json) {
|
||||
command.error(chalk.red("Error fetching project"));
|
||||
}
|
||||
|
||||
const project = response.data.result.data.json;
|
||||
|
||||
if (!project) {
|
||||
command.error(chalk.red("Error fetching project"));
|
||||
}
|
||||
|
||||
return project;
|
||||
} catch {
|
||||
// @ts-expect-error TODO: Fix this
|
||||
command.error(chalk.red(`Failed to fetch project: ${error.message}`));
|
||||
}
|
||||
};
|
||||
@@ -9,9 +9,12 @@ const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const configPath = path.join(__dirname, "..", "..", "config.json");
|
||||
|
||||
export const readAuthConfig = async (
|
||||
command: Command,
|
||||
): Promise<{ token: string; url: string }> => {
|
||||
export type AuthConfig = {
|
||||
token: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
export const readAuthConfig = async (command: Command): Promise<AuthConfig> => {
|
||||
if (!fs.existsSync(configPath)) {
|
||||
command.error(
|
||||
chalk.red(
|
||||
|
||||
Reference in New Issue
Block a user