Skip to content

Commit f7e92bc

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/feat-console-flow' into feat-pull-project-settings
2 parents 224c3fd + 7051e44 commit f7e92bc

File tree

6 files changed

+314
-3
lines changed

6 files changed

+314
-3
lines changed

src/SDK/Language.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public function getFilters(): array
8484
return [];
8585
}
8686

87+
/**
88+
* Language specific functions.
89+
* @return array
90+
*/
91+
public function getFunctions(): array
92+
{
93+
return [];
94+
}
95+
8796
protected function toPascalCase(string $value): string
8897
{
8998
return \ucfirst($this->toCamelCase($value));

src/SDK/Language/CLI.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,66 @@
22

33
namespace Appwrite\SDK\Language;
44

5+
use Twig\TwigFunction;
6+
57
class CLI extends Node
68
{
9+
/**
10+
* List of functions to ignore for console preview.
11+
* @var array
12+
*/
13+
private $consoleIgnoreFunctions = [
14+
'listidentities',
15+
'listmfafactors',
16+
'getprefs',
17+
'getsession',
18+
'getattribute',
19+
'listdocumentlogs',
20+
'getindex',
21+
'listcollectionlogs',
22+
'getcollectionusage',
23+
'listlogs',
24+
'listruntimes',
25+
'getusage',
26+
'getusage',
27+
'listvariables',
28+
'getvariable',
29+
'listproviderlogs',
30+
'listsubscriberlogs',
31+
'getsubscriber',
32+
'listtopiclogs',
33+
'getemailtemplate',
34+
'getsmstemplate',
35+
'getfiledownload',
36+
'getfilepreview',
37+
'getfileview',
38+
'getusage',
39+
'listlogs',
40+
'getprefs',
41+
'getusage',
42+
'listlogs',
43+
'getmembership',
44+
'listmemberships',
45+
'listmfafactors',
46+
'getmfarecoverycodes',
47+
'getprefs',
48+
'listtargets',
49+
'gettarget',
50+
];
51+
52+
/**
53+
* List of SDK services to ignore for console preview.
54+
* @var array
55+
*/
56+
private $consoleIgnoreServices = [
57+
'health',
58+
'migrations',
59+
'locale',
60+
'avatars',
61+
'project',
62+
'proxy',
63+
'vcs'
64+
];
765
/**
866
* @var array
967
*/
@@ -290,4 +348,18 @@ public function getParamExample(array $param): string
290348

291349
return $output;
292350
}
351+
352+
/**
353+
* Language specific filters.
354+
* @return array
355+
*/
356+
public function getFunctions(): array
357+
{
358+
return [
359+
/** Return true if the entered service->method is enabled for a console preview link */
360+
new TwigFunction('methodHaveConsolePreview', fn($method, $service) => preg_match('/^([Gg]et|[Ll]ist)/', $method)
361+
&& !in_array(strtolower($method), $this->consoleIgnoreFunctions)
362+
&& !in_array($service, $this->consoleIgnoreServices)),
363+
];
364+
}
293365
}

src/SDK/SDK.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public function __construct(Language $language, Spec $spec)
8585
'debug' => true
8686
]);
8787

88+
/**
89+
* Add language-specific functions
90+
*/
91+
foreach ($this->language->getFunctions() as $function) {
92+
$this->twig->addFunction($function);
93+
}
94+
8895
/**
8996
* Add language specific filters
9097
*/

templates/cli/base/requests/api.twig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@
1717
fs.writeFileSync(destination, response);
1818
{%~ endif %}
1919
if (parseOutput) {
20+
{%~ if methodHaveConsolePreview(method.name,service.name) %}
21+
if(console) {
22+
showConsoleLink('{{service.name}}', '{{ method.name }}',open
23+
{%- for parameter in method.parameters.path -%}{%- set param = (parameter.name | caseCamel | escapeKeyword) -%}{%- if param ends with 'Id' -%}, {{ param }} {%- endif -%}{%- endfor -%}
24+
);
25+
} else {
26+
parse(response)
27+
success()
28+
}
29+
{%~ else %}
2030
parse(response)
2131
success()
32+
{%~ endif %}
2233
}
2334

