-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.ts
153 lines (122 loc) · 3.74 KB
/
create.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import ora, { Ora } from 'ora';
import inquirer from 'inquirer';
import Command, { IConfig } from '../../base.js';
import { Flags } from '@oclif/core';
import {
MAIL_SERVICE_MODES,
MAIL_SERVICE_PLANS,
MAIL_SERVICE_URL,
DEV_MODE,
MAIL_SERVICE_URL_DEV,
} from '../../constants.js';
import { createDebugLogger } from '../../utils/output.js';
export default class MailCreate extends Command {
static description = 'create a mail server';
static flags = {
...Command.flags,
domain: Flags.string({
description: 'domain',
}),
plan: Flags.string({
description: 'plan',
}),
mode: Flags.string({
description: 'mode',
}),
};
static aliases = ['mail:create'];
spinner!: Ora;
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() {
this.spinner = ora();
const { flags } = await this.parse(MailCreate);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const domain = flags.domain || (await this.promptDomain());
const account = await this.getCurrentAccount();
await this.setGotConfig(flags);
((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');
const plan = flags.platform || (await this.promptPlan());
if (!MAIL_SERVICE_PLANS.includes(plan)) {
this.error(`Unknown plan: ${plan}`);
}
const mode = flags.plan || (await this.promptMode());
if (!MAIL_SERVICE_MODES.includes(mode)) {
this.error(`Unknown mode: ${mode}`);
}
try {
await this.got.post('api/v1/mails', { json: { domain, plan, mode } });
this.log(`Mail server ${domain} created.`);
} catch (error) {
debug(error.message);
if (error.response && error.response.body) {
debug(JSON.stringify(error.response.body));
}
if (error.response && error.response.statusCode === 404) {
this.error(`Could not create the mail server.`);
}
if (error.response && error.response.statusCode === 409) {
this.error(
`The mail server already exists. Please use a unique name for your mail server.`
);
}
if (
error.response &&
error.response.statusCode === 403 &&
error.response.body
) {
this.error(
`You are allowed to create only one Mail Server on the free plan.`
);
}
this.error(`Could not create the mail server. Please try again.`);
}
}
async promptDomain(): Promise<string> {
const { domain } = (await inquirer.prompt({
name: 'domain',
type: 'input',
message: 'Enter your domain:',
validate: (input) => input.length > 2,
})) as { domain: string };
return domain;
}
async promptPlan() {
this.spinner.start('Loading...');
try {
this.spinner.stop();
const { plan } = (await inquirer.prompt({
name: 'plan',
type: 'list',
message: 'Please select the plan you want:',
choices: [...MAIL_SERVICE_PLANS.map((plan) => plan)],
})) as { plan: string };
return plan;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptMode() {
this.spinner.start('Loading...');
try {
this.spinner.stop();
const { mode } = (await inquirer.prompt({
name: 'mode',
type: 'list',
message: 'Please select the mode you want:',
choices: [...MAIL_SERVICE_MODES.map((mode) => mode)],
})) as { mode: string };
return mode;
} catch (error) {
this.spinner.stop();
throw error;
}
}
}