-
Notifications
You must be signed in to change notification settings - Fork 12
/
remove.ts
105 lines (84 loc) · 2.84 KB
/
remove.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
import inquirer from 'inquirer';
import Command, { IConfig } from '../../../base.js';
import { Flags } from '@oclif/core';
import { createDebugLogger } from '../../../utils/output.js';
import { ux } from '@oclif/core';
import { IDNSRecords } from '../../../types/dns-records.js';
export default class Remove extends Command {
static description = 'remove a DNS record';
static baseURL = 'https://dns-service.iran.liara.ir';
static PATH = 'api/v1/zones/{zone}/dns-records/{id}';
static aliases = ['zone:record:rm'];
static flags = {
...Command.flags,
zone: Flags.string({
char: 'z',
description: 'name of the zone (domain)',
}),
name: Flags.string({
char: 'n',
description: 'Name of the record',
}),
...ux.table.flags(),
};
async run() {
const { flags } = await this.parse(Remove);
await this.setGotConfig(flags);
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 zone = flags.zone || (await this.promptZone());
const name = flags.name || (await this.promptName());
const recordID = await this.getRecordIDByName(zone, name);
if (recordID === undefined) {
this.error(`Record ${name} for zone ${zone} not found`);
}
try {
await this.got.delete(
Remove.PATH.replace('{zone}', zone).replace('{id}', recordID)
);
this.log(`Record ${name} removed.`);
} catch (error) {
if (error.response && error.response.statusCode === 404) {
this.error(`Zone not found.`);
}
this.error(error.message);
}
}
async setGotConfig(config: IConfig): Promise<void> {
await super.setGotConfig(config);
const new_got = this.got.extend({ prefixUrl: Remove.baseURL });
this.got = new_got; // baseURL is different for zone api
}
async promptName() {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'input',
message: 'Enter record name:',
validate: (input) => input.length > 0,
})) as { name: string };
return name;
}
async promptZone() {
const { zone } = (await inquirer.prompt({
name: 'zone',
type: 'input',
message: 'Enter domain:',
validate: (input) => input.length > 2,
})) as { zone: string };
return zone;
}
async getRecordIDByName(zone: string, name: string) {
const { data } = await this.got(
'api/v1/zones/{zone}/dns-records'.replace('{zone}', zone)
).json<IDNSRecords>();
if (!data.length) {
this.error(`Not found any records.
Please open up https://console.liara.ir/zones.`);
}
const recordID = data.find((record) => record.name === name);
return recordID?.id;
}
}