Files
cli/src/utils/utils.ts
Mauricio Siu 10cfe23763 feat: enhance CLI commands with improved flags and error handling
This commit introduces several improvements across multiple CLI commands:
- Added new flags for more flexible command usage
- Implemented interactive mode for commands when flags are not provided
- Improved error handling and messaging
- Added support for environment variable authentication
- Standardized confirmation prompts and error messages
- Enhanced type safety with TypeScript improvements
2025-02-23 21:29:45 -06:00

49 lines
1.4 KiB
TypeScript

import type { Command } from "@oclif/core";
import chalk from "chalk";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const configPath = path.join(__dirname, "..", "..", "config.json");
export type AuthConfig = {
token: string;
url: string;
};
export const readAuthConfig = async (command: Command): Promise<AuthConfig> => {
// Primero intentar leer desde variables de entorno
const envToken = process.env.DOKPLOY_AUTH_TOKEN;
const envUrl = process.env.DOKPLOY_URL;
if (envToken && envUrl) {
return { token: envToken, url: envUrl };
}
// Si no hay variables de entorno, usar el archivo de configuración
if (!fs.existsSync(configPath)) {
command.error(
chalk.red(
"No configuration file found and no environment variables set. Please authenticate first using the 'authenticate' command or set DOKPLOY_URL and DOKPLOY_AUTH_TOKEN environment variables.",
),
);
}
const configFileContent = fs.readFileSync(configPath, "utf8");
const config = JSON.parse(configFileContent);
const { token, url } = config;
if (!url || !token) {
command.error(
chalk.red(
"Incomplete authentication details. Please authenticate again using the 'authenticate' command or set environment variables.",
),
);
}
return { token, url };
};