Skip to content

Commit ab46b1d

Browse files
committed
fixed issues with ParaClient instance not using the selected endpoint properly
1 parent b4cc1bb commit ab46b1d

2 files changed

Lines changed: 58 additions & 48 deletions

File tree

index.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ export async function setup(config) {
7171
var endpointValue = (endpoint || defaultConfig.endpoint).trim();
7272

7373
newJWT(access, secret, endpointValue, config);
74-
var pc = new ParaClient(access, secret, parseEndpoint(endpointValue));
75-
ping(pc, config);
74+
ping(config);
7675

7776
if (access === 'app:para') {
7877
listApps(config, {}, access, async function () {
@@ -87,7 +86,7 @@ export async function setup(config) {
8786
const appname = await input({
8887
message: 'App name:'
8988
});
90-
newApp(pc, ['', appname], {});
89+
newApp(['', appname], config, {});
9190
}
9291
});
9392
}
@@ -100,12 +99,13 @@ export async function setup(config) {
10099
}
101100
}
102101

103-
export function createAll(pc, input, flags) {
102+
export function createAll(input, config, flags = {}) {
104103
if (!input[1]) {
105104
fail('No files specified.');
106105
return;
107106
}
108107

108+
const pc = getClient(config, flags);
109109
var files = globbySync(input[1], { realpath: true });
110110
var totalSize = 0;
111111
var totalObjects = 0;
@@ -194,7 +194,8 @@ export function createAll(pc, input, flags) {
194194
console.log(green('✔'), 'Created', totalObjects, 'objects with a total size of', Math.round(totalSize / 1024), 'KB.');
195195
}
196196

197-
export function readAll(pc, flags) {
197+
export function readAll(config, flags = {}) {
198+
const pc = getClient(config, flags);
198199
if (flags.id) {
199200
var readIds = flags.id;
200201
if (!(readIds instanceof Array)) {
@@ -211,12 +212,13 @@ export function readAll(pc, flags) {
211212
}
212213
}
213214

214-
export function updateAll(pc, input, flags) {
215+
export function updateAll(input, config, flags = {}) {
215216
if (!input[1]) {
216217
fail('No files specified.');
217218
return;
218219
}
219220

221+
const pc = getClient(config, flags);
220222
var files = globbySync(input[1], { realpath: true });
221223
var updateList = [];
222224

@@ -249,7 +251,8 @@ export function updateAll(pc, input, flags) {
249251
});
250252
}
251253

252-
export function deleteAll(pc, input, flags) {
254+
export function deleteAll(input, config, flags = {}) {
255+
const pc = getClient(config, flags);
253256
if (flags.id || input[1]) {
254257
var deleteIds = globbySync(input[1] || ' ', { realpath: true });
255258
if (deleteIds.length === 0) {
@@ -270,7 +273,8 @@ export function deleteAll(pc, input, flags) {
270273
}
271274
}
272275

273-
export function newKeys(pc, config) {
276+
export function newKeys(config, flags = {}) {
277+
const pc = getClient(config, flags);
274278
pc.newKeys().then(function (keys) {
275279
config.set('secretKey', keys.secretKey);
276280
console.log(green('✔'), 'New JWT generated and saved in', yellow(config.path));
@@ -310,12 +314,13 @@ export function newJWT(accessKey, secretKey, endpoint, config, flags) {
310314
}
311315
}
312316

313-
export function newApp(pc, input, flags) {
317+
export function newApp(input, config, flags = {}) {
314318
if (!input[1]) {
315319
fail('App name not specified.');
316320
return;
317321
}
318322

323+
const pc = getClient(config, flags);
319324
var appid = input[1];
320325
var req = pc.invokeGet('_setup/' + appid, { name: (flags.name || appid), shared: (flags.shared || false) });
321326
pc.getEntity(req).then(function (resp) {
@@ -330,12 +335,13 @@ export function newApp(pc, input, flags) {
330335
});
331336
}
332337

333-
export async function deleteApp(pc, input, flags) {
334-
if (!input[1]) {
338+
export async function deleteApp(inputArgs, config, flags = {}) {
339+
if (!inputArgs[1]) {
335340
fail('App id not specified.');
336341
return;
337342
}
338-
var appid = input[1];
343+
const pc = getClient(config, flags);
344+
var appid = inputArgs[1];
339345
if (appid.indexOf('app:') < 0) {
340346
appid = 'app:' + appid;
341347
}
@@ -366,8 +372,8 @@ export async function deleteApp(pc, input, flags) {
366372
}
367373
}
368374

369-
export function ping(pc, config) {
370-
console.log(">>>>>> ", pc.endpoint);
375+
export function ping(config, flags = {}) {
376+
const pc = getClient(config, flags);
371377
pc.me().then(function (mee) {
372378
pc.getServerVersion().then(function (ver) {
373379
console.log(green('✔'), 'Connected to Para server ' + cyan.bold('v' + ver),
@@ -383,23 +389,26 @@ export function ping(pc, config) {
383389
});
384390
}
385391

386-
export function me(pc, config) {
392+
export function me(config, flags = {}) {
393+
const pc = getClient(config, flags);
387394
pc.me().then(function (mee) {
388395
console.log(JSON.stringify(mee, null, 2));
389396
}).catch(function () {
390397
fail('Connection failed. Server might be down. Check the configuration file', yellow(config.path));
391398
});
392399
}
393400

394-
export function types(pc, config) {
401+
export function types(config, flags = {}) {
402+
const pc = getClient(config, flags);
395403
const types = pc.getEntity(pc.invokeGet("_types")).then(function (data) {
396404
console.log(JSON.stringify(data, null, 2));
397405
}).catch(function () {
398406
fail('Connection failed. Server might be down. Check the configuration file', yellow(config.path));
399407
});
400408
}
401409

402-
export function exportData(pc, config) {
410+
export function exportData(config, flags = {}) {
411+
const pc = getClient(config, flags);
403412
pc.invokeGet('/_export').then(function (data) {
404413
try {
405414
var filename = (data.headers['content-disposition'] || 'export.zip');
@@ -415,11 +424,12 @@ export function exportData(pc, config) {
415424
});
416425
}
417426

418-
export function importData(pc, input, config) {
427+
export function importData(input, config, flags = {}) {
419428
if (!input[1]) {
420429
fail('No file to import.');
421430
return;
422431
}
432+
const pc = getClient(config, flags);
423433
if (!config.get('jwt')) {
424434
newJWT(config.get('accessKey'), config.get('secretKey'), config.get('endpoint'), config);
425435
}
@@ -456,7 +466,8 @@ function promiseWhile(results, fn) {
456466
});
457467
}
458468

459-
export function search(pc, input, flags) {
469+
export function search(input, config, flags = {}) {
470+
const pc = getClient(config, flags);
460471
var p = new Pager(flags.page, flags.sort, flags.desc, flags.limit);
461472
if (flags.lastKey) {
462473
p.lastKey = flags.lastKey;
@@ -483,15 +494,17 @@ export function search(pc, input, flags) {
483494
}
484495
}
485496

486-
export function appSettings(pc, config) {
497+
export function appSettings(config, flags = {}) {
498+
const pc = getClient(config, flags);
487499
pc.appSettings().then(function (settings) {
488500
console.log(JSON.stringify(settings, null, 2));
489501
}).catch(function () {
490502
fail('Connection failed. Check the configuration file', yellow(config.path));
491503
});
492504
}
493505

494-
export function rebuildIndex(pc, config, flags) {
506+
export function rebuildIndex(config, flags = {}) {
507+
const pc = getClient(config, flags);
495508
pc.rebuildIndex(flags.destinationIndex).then(function (response) {
496509
console.log(JSON.stringify(response, null, 2));
497510
}).catch(function (err) {
@@ -500,11 +513,8 @@ export function rebuildIndex(pc, config, flags) {
500513
}
501514

502515
export function listApps(config, flags, parentAccessKey, failureCallback) {
516+
var pc = getClient(config, flags);
503517
var selectedEndpoint = getSelectedEndpoint(config, flags);
504-
var accessKey = selectedEndpoint.accessKey;
505-
var secretKey = selectedEndpoint.secretKey;
506-
var endpoint = selectedEndpoint.endpoint;
507-
var pc = new ParaClient(accessKey, secretKey, parseEndpoint(endpoint));
508518
var p = new Pager();
509519
var results = [];
510520
p.sortby = '_docid';
@@ -514,7 +524,7 @@ export function listApps(config, flags, parentAccessKey, failureCallback) {
514524
}).then(function () {
515525
var apps = results.map(function (app) {return app.appIdentifier.trim();});
516526
if (apps.length) {
517-
console.log('Found', p.count, 'apps on ' + cyan(endpoint) + ':\n', yellow('[') + green(apps.join(yellow('] ['))) + yellow(']'));
527+
console.log('Found', p.count, 'apps on ' + cyan(selectedEndpoint.endpoint) + ':\n', yellow('[') + green(apps.join(yellow('] ['))) + yellow(']'));
518528
console.log('\nTyping', cyan('para-cli select'), green(apps[0]), 'will switch to that app. \nCurrent app:',
519529
green(parentAccessKey));
520530
process.exit(0);
@@ -530,7 +540,6 @@ export function selectApp(input, config, flags) {
530540
var selectedEndpoint = getSelectedEndpoint(config, flags);
531541
var accessKey = selectedEndpoint.accessKey;
532542
var secretKey = selectedEndpoint.secretKey;
533-
var endpoint = selectedEndpoint.endpoint;
534543
if (accessKey === 'app:para' && secretKey) {
535544
var selectedApp = 'app:' + (input[1] || 'para').trim();
536545
if (selectedApp === 'app:para') {
@@ -546,7 +555,7 @@ export function selectApp(input, config, flags) {
546555
appid: accessKey,
547556
getCredentials: selectedApp
548557
}), secretKey, { algorithm: 'HS256' });
549-
var paraClient = new ParaClient(accessKey, secretKey, parseEndpoint(endpoint));
558+
var paraClient = getClient(config, flags);
550559
paraClient.setAccessToken(jwt);
551560
paraClient.me(jwt).then(function (data) {
552561
if (data && data.credentials) {
@@ -599,7 +608,6 @@ export async function addEndpoint(config) {
599608
mask: '*'
600609
});
601610

602-
var pc = new ParaClient("app:para", secretKey, parseEndpoint(endpoint));
603611
var endpoints = config.get('endpoints') || [];
604612
var existing = false;
605613
for (var i = 0; i < endpoints.length; i++) {
@@ -613,7 +621,7 @@ export async function addEndpoint(config) {
613621
endpoints.push({accessKey: 'app:para', secretKey: secretKey, endpoint: endpoint});
614622
}
615623
config.set('endpoints', endpoints);
616-
ping(pc, config);
624+
ping(config);
617625
} catch (error) {
618626
if (error.name === 'ExitPromptError') {
619627
console.log('\nAdd endpoint cancelled.');
@@ -709,6 +717,11 @@ function getSelectedEndpoint(config, flags) {
709717
}
710718
}
711719

720+
function getClient(config, flags = {}) {
721+
var selectedEndpoint = getSelectedEndpoint(config, flags);
722+
return new ParaClient(selectedEndpoint.accessKey, selectedEndpoint.secretKey, parseEndpoint(selectedEndpoint.endpoint));
723+
}
724+
712725
function sendFileChunk(chunkId, textEncoded, json, id, flags, start, end, pc, decoder) {
713726
if (start > 0 && textEncoded[start] !== 32) {
714727
for (var i = 0; i < 100 && start - i >= 0; i++) {

para-cli.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
/* eslint object-curly-spacing: ["error", "always"] */
2323

2424
import updateNotifier from 'update-notifier';
25-
import ParaClient from 'para-client-js';
2625
import Conf from 'conf';
2726
import figlet from 'figlet';
2827
import chalk from 'chalk';
@@ -133,8 +132,6 @@ if (!input[0]) {
133132
process.exitCode = 1;
134133
await setup(config);
135134
} else {
136-
var pc = new ParaClient(accessKey, secretKey, parseEndpoint(endpoint));
137-
138135
if (input[0] === 'setup') {
139136
await setup(config);
140137
}
@@ -158,66 +155,66 @@ if (!input[0]) {
158155
}
159156

160157
if (input[0] === 'create') {
161-
createAll(pc, input, flags);
158+
createAll(input, config, flags);
162159
}
163160

164161
if (input[0] === 'read') {
165-
readAll(pc, flags);
162+
readAll(config, flags);
166163
}
167164

168165
if (input[0] === 'update') {
169-
updateAll(pc, input, flags);
166+
updateAll(input, config, flags);
170167
}
171168

172169
if (input[0] === 'delete') {
173-
deleteAll(pc, input, flags);
170+
deleteAll(input, config, flags);
174171
}
175172

176173
if (input[0] === 'search') {
177-
search(pc, input, flags);
174+
search(input, config, flags);
178175
}
179176

180177
if (input[0] === 'new-key') {
181-
newKeys(pc, config);
178+
newKeys(config, flags);
182179
}
183180

184181
if (input[0] === 'new-jwt') {
185182
newJWT(accessKey, secretKey, endpoint, config, flags);
186183
}
187184

188185
if (input[0] === 'new-app') {
189-
newApp(pc, input, flags);
186+
newApp(input, config, flags);
190187
}
191188

192189
if (input[0] === 'delete-app') {
193-
deleteApp(pc, input, flags);
190+
deleteApp(input, config, flags);
194191
}
195192

196193
if (input[0] === 'ping') {
197-
ping(pc, config);
194+
ping(config, flags);
198195
}
199196

200197
if (input[0] === 'me') {
201-
me(pc, config);
198+
me(config, flags);
202199
}
203200

204201
if (input[0] === 'app-settings') {
205-
appSettings(pc, config);
202+
appSettings(config, flags);
206203
}
207204

208205
if (input[0] === 'rebuild-index') {
209-
rebuildIndex(pc, config, flags);
206+
rebuildIndex(config, flags);
210207
}
211208

212209
if (input[0] === 'export') {
213-
exportData(pc, config, flags);
210+
exportData(config, flags);
214211
}
215212

216213
if (input[0] === 'import') {
217-
importData(pc, input, config);
214+
importData(input, config, flags);
218215
}
219216

220217
if (input[0] === 'types') {
221-
types(pc, config);
218+
types(config, flags);
222219
}
223220
}

0 commit comments

Comments
 (0)