-
Notifications
You must be signed in to change notification settings - Fork 12
/
list.ts
84 lines (68 loc) · 2.33 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
import chalk from 'chalk';
import Command, { IConfig } from '../../base.js';
import { createDebugLogger } from '../../utils/output.js';
import { ux } from '@oclif/core';
import * as shamsi from 'shamsi-date-converter';
import moment from 'moment';
import { IZones } from '../../types/zone.js';
export default class List extends Command {
static description = 'list all zones';
static baseURL = 'https://dns-service.iran.liara.ir';
static PATH = 'api/v1/zones';
static aliases = ['zone:ls'];
static flags = {
...Command.flags,
...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.');
try {
const { data } = await this.got(List.PATH).json<IZones>();
const zonesData = data.map((zone) => {
const createdAtshamsiData = shamsi.gregorianToJalali(
new Date(zone.createdAt)
);
const lastCheckAt = new Date(zone.lastCheckAt);
const lastCheckDuration = moment
.duration(moment(lastCheckAt).diff(moment(Date.now())))
.humanize(true);
return {
Name: zone.name,
status:
zone.status === 'ACTIVE'
? chalk.green('ACTIVE')
: chalk.gray('PENDING'),
'created At': `${createdAtshamsiData[0]}-${createdAtshamsiData[1]}-${createdAtshamsiData[2]}`,
'lastCheck At': lastCheckDuration,
'current Name Servers': zone.currentNameServers.join(',\n'),
'name Servers': zone.nameServers.join(',\n'),
};
});
ux.table(
zonesData,
{
Name: {},
status: {},
'created At': {},
'lastCheck At': {},
'current Name Servers': {},
'name Servers': {},
},
flags
);
} catch (error) {
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
}
}