feat: add verify and authenticate command

This commit is contained in:
Mauricio Siu
2024-06-04 21:47:36 -06:00
parent 6dae0587d8
commit 9f033de1b2
4 changed files with 132 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import inquirer, { type Answers, type QuestionCollection } from "inquirer";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
@@ -13,8 +13,8 @@ export default class Authenticate extends Command {
static description = "Authenticate the user by saving server URL and token";
static examples = [
"$ <%= config.bin %> <%= command.id %> --url=https://panel.dokploy.com --token=aslgjasndjanskj123%!@#",
"$ <%= config.bin %> <%= command.id %> -u https://panel.dokploy.com -t aslgjasndjanskj123%!@#",
"$ <%= config.bin %> <%= command.id %> --url=https://panel.dokploy.com --token=MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY",
"$ <%= config.bin %> <%= command.id %> -u https://panel.dokploy.com -t MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY",
];
static flags = {
@@ -35,36 +35,50 @@ export default class Authenticate extends Command {
const { flags } = await this.parse(Authenticate);
let answers: any;
if (!flags.token || !flags.url) {
answers = await inquirer.prompt([
{
message: chalk.green(
"Enter your server URL (e.g., https://panel.dokploy.com): ",
),
name: "url",
type: "input",
validate: (input) => (input ? true : "Server URL is required"),
},
{
message: chalk.green(
"Enter your authentication token (e.g., aslgjasndjanskj123%!@#): ",
),
name: "token",
type: "input",
validate: (input) =>
input ? true : "Authentication token is required",
},
]);
let answers: Answers = {};
const questions: QuestionCollection[] = [];
let config: { token?: string; url?: string } = {};
if (fs.existsSync(configPath)) {
const configFileContent = fs.readFileSync(configPath, "utf8");
config = JSON.parse(configFileContent);
}
if (!flags.url) {
questions.push({
default: config.url,
message: chalk.green(
"Enter your server URL (e.g., https://panel.dokploy.com): ",
),
name: "url",
type: "input",
validate: (input) => (input ? true : "Server URL is required"),
});
}
if (!flags.token) {
questions.push({
default: config.token,
message: chalk.green(
"Enter your authentication token (e.g., MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY=): ",
),
name: "token",
type: "input",
validate: (input) =>
input ? true : "Authentication token is required",
});
}
if (questions.length > 0) {
answers = await inquirer.prompt(questions);
}
const url = flags.url || answers.url;
const token = flags.token || answers.token;
const config = {
token,
url,
};
config.token = token;
config.url = url;
try {
console.log(`\n${chalk.blue("Validating server...")}`);
@@ -86,7 +100,6 @@ export default class Authenticate extends Command {
if (!response.data.result.data.json) {
this.error(chalk.red("Invalid token"));
}
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
this.log(chalk.green("Authentication details saved successfully."));
} catch (error) {