Skip to content

Commit 43e6fc9

Browse files
committed
Merge branch 'feat-create-resources' into feat-console-flow
2 parents 0081b07 + 569a24b commit 43e6fc9

File tree

10 files changed

+408
-233
lines changed

10 files changed

+408
-233
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: CLI beta Publish
2+
on:
3+
pull_request:
4+
paths:
5+
- 'templates/cli/**.twig'
6+
- 'src/SDK/Language/CLI.php'
7+
8+
env:
9+
PACKAGE_NAME: "${{ vars.PACKAGE_NAME }}@0.16.0${{ github.event.pull_request.head.sha }}"
10+
11+
jobs:
12+
publish:
13+
environment: cli-testing
14+
permissions:
15+
contents: write
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout repo
19+
uses: actions/checkout@v3
20+
- name: Setup Composer dependencies
21+
run: docker run --rm --volume "$(pwd)":/app composer install --ignore-platform-reqs
22+
- name: Generate SDKS
23+
run: docker run --rm -v "$(pwd)":/app -w /app php:8.1-cli php example.php
24+
- name: Fix permission
25+
run: sudo chown -R 1001:1001 examples
26+
- name: Setup Node
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 'latest'
30+
registry-url: 'https://registry.npmjs.org'
31+
- name: Setup
32+
working-directory: ./examples/cli/
33+
run: npm install
34+
- name: Set version
35+
working-directory: ./examples/cli/
36+
run: |
37+
sed -i "s#appwrite-cli#${{ vars.PACKAGE_NAME }}#g" package.json
38+
sed -i "s#0.16.0#0.16.0${{ github.event.pull_request.head.sha }}#g" package.json
39+
- name: Publish
40+
working-directory: examples/cli/
41+
run: npm publish --access public
42+
env:
43+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
44+
- name: Instruction
45+
run: |
46+
echo "Install it by running npm install ${{ env.PACKAGE_NAME }}"
47+
echo "Run it using npx ${{ env.PACKAGE_NAME }}"

