UNPKG

@liara/cli

Version:

The command line interface for Liara

83 lines (82 loc) 2.71 kB
import { Flags } from '@oclif/core'; import Command from '../../base.js'; import { IAAS_API_URL } from '../../constants.js'; import inquirer from 'inquirer'; import { createDebugLogger } from '../../utils/output.js'; import ora from 'ora'; import { promptVMs } from '../../utils/prompt-vms.js'; class VmDelete extends Command { async setGotConfig(config) { await super.setGotConfig(config); this.got = this.got.extend({ prefixUrl: IAAS_API_URL, }); } async run() { const { flags } = await this.parse(VmDelete); const debug = createDebugLogger(flags.debug); this.spinner = ora(); await this.setGotConfig(flags); try { const vm = await this.getVm(flags.vm); if (flags.force) { await this.got.delete(`vm/${vm._id}`); } else if (await this.confirm(vm)) { await this.got.delete(`vm/${vm._id}`); } this.log(`vm "${vm.name}" deleted.`); } catch (error) { debug(error.message); if (error.response && error.response.data) { debug(JSON.stringify(error.response.data)); } if (error.response && error.response.statusCode === 400) { this.error(`Invalid vm ID.`); } throw error; } } async getVm(vmFlag) { this.spinner.start('Loading...'); try { if (vmFlag) { const vms = await this.getVms('vm does not exist.', (vm) => vm.name === vmFlag && vm.state !== 'DELETING'); this.spinner.stop(); return vms[0]; } const vms = await this.getVms('Please go to https://console.liara.ir/vms or use liara vm create command and create an VM, first.', (vm) => vm.state !== 'DELETING'); this.spinner.stop(); const vm = await promptVMs(vms); return vm; } catch (error) { this.spinner.stop(); throw error; } } async confirm(vm) { const { confirmation } = (await inquirer.prompt({ name: 'confirmation', type: 'confirm', message: `Are you sure you want to delete "${vm.name}"?`, default: false, })); return confirmation; } } VmDelete.description = 'delete a vm'; VmDelete.flags = { ...Command.flags, vm: Flags.string({ char: 'v', description: 'vm name', }), force: Flags.boolean({ char: 'f', description: 'force the deletion', }), }; VmDelete.aliases = ['vm:delete', 'vm:rm']; export default VmDelete;