@liara/cli
Version:
The command line interface for Liara
82 lines (81 loc) • 2.99 kB
JavaScript
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';
class Remove extends Command {
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) {
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,
}));
return name;
}
async promptZone() {
const { zone } = (await inquirer.prompt({
name: 'zone',
type: 'input',
message: 'Enter domain:',
validate: (input) => input.length > 2,
}));
return zone;
}
async getRecordIDByName(zone, name) {
const { data } = await this.got('api/v1/zones/{zone}/dns-records'.replace('{zone}', zone)).json();
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 === null || recordID === void 0 ? void 0 : recordID.id;
}
}
Remove.description = 'remove a DNS record';
Remove.baseURL = 'https://dns-service.iran.liara.ir';
Remove.PATH = 'api/v1/zones/{zone}/dns-records/{id}';
Remove.aliases = ['zone:record:rm'];
Remove.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(),
};
export default Remove;