Skip to content

Commit 0081b07

Browse files
committed
feat(cli): Expanding console flow to list and various get commands
1 parent d66b6ae commit 0081b07

File tree

3 files changed

+181
-27
lines changed

3 files changed

+181
-27
lines changed

templates/cli/base/requests/api.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
fs.writeFileSync(destination, response);
1818
{%~ endif %}
1919
if (parseOutput) {
20-
{%~ if method.name == 'get' and service.name not in ['health','migrations','locale'] %}
20+
{%~ if methodHaveConsolePreview(method.name,service.name) %}
2121
if(console) {
22-
showConsoleLink('{{service.name}}', 'get'
23-
{%- for parameter in method.parameters.path -%}{%- set param = (parameter.name | caseCamel | escapeKeyword) -%},open {%- if param ends with 'Id' -%}, {{ param }} {%- endif -%}{%- endfor -%}
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 -%}
2424
);
2525
} else {
2626
parse(response)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +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 method.name == 'get' -%}, console, open{%- endif -%}
73+
{% if methodHaveConsolePreview(method.name,service.name) %}, console, open{%- endif -%}
7474
}) => {
7575
{%~ endblock %}
7676
let client = !sdk ? await {% if service.name == "projects" %}sdkForConsole(){% else %}sdkForProject(){% endif %} :
@@ -95,7 +95,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
9595
{% if method.type == 'location' %}
9696
.requiredOption(`--destination <path>`, `output file path.`)
9797
{% endif %}
98-
{% if method.name == 'get' %}
98+
{% if methodHaveConsolePreview(method.name,service.name) %}
9999
.option(`--console`, `Get the resource console url`)
100100
.option(`--open`, `Use with '--console' to open the using default browser`)
101101
{% endif %}

templates/cli/lib/utils.js.twig

Lines changed: 176 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,45 @@ function getAllFiles(folder) {
1818
return files;
1919
}
2020

21-
function showConsoleLink(serviceName, action, open, id = '') {
22-
let resource = '';
23-
let service = '';
21+
22+
function showConsoleLink(serviceName, action, open, ...ids) {
23+
const projectId = localConfig.getProject().projectId;
24+
25+
const url = new URL(globalConfig.getEndpoint().replace('/v1', '/console'));
26+
url.pathname += `/project-${projectId}`;
27+
action = action.toLowerCase();
2428

2529
switch (serviceName) {
2630
case "account":
27-
service = 'account';
31+
url.pathname = url.pathname.replace(`/project-${projectId}`, '');
32+
url.pathname += getAccountPath(action);
2833
break;
2934
case "databases":
30-
resource = 'database';
31-
service = 'databases';
35+
url.pathname += getDatabasePath(action, ids);
3236
break;
3337
case "functions":
34-
resource = 'function';
35-
service = 'functions';
38+
url.pathname += getFunctionsPath(action, ids);
39+
break;
40+
case "messaging":
41+
url.pathname += getMessagingPath(action, ids);
3642
break;
3743
case "projects":
38-
service = `project-${id}`;
39-
id = '';
44+
url.pathname = url.pathname.replace(`/project-${projectId}`, '');
45+
url.pathname += getProjectsPath(action, ids);
46+
break;
47+
case "storage":
48+
url.pathname += getBucketsPath(action, ids);
4049
break;
4150
case "teams":
42-
resource = 'team';
43-
service = 'auth/teams';
51+
url.pathname += getTeamsPath(action, ids);
4452
break;
45-
4653
case "users":
47-
resource = 'user';
48-
service = 'auth';
54+
url.pathname += getUsersPath(action, ids);
4955
break;
5056
default:
5157
return;
5258
}
5359

54-
const baseUrl = globalConfig.getEndpoint().replace('/v1', '');
55-
56-
const end = action === 'get' ? (id ? `/${resource}-${id}` : `/${resource}`) : '';
57-
const projectId = localConfig.getProject().projectId;
58-
const middle = resource !== '' ? `/project-${projectId}` : '';
59-
const url = `${baseUrl}/console${middle}/${service}${end}`
60-
6160

6261
success(url);
6362

@@ -67,6 +66,161 @@ function showConsoleLink(serviceName, action, open, id = '') {
6766
}
6867
}
6968

69+
function getAccountPath(action) {
70+
let path = '/account';
71+
72+
if (action === 'listsessions') {
73+
path += '/sessions';
74+
}
75+
76+
return path;
77+
}
78+
79+
function getDatabasePath(action, ids) {
80+
let path = '/databases';
81+
82+
83+
if (['get', 'listcollections', 'getcollection', 'listattributes', 'listdocuments', 'getdocument', 'listindexes', 'getdatabaseusage'].includes(action)) {
84+
path += `/database-${ids[0]}`;
85+
}
86+
87+
if (action === 'getdatabaseusage') {
88+
path += `/usage`;
89+
}
90+
91+
if (['getcollection', 'listattributes', 'listdocuments', 'getdocument', 'listindexes'].includes(action)) {
92+
path += `/collection-${ids[1]}`;
93+
}
94+
95+
if (action === 'listattributes') {
96+
path += '/attributes';
97+
}
98+
if (action === 'listindexes') {
99+
path += '/indexes';
100+
}
101+
if (action === 'getdocument') {
102+
path += `/document-${ids[2]}`;
103+
}
104+
105+
106+
return path;
107+
}
108+
109+
function getFunctionsPath(action, ids) {
110+
let path = '/functions';
111+
112+
if (action !== 'list') {
113+
path += `/function-${ids[0]}`;
114+
}
115+
116+
if (action === 'getdeployment') {
117+
path += `/deployment-${ids[1]}`
118+
}
119+
120+
if (action === 'getexecution' || action === 'listexecution') {
121+
path += `/executions`
122+
}
123+
if (action === 'getfunctionusage') {
124+
path += `/usage`
125+
}
126+
127+
return path;
128+
}
129+
130+
function getMessagingPath(action, ids) {
131+
let path = '/messaging';
132+
133+
if (['getmessage', 'listmessagelogs'].includes(action)) {
134+
path += `/message-${ids[0]}`;
135+
}
136+
137+
if (['listproviders', 'getprovider'].includes(action)) {
138+
path += `/providers`;
139+
}
140+
141+
if (action === 'getprovider') {
142+
path += `/provider-${ids[0]}`;
143+
}
144+
145+
if (['listtopics', 'gettopic'].includes(action)) {
146+
path += `/topics`;
147+
}
148+
149+
if (action === 'gettopic') {
150+
path += `/topic-${ids[0]}`;
151+
}
152+
153+
return path;
154+
}
155+
156+
function getProjectsPath(action, ids) {
157+
let path = '';
158+
159+
if (action !== 'list') {
160+
path += `/project-${ids[0]}`;
161+
}
162+
163+
if (['listkeys', 'getkey'].includes(action)) {
164+
path += '/overview/keys'
165+
}
166+
167+
if (['listplatforms', 'getplatform'].includes(action)) {
168+
path += '/overview/platforms'
169+
}
170+
171+
if (['listwebhooks', 'getwebhook'].includes(action)) {
172+
path += '/settings/webhooks'
173+
}
174+
175+
if (['getplatform', 'getkey', 'getwebhook'].includes(action)) {
176+
path += ids[1];
177+
}
178+
179+
return path;
180+
}
181+
182+
function getBucketsPath(action, ids) {
183+
let path = '/storage';
184+
185+
if (action !== 'listbuckets') {
186+
path += `/bucket-${ids[0]}`;
187+
}
188+
189+
if (action === 'getbucketusage') {
190+
path += `/usage`
191+
}
192+
193+
if (action === 'getfile') {
194+
path += `/file-${ids[1]}`
195+
}
196+
197+
return path;
198+
}
199+
200+
function getTeamsPath(action, ids) {
201+
let path = '/auth/teams';
202+
203+
if (action !== 'list') {
204+
path += `/team-${ids[0]}`;
205+
}
206+
207+
return path;
208+
}
209+
210+
function getUsersPath(action, ids) {
211+
let path = '/auth';
212+
213+
if (action !== 'list') {
214+
path += `/user-${ids[0]}`;
215+
}
216+
217+
if (action === 'listsessions') {
218+
path += 'sessions';
219+
}
220+
221+
return path;
222+
}
223+
70224
module.exports = {
71225
getAllFiles,
72226
showConsoleLink

0 commit comments

Comments
 (0)