-
Notifications
You must be signed in to change notification settings - Fork 12
/
use.ts
62 lines (55 loc) · 1.75 KB
/
use.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from 'fs-extra';
import chalk from 'chalk';
import Command, { IAccounts } from '../../base.js';
import inquirer from 'inquirer';
import { Flags } from '@oclif/core';
import { GLOBAL_CONF_PATH, GLOBAL_CONF_VERSION } from '../../constants.js';
export default class AccountUse extends Command {
static description = 'select an account';
static flags = {
...Command.flags,
account: Flags.string({ char: 'a', description: 'account name' }),
};
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: IAccounts): Promise<string> {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'list',
message: 'Enter your account name:',
choices: [...Object.keys(accounts)],
})) as { name: string };
return name;
}
}