2435
return response;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const tar = require("tar");
44
const ignore = require("ignore");
55
const { promisify } = require('util');
66
const libClient = require('../client.js');
7-
const { getAllFiles } = require('../utils.js');
7+
const { getAllFiles, showConsoleLink } = require('../utils.js');
88
const { Command } = require('commander');
99
const { sdkForProject, sdkForConsole } = require('../sdks')
1010
const { parse, actionRunner, parseInteger, parseBool, commandDescriptions, success, log } = require('../parser')
@@ -70,6 +70,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
7070
{%- if 'multipart/form-data' in method.consumes -%},onProgress = () => {}{%- endif -%}
7171

7272
{%- if method.type == 'location' -%}, destination{%- endif -%}
73+
{% if methodHaveConsolePreview(method.name,service.name) %}, console, open{%- endif -%}
7374
}) => {
7475
{%~ endblock %}
7576
let client = !sdk ? await {% if service.name == "projects" %}sdkForConsole(){% else %}sdkForProject(){% endif %} :
@@ -94,6 +95,10 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
9495
{% if method.type == 'location' %}
9596
.requiredOption(`--destination <path>`, `output file path.`)
9697
{% endif %}
98+
{% if methodHaveConsolePreview(method.name,service.name) %}
99+
.option(`--console`, `Get the resource console url`)
100+
.option(`--open`, `Use with '--console' to open the using default browser`)
101+
{% endif %}
97102
{% endautoescape %}
98103
.action(actionRunner({{ service.name | caseLower }}{{ method.name | caseUcfirst }}))
99104

templates/cli/lib/utils.js.twig

Lines changed: 209 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const fs = require("fs");
22
const path = require("path");
3+
const { localConfig, globalConfig } = require("./config");
4+
const { success, log } = require('./parser')
5+
const readline = require('readline');
6+
const cp = require('child_process');
37

48
function getAllFiles(folder) {
59
const files = [];
6-
for(const pathDir of fs.readdirSync(folder)) {
10+
for (const pathDir of fs.readdirSync(folder)) {
711
const pathAbsolute = path.join(folder, pathDir);
812
if (fs.statSync(pathAbsolute).isDirectory()) {
913
files.push(...getAllFiles(pathAbsolute));
@@ -20,7 +24,210 @@ const checkDeployConditions = (localConfig) => {
2024
}
2125
}
2226

27+
function showConsoleLink(serviceName, action, open, ...ids) {
28+
const projectId = localConfig.getProject().projectId;
29+
30+
const url = new URL(globalConfig.getEndpoint().replace('/v1', '/console'));
31+
url.pathname += `/project-${projectId}`;
32+
action = action.toLowerCase();
33+
34+
switch (serviceName) {
35+
case "account":
36+
url.pathname = url.pathname.replace(`/project-${projectId}`, '');
37+
url.pathname += getAccountPath(action);
38+
break;
39+
case "databases":
40+
url.pathname += getDatabasePath(action, ids);
41+
break;
42+
case "functions":
43+
url.pathname += getFunctionsPath(action, ids);
44+
break;
45+
case "messaging":
46+
url.pathname += getMessagingPath(action, ids);
47+
break;
48+
case "projects":
49+
url.pathname = url.pathname.replace(`/project-${projectId}`, '');
50+
url.pathname += getProjectsPath(action, ids);
51+
break;
52+
case "storage":
53+
url.pathname += getBucketsPath(action, ids);
54+
break;
55+
case "teams":
56+
url.pathname += getTeamsPath(action, ids);
57+
break;
58+
case "users":
59+
url.pathname += getUsersPath(action, ids);
60+
break;
61+
default:
62+
return;
63+
}
64+
65+
66+
success(url);
67+
68+
if (open) {
69+
const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
70+
cp.exec(`${start} ${url}`);
71+
}
72+
}
73+
74+
function getAccountPath(action) {
75+
let path = '/account';
76+
77+
if (action === 'listsessions') {
78+
path += '/sessions';
79+
}
80+
81+
return path;
82+
}
83+
84+
function getDatabasePath(action, ids) {
85+
let path = '/databases';
86+
87+
88+
if (['get', 'listcollections', 'getcollection', 'listattributes', 'listdocuments', 'getdocument', 'listindexes', 'getdatabaseusage'].includes(action)) {
89+
path += `/database-${ids[0]}`;
90+
}
91+
92+
if (action === 'getdatabaseusage') {
93+
path += `/usage`;
94+
}
95+
96+
if (['getcollection', 'listattributes', 'listdocuments', 'getdocument', 'listindexes'].includes(action)) {
97+
path += `/collection-${ids[1]}`;
98+
}
99+
100+
if (action === 'listattributes') {
101+
path += '/attributes';
102+
}
103+
if (action === 'listindexes') {
104+
path += '/indexes';
105+
}
106+
if (action === 'getdocument') {
107+
path += `/document-${ids[2]}`;
108+
}
109+
110+
111+
return path;
112+
}
113+
114+
function getFunctionsPath(action, ids) {
115+
let path = '/functions';
116+
117+
if (action !== 'list') {
118+
path += `/function-${ids[0]}`;
119+
}
120+
121+
if (action === 'getdeployment') {
122+
path += `/deployment-${ids[1]}`
123+
}
124+
125+
if (action === 'getexecution' || action === 'listexecution') {
126+
path += `/executions`
127+
}
128+
if (action === 'getfunctionusage') {
129+
path += `/usage`
130+
}
131+
132+
return path;
133+
}
134+
135+
function getMessagingPath(action, ids) {
136+
let path = '/messaging';
137+
138+
if (['getmessage', 'listmessagelogs'].includes(action)) {
139+
path += `/message-${ids[0]}`;
140+
}
141+
142+
if (['listproviders', 'getprovider'].includes(action)) {
143+
path += `/providers`;
144+
}
145+
146+
if (action === 'getprovider') {
147+
path += `/provider-${ids[0]}`;
148+
}
149+
150+
if (['listtopics', 'gettopic'].includes(action)) {
151+
path += `/topics`;
152+
}
153+
154+
if (action === 'gettopic') {
155+
path += `/topic-${ids[0]}`;
156+
}
157+
158+
return path;
159+
}
160+
161+
function getProjectsPath(action, ids) {
162+
let path = '';
163+
164+
if (action !== 'list') {
165+
path += `/project-${ids[0]}`;
166+
}
167+
168+
if (['listkeys', 'getkey'].includes(action)) {
169+
path += '/overview/keys'
170+
}
171+
172+
if (['listplatforms', 'getplatform'].includes(action)) {
173+
path += '/overview/platforms'
174+
}
175+
176+
if (['listwebhooks', 'getwebhook'].includes(action)) {
177+
path += '/settings/webhooks'
178+
}
179+
180+
if (['getplatform', 'getkey', 'getwebhook'].includes(action)) {
181+
path += `/${ids[1]}`;
182+
}
183+
184+
return path;
185+
}
186+
187+
function getBucketsPath(action, ids) {
188+
let path = '/storage';
189+
190+
if (action !== 'listbuckets') {
191+
path += `/bucket-${ids[0]}`;
192+
}
193+
194+
if (action === 'getbucketusage') {
195+
path += `/usage`
196+
}
197+
198+
if (action === 'getfile') {
199+
path += `/file-${ids[1]}`
200+
}
201+
202+
return path;
203+
}
204+
205+
function getTeamsPath(action, ids) {
206+
let path = '/auth/teams';
207+
208+
if (action !== 'list') {
209+
path += `/team-${ids[0]}`;
210+
}
211+
212+
return path;
213+
}
214+
215+
function getUsersPath(action, ids) {
216+
let path = '/auth';
217+
218+
if (action !== 'list') {
219+
path += `/user-${ids[0]}`;
220+
}
221+
222+
if (action === 'listsessions') {
223+
path += 'sessions';
224+
}
225+
226+
return path;
227+
}
228+
23229
module.exports = {
24230
getAllFiles,
25-
checkDeployConditions
231+
checkDeployConditions,
232+
showConsoleLink
26233
};

0 commit comments

Comments
 (0)