Skip to content

Commit 4b3a3e1

Browse files
Merge pull request #850 from appwrite/feat-cli-whoami-command
feat(cli): Adding console whoami command
2 parents c91aa21 + ee66125 commit 4b3a3e1

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

templates/cli/index.js.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { version } = require("./package.json");
1111
const { commandDescriptions, cliConfig } = require("./lib/parser");
1212
const { client } = require("./lib/commands/generic");
1313
{% if sdk.test != "true" %}
14-
const { login, logout } = require("./lib/commands/generic");
14+
const { login, logout, whoami } = require("./lib/commands/generic");
1515
const { pull } = require("./lib/commands/pull");
1616
const { push } = require("./lib/commands/push");
1717
{% endif %}
@@ -36,6 +36,7 @@ program
3636
})
3737
.showSuggestionAfterError()
3838
{% if sdk.test != "true" %}
39+
.addCommand(whoami)
3940
.addCommand(login)
4041
.addCommand(pull)
4142
.addCommand(push)

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,51 @@ const { Command } = require("commander");
33
const Client = require("../client");
44
const { sdkForConsole } = require("../sdks");
55
const { globalConfig, localConfig } = require("../config");
6-
const { actionRunner, success, parseBool, commandDescriptions, log, parse } = require("../parser");
6+
const { actionRunner, success, parseBool, commandDescriptions, error, parse, drawTable } = require("../parser");
77
{% if sdk.test != "true" %}
88
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
99
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1010

11+
const whoami = new Command("whoami")
12+
.description(commandDescriptions['whoami'])
13+
.option("-j, --json", "Output in JSON format")
14+
.action(actionRunner(async ({ json }) => {
15+
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
16+
error("No user is signed in. To sign in, run: appwrite login ");
17+
return;
18+
}
19+
20+
let client = await sdkForConsole(false);
21+
22+
let account;
23+
24+
try {
25+
account = await accountGet({
26+
sdk: client,
27+
parseOutput: false
28+
});
29+
} catch (error) {
30+
error("No user is signed in. To sign in, run: appwrite login");
31+
return;
32+
}
33+
34+
const data = [
35+
{
36+
'ID': account.$id,
37+
'Name': account.name,
38+
'Email': account.email,
39+
'MFA enabled': account.mfa ? 'Yes' : 'No'
40+
}
41+
];
42+
if (json) {
43+
console.log(data);
44+
45+
return;
46+
}
47+
48+
drawTable(data)
49+
}));
50+
1151
const login = new Command("login")
1252
.description(commandDescriptions['login'])
1353
.configureHelp({
@@ -34,7 +74,7 @@ const login = new Command("login")
3474
sdk: client,
3575
parseOutput: false
3676
});
37-
} catch(error) {
77+
} catch (error) {
3878
if (error.response === 'user_more_factors_required') {
3979
const { factor } = await inquirer.prompt(questionsListFactors);
4080

@@ -158,9 +198,10 @@ const client = new Command("client")
158198
}));
159199

160200
module.exports = {
161-
{% if sdk.test != "true" %}
201+
{% if sdk.test != "true" %}
202+
whoami,
162203
login,
163204
logout,
164-
{% endif %}
205+
{% endif %}
165206
client
166207
};

templates/cli/lib/parser.js.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ const commandDescriptions = {
168168
"client": `The client command allows you to configure your CLI`,
169169
"login": `The login command allows you to authenticate and manage a user account.`,
170170
"logout": `The logout command allows you to logout of your {{ spec.title|caseUcfirst }} account.`,
171+
"whoami": `The whoami command gives information about the currently logged in user.`,
171172
"console" : `The console command allows gives you access to the APIs used by the Appwrite console.`,
172173
"assistant": `The assistant command allows you to interact with the Appwrite Assistant AI`,
173174
"messaging": `The messaging command allows you to send messages.`,
@@ -184,6 +185,7 @@ const commandDescriptions = {
184185
}
185186

186187
module.exports = {
188+
drawTable,
187189
parse,
188190
actionRunner,
189191
parseInteger,

0 commit comments

Comments
 (0)