-
Notifications
You must be signed in to change notification settings - Fork 12
/
list.ts
118 lines (99 loc) · 3.08 KB
/
list.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
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 { RecordType, IDNSRecords } from '../../../types/dns-records.js';
export default class List extends Command {
static description = 'list all DNS records';
static baseURL = 'https://dns-service.iran.liara.ir';
static PATH = 'api/v1/zones/{zone}/dns-records';
static aliases = ['zone:record:ls'];
static flags = {
...Command.flags,
zone: Flags.string({
char: 'z',
description: 'name of the zone (domain)',
}),
...ux.table.flags(),
};
async run() {
const { flags } = await this.parse(List);
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());
try {
const { data } = await this.got(
List.PATH.replace('{zone}', zone)
).json<IDNSRecords>();
const tableData = data.map((record) => {
// @ts-ignore
let contents: [string] = [];
switch (record.type) {
case RecordType.A:
case RecordType.AAAA:
record.contents.map((rec) => {
// @ts-ignore
contents.push(rec.ip);
});
break;
case RecordType.ALIAS:
case RecordType.CNAME:
case RecordType.MX:
case RecordType.SRV:
record.contents.map((rec) => {
// @ts-ignore
contents.push(rec.host);
});
break;
case RecordType.TXT:
record.contents.map((rec) => {
// @ts-ignore
contents.push(rec.text);
});
break;
default:
this.error('Unknown error in showing records');
}
return {
id: record.id,
name: record.name,
type: record.type,
ttl: record.ttl,
contents: contents.join('\n'),
};
});
const columnConfig = {
id: {},
name: {},
type: {},
ttl: {},
contents: {},
};
ux.table(tableData, columnConfig, flags);
} 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: List.baseURL });
this.got = new_got; // baseURL is different for zone api
}
async promptZone() {
const { zone } = (await inquirer.prompt({
name: 'zone',
type: 'input',
message: 'Enter domain:',
validate: (input) => input.length > 2,
})) as { zone: string };
return zone;
}
}