Skip to content

Commit 9387d95

Browse files
committed
feat(cli): Interactive multiple account changing
1 parent fd78fad commit 9387d95

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

templates/cli/lib/commands/generic.js.twig

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { Command } = require("commander");
33
const Client = require("../client");
44
const { sdkForConsole, questionGetEndpoint } = require("../sdks");
55
const { globalConfig, localConfig } = require("../config");
6-
const { actionRunner, success, parseBool, commandDescriptions, error, parse,log, drawTable } = require("../parser");
6+
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
77
{% if sdk.test != "true" %}
88
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
99
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
@@ -13,6 +13,20 @@ const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1';
1313

1414
const loginCommand = async ({ selfHosted }) => {
1515
const answers = await inquirer.prompt(questionsLogin);
16+
17+
if (answers.method === 'select') {
18+
const accountId = answers.accountId;
19+
20+
if (!globalConfig.getLoginIds().includes(accountId)) {
21+
throw Error('Login ID not found');
22+
}
23+
24+
globalConfig.setCurrentLogin(accountId);
25+
success(`Current account is ${accountId}`);
26+
27+
return;
28+
}
29+
1630
const oldCurrent = globalConfig.getCurrentLogin();
1731
const id = ID.unique();
1832

@@ -146,21 +160,6 @@ login
146160

147161
}));
148162

149-
login
150-
.command('change')
151-
.description("Change the current account")
152-
.option(`-a, --accountId <accountId>`, `Login ID`)
153-
.action(actionRunner(async ({ accountId }) => {
154-
const loginIds = globalConfig.getLoginIds();
155-
156-
if (!loginIds.includes(accountId)) {
157-
throw Error('Login ID not found');
158-
}
159-
160-
globalConfig.setCurrentLogin(accountId);
161-
success(`Current account is ${accountId}`);
162-
}));
163-
164163
const logout = new Command("logout")
165164
.description(commandDescriptions['logout'])
166165
.configureHelp({
@@ -253,7 +252,7 @@ const client = new Command("client")
253252
success()
254253
}));
255254

256-
const migrate = async ()=>{
255+
const migrate = async () => {
257256
if (!globalConfig.has('endpoint') || !globalConfig.has('cookie')) {
258257
return;
259258
}

templates/cli/lib/questions.js.twig

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { localConfig } = require('./config');
1+
const chalk = require("chalk");
2+
const { localConfig, globalConfig } = require('./config');
23
const { projectsList } = require('./commands/projects');
34
const { teamsList } = require('./commands/teams');
45
const { functionsListRuntimes } = require('./commands/functions');
@@ -126,7 +127,7 @@ const questionsPullProject = [
126127
message: "Choose the project organization",
127128
choices: async () => {
128129
let client = await sdkForConsole(true);
129-
const { teams } = await paginate(teamsList, { parseOutput: false , sdk: client}, 100, 'teams');
130+
const { teams } = await paginate(teamsList, { parseOutput: false, sdk: client }, 100, 'teams');
130131

131132
let choices = teams.map((team, idx) => {
132133
return {
@@ -194,7 +195,7 @@ const questionsPullProject = [
194195
choices: async (answers) => {
195196
let response = await projectsList({
196197
parseOutput: false,
197-
queries: [JSON.stringify({ method: 'equal', attribute:'teamId', values: [answers.organization.id] })],
198+
queries: [JSON.stringify({ method: 'equal', attribute: 'teamId', values: [answers.organization.id] })],
198199
})
199200
let projects = response["projects"]
200201
let choices = projects.map((project, idx) => {
@@ -245,7 +246,7 @@ const questionsPullFunction = [
245246
id: runtime['$id'],
246247
entrypoint: getEntrypoint(runtime['$id']),
247248
ignore: getIgnores(runtime['$id']),
248-
commands : getInstallCommand(runtime['$id'])
249+
commands: getInstallCommand(runtime['$id'])
249250
},
250251
}
251252
})
@@ -280,6 +281,16 @@ const questionsPullCollection = [
280281
];
281282

282283
const questionsLogin = [
284+
{
285+
type: "list",
286+
name: "method",
287+
message: "You're already logged in, what you like to do?",
288+
choices: [
289+
{ name: 'Login to a different account', value: 'login' },
290+
{ name: 'Change to a different existed account', value: 'select' }
291+
],
292+
when: () => globalConfig.getCurrentLogin() !== ''
293+
},
283294
{
284295
type: "input",
285296
name: "email",
@@ -290,6 +301,7 @@ const questionsLogin = [
290301
}
291302
return true;
292303
},
304+
when: (answers) => answers.method === 'login'
293305
},
294306
{
295307
type: "password",
@@ -301,8 +313,33 @@ const questionsLogin = [
301313
return "Please enter your password";
302314
}
303315
return true;
304-
}
316+
},
317+
when: (answers) => answers.method === 'login'
305318
},
319+
{
320+
type: "list",
321+
name: "accountId",
322+
message: "Select an account to switch to",
323+
choices() {
324+
const logins = globalConfig.getLogins();
325+
const current = globalConfig.getCurrentLogin();
326+
327+
const data = [];
328+
329+
const longestEmail = logins.reduce((prev, current) => (prev && prev.email > current.email) ? prev : current).email.length;
330+
331+
logins.forEach((login) => {
332+
data.push({
333+
value: login.id,
334+
name: `${login.email.padEnd(longestEmail)} ${current === login.id ? chalk.green.bold('In use') : ' '.repeat(6)} ${login.endpoint}`,
335+
});
336+
})
337+
338+
return data;
339+
},
340+
when: (answers) => answers.method === 'select'
341+
},
342+
306343
];
307344

308345
const questionsPushResources = [
@@ -494,7 +531,7 @@ const questionsListFactors = [
494531
name: `Recovery code`,
495532
value: 'recoveryCode'
496533
}
497-
].filter((ch) => factors[ch.value] === true);
534+
].filter((ch) => factors[ch.value] === true);
498535

499536
return choices;
500537
}

0 commit comments

Comments
 (0)