UNPKG

@liara/cli

Version:

The command line interface for Liara

98 lines (97 loc) 3.49 kB
import ora from 'ora'; 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() { this.spinner = ora(); const { flags } = await this.parse(Remove); 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 hostname = flags.name || (await this.promptHostname()); const sayYes = flags.yes; try { const database = await this.getDatabaseByHostname(hostname); if (database === undefined) { this.log(`Database ${hostname} not found`); return; } const tableData = [ { type: database.type, hostname: database.hostname, status: database.status.toString(), }, ]; const tableConfig = { Hostname: { get: (row) => row.hostname }, Type: { get: (row) => row.type }, status: { get: (row) => row.status }, }; ux.table(tableData, tableConfig, { title: 'Database Specification', }); if (!sayYes && (await this.promptContinue()) === 'n') { this.log('Operation cancelled'); return; } const databaseID = database._id; await this.got.delete(Remove.PATH.replace('{database-id}', databaseID)); this.log(`Database ${hostname} removed.`); } catch (error) { debug(error.message); if (error.response && error.response.body) { debug(JSON.stringify(error.response.body)); } this.error(`Could not remove the database. Please try again.`); } } async promptHostname() { const { name } = (await inquirer.prompt({ name: 'name', type: 'input', message: 'Enter name:', validate: (input) => input.length > 2, })); return name; } async promptContinue() { const { yes } = (await inquirer.prompt({ name: 'yes', type: 'input', message: 'continue? (y/n):', validate: (input) => input === 'y' || input === 'n', })); return yes; } async getDatabaseByHostname(hostname) { const { databases } = await this.got('v1/databases').json(); if (!databases.length) { this.error(`Not found any database. Please open up https://console.liara.ir/databases and create the database, first.`); } const database = databases.find((database) => database.hostname === hostname); return database; } } Remove.description = 'remove a database'; Remove.PATH = 'v1/databases/{database-id}'; Remove.aliases = ['db:rm']; Remove.flags = { ...Command.flags, name: Flags.string({ char: 'n', description: 'name of your database', }), yes: Flags.boolean({ char: 'y', description: 'say yes to continue prompt', }), }; export default Remove;