UNPKG

@liara/cli

Version:

The command line interface for Liara

124 lines (123 loc) 4.59 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'; class Resize extends Command { async run() { this.spinner = ora(); const { flags } = await this.parse(Resize); const debug = createDebugLogger(flags.debug); await this.setGotConfig(flags); const hostname = flags.name || (await this.promptHostname()); const disk = flags.disk === 'y' ? true : flags.disk === 'n' ? false : (await this.promptDisk()) === 'y' ? true : false; try { const database = await this.getDatabaseByHostname(hostname); if (database === undefined) { this.log(`Database ${hostname} not found`); return; } const planID = flags.plan || (await this.promptPlan(database.type)); const databaseID = database._id; const result = await this.got.post(`v1/databases/${databaseID}/resize`, { json: { planID: planID, disk: disk }, }); this.log(`Database ${hostname} changed to plan ${planID}.`); } catch (error) { debug(error.message); if (error.response && error.response.body) { debug(JSON.stringify(error.response.body)); } this.error(`Could not change the plan now. Please try again later.`); } } async promptHostname() { const { name } = (await inquirer.prompt({ name: 'name', type: 'input', message: 'Enter name:', validate: (input) => input.length > 2, })); return name; } 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; } async promptPlan(databaseType) { this.spinner.start('Loading...'); try { const { plans } = await this.got('v1/me').json(); this.spinner.stop(); const { plan } = (await inquirer.prompt({ name: 'plan', type: 'list', message: 'Please select new plan:', choices: [ ...Object.keys(plans.databases) .filter((plan) => { if ((plan === 'free' || plan.includes('g2')) && plans.databases[plan].available && plans.databases[plan].supports.includes(databaseType)) { return true; } }) .map((plan) => { const availablePlan = plans.databases[plan]; const ram = availablePlan.RAM.amount; const cpu = availablePlan.CPU.amount; const disk = availablePlan.volume; const price = availablePlan.price * 720; const storageClass = availablePlan.storageClass; return { value: plan, name: `RAM: ${ram}GB, CPU: ${cpu} Core, Disk: ${disk}GB, ${storageClass || 'SSD'}, Price: ${price.toLocaleString()} Tomans/Month `, }; }), ], })); return plan; } catch (error) { this.spinner.stop(); throw error; } } async promptDisk() { const { disk } = (await inquirer.prompt({ name: 'disk', type: 'input', message: 'extend disk size? (y/n):', validate: (input) => input === 'y' || input === 'n', })); return disk; } } Resize.description = 'resize a database'; Resize.flags = { ...Command.flags, name: Flags.string({ char: 'n', description: 'name of your database', }), plan: Flags.string({ description: 'new plan name', }), disk: Flags.string({ char: 'd', description: 'extend disk size or not', }), }; export default Resize;