UNPKG

@liara/cli

Version:

The command line interface for Liara

100 lines (99 loc) 3.61 kB
import inquirer from 'inquirer'; import Command from '../../../base.js'; import { Flags } from '@oclif/core'; import { createDebugLogger } from '../../../utils/output.js'; import { ux } from '@oclif/core'; import { RecordType } from '../../../types/dns-records.js'; class List extends Command { 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(); const tableData = data.map((record) => { // @ts-ignore let contents = []; 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) { 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, })); return zone; } } List.description = 'list all DNS records'; List.baseURL = 'https://dns-service.iran.liara.ir'; List.PATH = 'api/v1/zones/{zone}/dns-records'; List.aliases = ['zone:record:ls']; List.flags = { ...Command.flags, zone: Flags.string({ char: 'z', description: 'name of the zone (domain)', }), ...ux.table.flags(), }; export default List;