-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.ts
76 lines (59 loc) · 2 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
import ora from 'ora';
import inquirer from 'inquirer';
import Command, { IConfig } from '../../base.js';
import { Flags } from '@oclif/core';
import { createDebugLogger } from '../../utils/output.js';
export default class Create extends Command {
static description = 'create a new zone';
static baseURL = 'https://dns-service.iran.liara.ir';
static PATH = 'api/v1/zones';
static flags = {
...Command.flags,
zone: Flags.string({
char: 'z',
description: 'zone name (domain)',
}),
};
async run(): Promise<void> {
this.spinner = ora();
const { flags } = await this.parse(Create);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const account = await this.getCurrentAccount();
((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');
const name = flags.zone || (await this.promptName());
try {
await this.got.post(Create.PATH, {
json: { name: name },
});
this.log(`Zone ${name} created.`);
} catch (error) {
debug(error.message);
if (error.response && error.response.body) {
debug(JSON.stringify(error.response.body));
}
if (error.response && error.response.statusCode === 400) {
this.error(`Enter correct domain name.`);
}
if (error.response && error.response.statusCode === 409) {
this.error(`The zone already exists.`);
}
this.error(`Could not create the zone. Please try again.`);
}
}
async promptName() {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'input',
message: 'Enter domain:',
validate: (input) => input.length > 2,
})) as { name: string };
return name;
}
async setGotConfig(config: IConfig): Promise<void> {
await super.setGotConfig(config);
const new_got = this.got.extend({ prefixUrl: Create.baseURL });
this.got = new_got; // baseURL is different for zone api
}
}