@liara/cli
Version:
The command line interface for Liara
96 lines (95 loc) • 3.49 kB
JavaScript
import inquirer from 'inquirer';
import ora from 'ora';
import { Flags } from '@oclif/core';
import Command from '../../base.js';
import { createDebugLogger } from '../../utils/output.js';
import { MAIL_SERVICE_URL, DEV_MODE, MAIL_SERVICE_URL_DEV, } from '../../constants.js';
class MailDelete extends Command {
async setGotConfig(config) {
await super.setGotConfig(config);
this.got = this.got.extend({
prefixUrl: DEV_MODE ? MAIL_SERVICE_URL_DEV : MAIL_SERVICE_URL,
});
}
async run() {
var _a;
const { flags } = await this.parse(MailDelete);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const { data } = await this.got('api/v1/mails').json();
const mailDomain = await this.promptMails();
const mailId = flags.mail ||
((_a = data.mailServers.find((mail) => mail.domain === mailDomain)) === null || _a === void 0 ? void 0 : _a.id);
try {
if (!flags.force) {
if (await this.confirm(mailDomain)) {
await this.got.delete(`api/v1/mails/${mailId}`);
this.log(`Mail server ${mailDomain} deleted.`);
}
}
else {
await this.got.delete(`api/v1/mails/${mailId}`);
this.log(`Mail server ${mailDomain} deleted.`);
}
}
catch (error) {
debug(error.message);
if (error.response && error.response.data) {
debug(JSON.stringify(error.response.data));
}
if (error.response && error.response.status === 404) {
this.error(`Could not find the mail server.`);
}
if (error.response && error.response.status === 409) {
this.error(`Another operation is already running. Please wait.`);
}
this.error(`Could not delete the mail server. Please try again.`);
}
}
async promptMails() {
this.spinner = ora();
this.spinner.start('Loading...');
try {
const { data } = await this.got('api/v1/mails').json();
this.spinner.stop();
if (!data.mailServers.length) {
this.warn('Please go to https://console.liara.ir/mail and create a mail server, first.');
this.exit(1);
}
const { mailDomain } = (await inquirer.prompt({
name: 'mailDomain',
type: 'list',
message: 'Please select a mail server:',
choices: [...data.mailServers.map((mail) => mail.domain)],
}));
return mailDomain;
}
catch (error) {
this.spinner.stop();
throw error;
}
}
async confirm(mail) {
const { confirmation } = (await inquirer.prompt({
name: 'confirmation',
type: 'confirm',
message: `Are you sure you want to delete mail server "${mail}"?`,
default: false,
}));
return confirmation;
}
}
MailDelete.description = 'delete an mail server';
MailDelete.flags = {
...Command.flags,
mail: Flags.string({
char: 'm',
description: 'mail server id',
}),
force: Flags.boolean({
char: 'f',
description: 'force the deletion',
}),
};
MailDelete.aliases = ['mail:delete'];
export default MailDelete;