example.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function getSSLPage($url) {
186186
->setTwitter('appwrite_io')
187187
->setDiscord('564160730845151244', 'https://appwrite.io/discord')
188188
->setDefaultHeaders([
189-
'X-Appwrite-Response-Format' => '0.15.0',
189+
'X-Appwrite-Response-Format' => '1.5.0',
190190
])
191191
;
192192

@@ -393,7 +393,7 @@ function getSSLPage($url) {
393393
;
394394

395395
$sdk->generate(__DIR__ . '/examples/apple');
396-
396+
397397
// DotNet
398398
$sdk = new SDK(new DotNet(), new Swagger2($spec));
399399

@@ -442,7 +442,7 @@ function getSSLPage($url) {
442442
// Android
443443

444444
$sdk = new SDK(new Android(), new Swagger2($spec));
445-
445+
446446
$sdk
447447
->setName('Android')
448448
->setNamespace('io appwrite')
@@ -466,7 +466,7 @@ function getSSLPage($url) {
466466

467467
// Kotlin
468468
$sdk = new SDK(new Kotlin(), new Swagger2($spec));
469-
469+
470470
$sdk
471471
->setName('Kotlin')
472472
->setNamespace('io appwrite')

src/SDK/Language/CLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ public function getFiles(): array
232232
],
233233
[
234234
'scope' => 'default',
235-
'destination' => 'lib/commands/create.js',
236-
'template' => 'cli/lib/commands/create.js.twig',
235+
'destination' => 'lib/commands/init.js',
236+
'template' => 'cli/lib/commands/init.js.twig',
237237
],
238238
[
239239
'scope' => 'default',

templates/cli/index.js.twig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ 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");
15-
const { create } = require("./lib/commands/create");
14+
const { login, logout, whoami } = require("./lib/commands/generic");
15+
const { init } = require("./lib/commands/init");
1616
const { pull } = require("./lib/commands/pull");
1717
const { push } = require("./lib/commands/push");
1818
{% endif %}
@@ -37,8 +37,9 @@ program
3737
})
3838
.showSuggestionAfterError()
3939
{% if sdk.test != "true" %}
40+
.addCommand(whoami)
4041
.addCommand(login)
41-
.addCommand(create)
42+
.addCommand(init)
4243
.addCommand(pull)
4344
.addCommand(push)
4445
.addCommand(logout)

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

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,73 @@ 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 login = new Command("login")
12-
.description(commandDescriptions['login'])
13-
.configureHelp({
14-
helpWidth: process.stdout.columns || 80
11+
const loginCommand = async () => {
12+
const answers = await inquirer.prompt(questionsLogin)
13+
14+
let client = await sdkForConsole(false);
15+
16+
await accountCreateEmailPasswordSession({
17+
email: answers.email,
18+
password: answers.password,
19+
parseOutput: false,
20+
sdk: client
1521
})
16-
.action(actionRunner(async () => {
17-
const answers = await inquirer.prompt(questionsLogin)
1822

19-
let client = await sdkForConsole(false);
23+
client.setCookie(globalConfig.getCookie());
2024

21-
await accountCreateEmailPasswordSession({
22-
email: answers.email,
23-
password: answers.password,
24-
parseOutput: false,
25-
sdk: client
26-
})
25+
let account;
26+
27+
try {
28+
account = await accountGet({
29+
sdk: client,
30+
parseOutput: false
31+
});
32+
} catch(error) {
33+
if (error.response === 'user_more_factors_required') {
34+
const { factor } = await inquirer.prompt(questionsListFactors);
35+
36+
const challenge = await accountCreateMfaChallenge({
37+
factor,
38+
parseOutput: false,
39+
sdk: client
40+
});
41+
42+
const { otp } = await inquirer.prompt(questionsMfaChallenge);
43+
44+
await accountUpdateMfaChallenge({
45+
challengeId: challenge.$id,
46+
otp,
47+
parseOutput: false,
48+
sdk: client
49+
});
50+
51+
account = await accountGet({
52+
sdk: client,
53+
parseOutput: false
54+
});
55+
} else {
56+
throw error;
57+
}
58+
}
59+
60+
success("Signed in as user with ID: " + account.$id);
61+
};
2762

28-
client.setCookie(globalConfig.getCookie());
63+
const whoami = new Command("whoami")
64+
.description(commandDescriptions['whoami'])
65+
.option("-j, --json", "Output in JSON format")
66+
.action(actionRunner(async ({ json }) => {
67+
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
68+
error("No user is signed in. To sign in, run: appwrite login ");
69+
return;
70+
}
71+
72+
let client = await sdkForConsole(false);
2973

3074
let account;
3175

@@ -34,37 +78,36 @@ const login = new Command("login")
3478
sdk: client,
3579
parseOutput: false
3680
});
37-
} catch(error) {
38-
if (error.response === 'user_more_factors_required') {
39-
const { factor } = await inquirer.prompt(questionsListFactors);
40-
41-
const challenge = await accountCreateMfaChallenge({
42-
factor,
43-
parseOutput: false,
44-
sdk: client
45-
});
46-
47-
const { otp } = await inquirer.prompt(questionsMfaChallenge);
48-
49-
await accountUpdateMfaChallenge({
50-
challengeId: challenge.$id,
51-
otp,
52-
parseOutput: false,
53-
sdk: client
54-
});
55-
56-
account = await accountGet({
57-
sdk: client,
58-
parseOutput: false
59-
});
60-
} else {
61-
throw error;
81+
} catch (error) {
82+
error("No user is signed in. To sign in, run: appwrite login");
83+
return;
84+
}
85+
86+
const data = [
87+
{
88+
'ID': account.$id,
89+
'Name': account.name,
90+
'Email': account.email,
91+
'MFA enabled': account.mfa ? 'Yes' : 'No'
6292
}
93+
];
94+
if (json) {
95+
console.log(data);
96+
97+
return;
6398
}
6499

65-
success("Signed in as user with ID: " + account.$id);
100+
drawTable(data)
66101
}));
67102

103+
104+
const login = new Command("login")
105+
.description(commandDescriptions['login'])
106+
.configureHelp({
107+
helpWidth: process.stdout.columns || 80
108+
})
109+
.action(actionRunner(loginCommand));
110+
68111
const logout = new Command("logout")
69112
.description(commandDescriptions['logout'])
70113
.configureHelp({
@@ -158,9 +201,11 @@ const client = new Command("client")
158201
}));
159202

160203
module.exports = {
161-
{% if sdk.test != "true" %}
204+
{% if sdk.test != "true" %}
205+
loginCommand,
206+
whoami,
162207
login,
163208
logout,
164-
{% endif %}
209+
{% endif %}
165210
client
166211
};

0 commit comments

Comments
 (0)