@liara/cli
Version:
The command line interface for Liara
97 lines (96 loc) • 3.19 kB
JavaScript
import { Flags, ux } from '@oclif/core';
import Command from '../../base.js';
import { IAAS_API_URL } from '../../constants.js';
import ora from 'ora';
import { promptVMs } from '../../utils/prompt-vms.js';
import { createDebugLogger } from '../../utils/output.js';
class Vminfo 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(Vminfo);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
this.spinner = ora();
try {
const vm = await this.getVm(flags.vm);
ux.Table.table([
{
Name: vm.name,
State: vm.state,
OS: vm.OS,
Power: vm.power,
Hostname: vm.config.hostname,
Password: vm.config.rootPassword,
IPs: vm.IPs.map((ip) => ip.address).join('\n'),
CPU: vm.planDetails.CPU.amount + 'Cores',
RAM: vm.planDetails.RAM.amount + 'GB',
Volume: vm.planDetails.volume + 'GB',
},
], {
Name: {},
State: {},
OS: {},
Power: {},
Hostname: {},
Password: {},
IPs: {},
CPU: {},
RAM: {},
Volume: {},
}, flags);
}
catch (error) {
debug(error.message);
if (error.response && error.response.data) {
debug(JSON.stringify(error.response.data));
}
if (error.response && error.response.statusCode === 404) {
this.error(`Could not find the vm.`);
}
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);
const vm = await this.got
.get(`vm/${vms[0]._id}`)
.json();
this.spinner.stop();
return vm;
}
const vms = await this.getVms('No running vm found.', (vm) => vm.state !== 'DELETING');
const selectedVm = await promptVMs(vms);
const vm = await this.got
.get(`vm/${selectedVm._id}`)
.json();
this.spinner.stop();
return vm;
}
catch (error) {
this.spinner.stop();
throw error;
}
}
}
Vminfo.description = 'show vm information';
Vminfo.aliases = ['vm:show', 'vm:inspect'];
Vminfo.flags = {
...Command.flags,
...ux.Table.table.flags(),
vm: Flags.string({
char: 'v',
description: 'vm name',
}),
};
export default Vminfo;