-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdelete.ts
121 lines (101 loc) · 3.17 KB
/
delete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import inquirer from 'inquirer';
import ora from 'ora';
import { Flags } from '@oclif/core';
import Command, { IConfig, IGetMailboxesResponse } from '../../base.js';
import { createDebugLogger } from '../../utils/output.js';
import {
MAIL_SERVICE_URL,
DEV_MODE,
MAIL_SERVICE_URL_DEV,
} from '../../constants.js';
export default class MailDelete extends Command {
static description = 'delete an mail server';
static flags = {
...Command.flags,
mail: Flags.string({
char: 'm',
description: 'mail server id',
}),
force: Flags.boolean({
char: 'f',
description: 'force the deletion',
}),
};
static aliases = ['mail:delete'];
async setGotConfig(config: IConfig): Promise<void> {
await super.setGotConfig(config);
this.got = this.got.extend({
prefixUrl: DEV_MODE ? MAIL_SERVICE_URL_DEV : MAIL_SERVICE_URL,
});
}
async run() {
const { flags } = await this.parse(MailDelete);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const { data } = await this.got(
'api/v1/mails'
).json<IGetMailboxesResponse>();
const mailDomain = await this.promptMails();
const mailId =
flags.mail ||
data.mailServers.find((mail) => mail.domain === mailDomain)?.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<IGetMailboxesResponse>();
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)],
})) as { mailDomain: string };
return mailDomain;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async confirm(mail: string) {
const { confirmation } = (await inquirer.prompt({
name: 'confirmation',
type: 'confirm',
message: `Are you sure you want to delete mail server "${mail}"?`,
default: false,
})) as { confirmation: boolean };
return confirmation;
}
}