@@ -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
502515export 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+
712725function 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 ++ ) {
0 commit comments