@liara/cli
Version:
The command line interface for Liara
48 lines (47 loc) • 1.83 kB
JavaScript
import fs from 'fs-extra';
import chalk from 'chalk';
import Command from '../../base.js';
import inquirer from 'inquirer';
import { Flags } from '@oclif/core';
import { GLOBAL_CONF_PATH, GLOBAL_CONF_VERSION } from '../../constants.js';
class AccountUse extends Command {
async run() {
const { flags } = await this.parse(AccountUse);
const liara_json = await this.readGlobalConfig();
if (!liara_json ||
!liara_json.accounts ||
Object.keys(liara_json.accounts).length === 0) {
this.error("Please add your accounts via 'liara account:add' command, first.");
}
const name = flags.account || (await this.promptName(liara_json.accounts));
const selectedAccount = liara_json.accounts[name];
!selectedAccount &&
this.error(`Could not find any account associated with this name ${name}.`);
for (const account of Object.keys(liara_json.accounts)) {
liara_json.accounts[account].current = false;
if (name === account) {
liara_json.accounts[account].current = true;
}
}
fs.writeFileSync(GLOBAL_CONF_PATH, JSON.stringify({
version: GLOBAL_CONF_VERSION,
accounts: liara_json.accounts,
}));
this.log(chalk.green('> Auth credentials changed.'));
}
async promptName(accounts) {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'list',
message: 'Enter your account name:',
choices: [...Object.keys(accounts)],
}));
return name;
}
}
AccountUse.description = 'select an account';
AccountUse.flags = {
...Command.flags,
account: Flags.string({ char: 'a', description: 'account name' }),
};
export default AccountUse;