forked from SalesforceCommerceCloud/sfcc-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·2592 lines (2480 loc) · 130 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var colors = require('colors');
var program = require('commander');
var { prompt } = require('inquirer');
var log = require('./lib/log');
program
.version(require('./package.json').version, '-V, --version')
.option('-D, --debug', 'enable verbose output', function() {
process.env.DEBUG = true;
process.env.NODE_DEBUG = 'request';
})
.option('--selfsigned', 'allow connection to hosts using self-signed certificates', function() {
process.env.SFCC_ALLOW_SELF_SIGNED = true;
})
.option('-I, --ignorewarnings', 'ignore any warnings logged to the console', function() {
process.env.SFCC_IGNORE_WARNINGS = true;
});
program
.command('auth:login [client] [secret]')
.option('-a, --authserver [authserver]','The authorization server used to authenticate')
.description('Authenticate a present user for interactive use')
.action(function(client, secret, options) {
require('./lib/auth').login(client, secret, options.authserver);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Authenticate a user (resource owner) for interactive use. The user must be present and must');
console.log(' provide his login credentials as part of the authentication flow. The authentication requires');
console.log(' an API key (client).');
console.log();
console.log(' If not passed the API key is being looked up in a dw.json file in the current working');
console.log(' You may use environment variable SFCC_OAUTH_CLIENT_ID to pass the API key alternatively.');
if ( require('./lib/auth').OAUTH_AUTHORIZATION_CODE_GRANT_ALLOWED ) {
console.log();
console.log(' The client [secret] is optional. If the secret is not provided, the authentication is done');
console.log(' using the Oauth2 authorization code grant. If the secret is not provided, the ');
console.log(' authentication is done using the Oauth2 implicit grant.');
}
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci auth:login app-client-id');
if ( require('./lib/auth').OAUTH_AUTHORIZATION_CODE_GRANT_ALLOWED ) {
console.log(' $ sfcc-ci auth:login app-client-id app-client-secret');
}
console.log(' $ sfcc-ci auth:login app-client-id -a account.demandware.com');
console.log(' $ sfcc-ci auth:login');
console.log();
});
program
.command('auth:logout')
.description('End the current sessions and clears the authentication')
.action(function() {
require('./lib/auth').cli.logout();
}).on('--help', function() {
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci auth:logout');
console.log();
});
program
.command('client:auth [client] [secret] [user] [user_password]')
.option('-a, --authserver [authserver]','The authorization server used to authenticate')
.option('-r, --renew','Controls whether the authentication should be automatically renewed, ' +
'once the token expires.')
.description('Authenticate an API client with an optional user for automation use')
.action(function(client, secret, user, user_password, options) {
var renew = ( options.renew ? options.renew : false );
require('./lib/auth').auth(client, secret, user, user_password, renew, options.authserver);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Authenticate an API client for automation use, where presence of the resource owner is not');
console.log(' required. Optionally, user (resource owner) credentials can be provided to grant access to');
console.log(' user specific resources.');
console.log();
console.log(' The client and the client secret are optional. If not provided, client and secret are read');
console.log(' from a dw.json file located in the current working directory. You can make use of environment');
console.log(' variables SFCC_OAUTH_CLIENT_ID and SFCC_OAUTH_CLIENT_SECRET to pass the API key and secret.');
console.log();
console.log(' If user credentials are not provided, they are read from a dw.json file located in the')
console.log(' current working directory. You may use environment variables SFCC_OAUTH_USER_NAME and');
console.log(' SFCC_OAUTH_USER_PASSWORD to pass the user credentails alternatively.');
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:auth my_client_id my_client_secret user_name user_password');
console.log(' $ sfcc-ci client:auth my_client_id my_client_secret');
console.log(' $ sfcc-ci client:auth my_client_id my_client_secret -r');
console.log(' $ sfcc-ci client:auth my_client_id my_client_secret -a account.demandware.com');
console.log(' $ sfcc-ci client:auth');
console.log();
});
program
.command('client:auth:renew')
.description('Renews the client authentication. Requires the initial client authentication to ' +
'be run with the --renew option.')
.action(function() {
require('./lib/auth').renew();
}).on('--help', function() {
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:auth:renew');
console.log();
});
program
.command('client:auth:token')
.description('Return the current authentication token')
.action(function() {
console.log(require('./lib/auth').getToken());
}).on('--help', function() {
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:auth:token');
console.log();
});
program
.command('client:list')
.description('Lists a Oauth clients you have access to')
.option('-c, --count <count>','Max count of list items (default is 25)')
.option('--start <start>','Zero-based index of first item to return (default is 0)')
.option('-a, --clientid <clientid>','id of the Oauth client to get details for')
.option('--auditlogs', 'Returns audit logs for changes made to an Oauth client')
.option('-j, --json', 'Formats the output in json')
.option('-v, --verbose', 'Outputs additional details')
.action(function(options) {
var asJson = ( options.json ? options.json : false );
var verbose = ( options.verbose ? options.verbose : false );
var start = ( options.start ? options.start : 0 );
var count = ( options.count ? options.count : 25 );
var auditlogs = ( options.auditlogs ? options.auditlogs : null );
var id = options.clientid;
if (id) {
require('./lib/client').cli.info(id, auditlogs, asJson);
} else {
require('./lib/client').cli.list(start, count, asJson, verbose);
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Lists Oauth clients you have access to');
console.log('');
console.log(' This requires permissions in Account Manager to manage API clients of the org,');
console.log(' the client belongs to. By default client ids are displayed as a short version with');
console.log(' the first 7 characters only. Use --verbose to display the full details.')
console.log('');
console.log(' Use --clientid to get details of a single Oauth client.');
console.log('');
console.log(' Use option --auditlogs to return audit logs for changes made to a single org.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:list');
console.log(' $ sfcc-ci client:list --verbose');
console.log(' $ sfcc-ci client:list --count 50');
console.log(' $ sfcc-ci client:list --count 50 --start 1');
console.log(' $ sfcc-ci client:list --clientid xxxx-yyyy-zzzz');
console.log(' $ sfcc-ci client:list --clientid xxxx-yyyy-zzzz --json');
console.log(' $ sfcc-ci client:list --clientid xxxx-yyyy-zzzz --auditlogs');
console.log();
});
program
.command('client:create')
.description('Creates a new Oauth client')
.option('-c, --configuration <configuration>', 'Configuration of for Oauth client as json')
.option('-f, --file <file>', 'Configuration of the Oauth client as utf-8 encoded text file containing json')
.option('-j, --json', 'Formats the output in json')
.option('-N, --noprompt','No prompt to confirm creation')
.action(async function(options) {
var configuration = ( options.configuration ? JSON.parse(options.configuration) : null );
var asJson = ( options.json ? options.json : false );
var file = ( options.file ? options.file : null );
var noPrompt = ( options.noprompt ? options.noprompt : false );
// determine fallback organization from authenticated subject
var fallbackOrgId;
try {
fallbackOrgId = await require('./lib/auth').getOrgOfAuthenticatedSubject();
} catch (e) {
console.error(e.message);
}
if ( !configuration && !file) {
log.error('Configuration missing. Please specify configuration using -c,' +
'--configuration or -f, --file.');
} else if ( noPrompt ) {
require('./lib/client').cli.create(configuration, file, fallbackOrgId, asJson);
} else {
if (fallbackOrgId) {
console.info('The Oauth client will be created for org (id ' + fallbackOrgId.slice(0,7) +
'), if no other org was specified.');
}
return prompt({
type : 'confirm',
name : 'ok',
default : false,
message : `Create new Oauth client. Are you sure?`
}).then((answers) => {
if (answers['ok']) {
require('./lib/client').cli.create(configuration, file, fallbackOrgId, asJson);
} else {
console.info('Operation aborted');
}
});
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Creates a new Oauth client');
console.log('');
console.log(' This requires permissions in Account Manager to manage API clients. You can pass');
console.log(' the client confguration as JSON string through option -c,--configuration or as a');
console.log(' utf-8 encoded text file containing JSON through option -f,--file.');
console.log('');
console.log(' If not specified, the new Oauth client will be created in the primary organization');
console.log(' of the authenticated user. If no user is authenticated, the new Oauth client will');
console.log(' be created in the same org as the authenticated API client, unless the authenticated');
console.log(' client belongs to multiple organizations.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:create --configuration \'{"name": "my new client","active": true}\'');
console.log(' $ sfcc-ci client:create --file \'path/to/file.json\'');
console.log(' $ sfcc-ci client:create --file \'path/to/file.json\' --noprompt');
console.log();
});
program
.command('client:update')
.description('Update an Oauth client')
.option('-a, --clientid <clientid>','id of the Oauth client to update')
.option('-c, --changes <changes>', 'Changes to Oauth client details as json')
.option('-f, --file <file>', 'Changes to Oauth client details as utf encoded text file containing json')
.option('-j, --json', 'Formats the output in json')
.option('-N, --noprompt','No prompt to confirm update')
.action(function(options) {
var id = options.clientid;
var changes = ( options.changes ? JSON.parse(options.changes) : null );
var asJson = ( options.json ? options.json : false );
var noPrompt = ( options.noprompt ? options.noprompt : false );
var file = ( options.file ? options.file : null );
if ( !id ) {
log.error('Missing required --clientid. Use -h,--help for help.');
} else if ( !changes && !file ) {
log.error('Changes missing. Please specify changes using -c,--change or -f, --file.');
} else if ( noPrompt ) {
require('./lib/client').cli.update(id, changes, file, asJson);
} else {
prompt({
type : 'confirm',
name : 'ok',
default : false,
message : `Update Oauth client ${require('./lib/client').trimClientID(id)}. Are you sure?`
}).then((answers) => {
if (answers['ok']) {
require('./lib/client').cli.update(id, changes, file, asJson);
} else {
console.info('Operation aborted');
}
});
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Updates an existing Oauth client');
console.log('');
console.log(' This requires permissions in Account Manager to manage API clients of the org,');
console.log(' the client belongs to. You should pass changes to the user details in json')
console.log(' (option -c,--changes).');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:update --clientid xxxx-yyyy-zzzz --changes \'{"active": true}\'');
console.log();
});
program
.command('client:rotate')
.description('Rotate credentials of an Oauth client')
.option('-a, --clientid <clientid>','id of the Oauth client to rotate')
.option('-j, --json', 'Formats the output in json')
.option('-N, --noprompt','No prompt to confirm rotation')
.action(function(options) {
var id = options.clientid;
var asJson = ( options.json ? options.json : false );
var noPrompt = ( options.noprompt ? options.noprompt : false );
if ( !id ) {
log.error('Missing required --clientid. Use -h,--help for help.');
} else if ( noPrompt ) {
require('./lib/client').cli.rotate(id, asJson);
} else {
prompt({
type : 'confirm',
name : 'ok',
default : false,
message : `Rotate credentials for Oauth client ${require('./lib/client').trimClientID(id)}. ` +
`Are you sure?`
}).then((answers) => {
if (answers['ok']) {
require('./lib/client').cli.rotate(id, asJson);
}
});
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Rotates credentials of an Oauth client');
console.log('');
console.log(' This generates a new Oauth client with a new client id and client secret. It uses the client');
console.log(' referenced through --clientid and replicates the same client configuration into the new Oauth');
console.log(' client. The operation requires permissions in Account Manager to manage API clients of the');
console.log(' org the client belongs to. The client referenced by --clientid will not be changed as part of');
console.log(' this operation. Deactivation and deletion of the reference client is not done.');
console.log('');
console.log(' Configuration and credentials of the new Oauth client will be returned through stdout.');
console.log('');
console.log(' The operation does not check if the reference Oauth client was already rotated before. To');
console.log(' avoid unnecessary creation of Oauth clients please ensure you have not rotated the same');
console.log(' client before.')
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:rotate --clientid xxxx-yyyy-zzzz');
console.log();
});
program
.command('client:delete')
.description('Delete an Oauth client')
.option('-a, --clientid <clientid>','id of the Oauth client to delete')
.option('-j, --json', 'Formats the output in json')
.option('-N, --noprompt','No prompt to confirm deletion')
.action(function(options) {
var id = options.clientid;
var asJson = ( options.json ? options.json : false );
var noPrompt = ( options.noprompt ? options.noprompt : false );
if ( !id ) {
log.error('Missing required --clientid. Use -h,--help for help.');
} else if ( noPrompt ) {
require('./lib/client').cli.delete(id, asJson);
} else {
prompt({
type : 'confirm',
name : 'ok',
default : false,
message : `Delete Oauth client ${require('./lib/client').trimClientID(id)}. Are you sure?`
}).then((answers) => {
if (answers['ok']) {
require('./lib/client').cli.delete(id, asJson);
}
});
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Delete an Oauth client.');
console.log('');
console.log(' This requires permissions in Account Manager to manage API clients of the org,');
console.log(' the client belongs to.');
console.log('');
console.log(' NOTE: The Oauth client is not deactivated, but completely deleted by this operation.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci client:delete --clientid xxxx-yyyy-zzzz');
console.log();
});
program
.command('data:upload')
.option('-i, --instance <instance>','Instance to upload the file to. Can be an ' +
'instance alias. If not specified the currently configured instance will be used.')
.option('-t, --target <target>', 'Target (WebDAV) location to upload to')
.option('-f, --file <file>', 'File to upload')
.option('-c, --certificate <certificate>','Path to the certificate to use for two factor authentication.')
.option('-p, --passphrase <passphrase>','Passphrase to be used to read the given certificate.')
.description('Uploads a file onto a Commerce Cloud instance')
.action(function(options) {
var instance = require('./lib/instance').getInstance(options.instance);
var target = ( options.target ? options.target : null );
if (!target) {
this.missingArgument('target');
return;
}
var file = ( options.file ? options.file : null );
if (!file) {
this.missingArgument('file');
return;
}
require('./lib/webdav').cli.upload(instance, '/' + target, file, true, {
pfx: options.certificate,
passphrase: options.passphrase
});
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Uploads the file onto an instance into the target WebDAV folder.');
console.log(' Note, that there is a max file size of 100 MB for uploading files. You may');
console.log(' want to zip or gzip the file to upload.');
console.log();
console.log(' The file may include a path to the actual location where the file resides locally.');
console.log(' The provided --target <target> is relative to /webdav/Sites/, e.g. "impex/src/upload".');
console.log();
console.log(' Supported top level --target are "impex", "static", "catalogs", "libraries" and "dynamic".');
console.log(' In order to use "catalogs", "libraries" and "dynamic" you have to set API permissions for');
console.log(' a specific catalog, library or dynamic folder in WebDAV Client Permissions.');
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci data:upload --instance my-instance.demandware.net --target impex/src/upload ' +
'--file data.xml');
console.log(' $ sfcc-ci data:upload --instance my-instance.demandware.net --target impex/src/instance ' +
'--file site-import.zip');
console.log();
});
program
.command('sandbox:realm:list')
.description('List realms eligible to manage sandboxes for')
.option('-r, --realm <realm>','Realm to get details for')
.option('--show-usage', 'Whether to return detailed usage data')
.option('-j, --json','Formats the output in json')
.action(function(options) {
var realm = ( options.realm ? options.realm : null );
var asJson = ( options.json ? options.json : false );
var topic = ( options.showUsage ? 'usage' : null );
require('./lib/sandbox').cli.realm.list(realm, topic, asJson);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Use --realm <realm> to get details of a single realm such as configuration and usage');
console.log(' information about sandboxes. Use --usage to retrieve detailed usage information of');
console.log(' sandboxes in that realm.');
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:realm:list');
console.log(' $ sfcc-ci sandbox:realm:list --json');
console.log(' $ sfcc-ci sandbox:realm:list --realm zzzz');
console.log(' $ sfcc-ci sandbox:realm:list --realm zzzz --json');
console.log(' $ sfcc-ci sandbox:realm:list --realm zzzz --show-usage');
console.log();
});
program
.command('sandbox:realm:update')
.description('Update realm settings')
.option('-r, --realm <realm>', 'Realm to update')
.option('-m, --max-sandbox-ttl <maxSandboxTTL>', 'Maximum number of hours a sandbox can live in the realm')
.option('-d, --default-sandbox-ttl <defaultSandboxTTL>', 'Number of hours a sandbox lives in the realm by default')
.option('--start-scheduler <startScheduler>', 'Start schedule for all sandboxes under this realm')
.option('--stop-scheduler <stopScheduler>', 'Stop schedule for all sandboxes under this realm')
.option('-j, --json', 'Formats the output in json')
.action(function (options) {
var realm = (options.realm ? options.realm : null);
if (!realm) {
this.missingArgument('realm');
return;
}
var maxSandboxTTL = (options.maxSandboxTtl ? parseInt(options.maxSandboxTtl) : false);
var defaultSandboxTTL = (options.defaultSandboxTtl ? parseInt(options.defaultSandboxTtl) : false);
var scheduleOk = true;
try {
var startScheduler = options.startScheduler ?
(options.startScheduler === "null" ? "null" : JSON.parse(options.startScheduler)) : null;
var stopScheduler = options.stopScheduler ?
(options.stopScheduler === "null" ? "null" : JSON.parse(options.stopScheduler)) : null;
} catch (objError) {
scheduleOk = false;
if (objError instanceof SyntaxError) {
log.error(objError.name + ' : Invalid JSON format for scheduler. Use -h,--help for help.');
} else {
log.error(objError.message);
}
}
var asJson = (options.json ? options.json : false);
if (scheduleOk) {
require('./lib/sandbox').cli.realm.update(realm, maxSandboxTTL, defaultSandboxTTL, startScheduler,
stopScheduler, asJson);
}
}).on('--help', function () {
console.log('');
console.log(' Details:');
console.log();
console.log(' Update details of a realm.');
console.log();
console.log(' Use --max-sandbox-ttl to update the maximum number of hours a sandbox can live');
console.log(' in the realm (must adhere to the maximum TTL quota). Use --default-sandbox-ttl to');
console.log(' update the number of hours a sandbox lives in the realm when no TTL was given upon');
console.log(' provisioning (must adhere to the maximum TTL quota).');
console.log();
console.log(' Use --start-scheduler and --stop-scheduler to update the start and stop schedule for the ');
console.log(' realm. You can use the value \'null\' to remove the existing schedule from the sandbox');
console.log();
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:realm:update --realm zzzz --max-sandbox-ttl 72');
console.log(' $ sfcc-ci sandbox:realm:update --realm zzzz --default-sandbox-ttl 24');
console.log(' $ sfcc-ci sandbox:realm:update --realm zzzz --default-sandbox-ttl 24');
console.log(' $ sfcc-ci sandbox:realm:update --realm zzzz --start-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"08:00:00+03:00"}\'');
console.log(' $ sfcc-ci sandbox:realm:update --realm zzzz --stop-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"19:00:00Z"}\'');
console.log();
});
program
.command('sandbox:list')
.description('List all available sandboxes')
.option('--show-deleted', 'Whether to include deleted sandboxes')
.option('-j, --json','Formats the output in json')
.option('-S, --sortby <sortby>', 'Sort by specifying any field')
.action(function(options) {
var showDeleted = ( options.showDeleted ? true : false );
var asJson = ( options.json ? options.json : false );
var sortby = ( options.sortby ? options.sortby : null );
require('./lib/sandbox').cli.list(showDeleted, asJson, sortby);
}).on('--help', function() {
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:list');
console.log(' $ sfcc-ci sandbox:list --show-deleted');
console.log(' $ sfcc-ci sandbox:list --json');
console.log();
});
program
.command('sandbox:ips')
.description('List inbound and outbound IP addresses for sandboxes')
.option('-r, --realm <realm>','Realm to get IP details for')
.option('-j, --json','Formats the output in json')
.action(function(options) {
var realm = ( options.realm ? options.realm : null );
var asJson = ( options.json ? options.json : false );
require('./lib/sandbox').cli.ips(realm, asJson);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' Use the optional --realm parameter to only retrieve IP addresses relevant for a particular');
console.log(' realm.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:ips');
console.log(' $ sfcc-ci sandbox:ips --realm zzzz');
console.log(' $ sfcc-ci sandbox:ips --json');
console.log();
});
program
.command('sandbox:create')
.option('-r, --realm <realm>','Realm to create the sandbox for')
.option('-t, --ttl <hours>','Number of hours the sandbox will live')
.option('--auto-scheduled', 'Sets the sandbox as being auto scheduled')
.option('--start-scheduler <startScheduler>', 'Start schedule for the sandbox')
.option('--stop-scheduler <stopScheduler>', 'Stop schedule for the sandbox')
.option('-p, --profile <profile>','Resource profile used for the sandbox, "medium" is the default')
.option('--ocapi-settings <json>','Additional OCAPI settings applied to the sandbox')
.option('--webdav-settings <json>','Additional WebDAV permissions applied to the sandbox')
.option('-j, --json','Formats the output in json')
.option('-s, --sync', 'Operates in synchronous mode and waits until the operation has been finished.')
.option('-d, --default', 'Sets the created sandbox as default instance.')
.option('-a, --set-alias <alias>','Instance alias to create for the sandbox')
.description('Create a new sandbox')
.action(function(options) {
var realm = ( options.realm ? options.realm : null );
var ttl = ( options.ttl ? parseInt(options.ttl) : null );
var autoScheduled = ( options.autoScheduled ? options.autoScheduled : false );
var profile = ( options.profile ? options.profile : null );
var asJson = ( options.json ? options.json : false );
var sync = ( options.sync ? options.sync : false );
var setAsDefault = ( options.default ? options.default : false );
var alias = ( options.setAlias ? options.setAlias : null );
var ocapiSettings = ( options.ocapiSettings ? options.ocapiSettings : null );
var webdavSettings = ( options.webdavSettings ? options.webdavSettings : null );
var scheduleOk = true;
try {
var startScheduler = options.startScheduler ? JSON.parse(options.startScheduler) : null;
var stopScheduler = options.stopScheduler ? JSON.parse(options.stopScheduler) : null;
} catch (objError) {
scheduleOk = false;
if (objError instanceof SyntaxError) {
log.error(objError.name + ' : Invalid JSON format for scheduler. Use -h,--help for help.');
} else {
log.error(objError.message);
}
}
if (scheduleOk) {
require('./lib/sandbox').cli.create(realm, alias, ttl, profile, autoScheduled, startScheduler,
stopScheduler, ocapiSettings, webdavSettings, asJson, sync, setAsDefault);
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The sandbox will be created for the realm using the <realm> argument or stored in dw.json');
console.log(' config file. You must have permission to create a new sandbox for the realm. The number of');
console.log(' sandboxes allowed to create is limited. The command only trigger the creation and does not');
console.log(' wait until the sandbox is fully up and running. Use may use `sfcc-ci sandbox:list` to check');
console.log(' the status of the sandbox.');
console.log();
console.log(' The --auto-scheduled flag controls if the sandbox is being auto scheduled according to the');
console.log(' schedule configured at sandbox realm level. By default or if omitted the sandbox is not auto');
console.log(' scheduled.');
console.log();
console.log(' Use --start-scheduler and --stop-scheduler to add the start and stop schedule for the ');
console.log(' sandbox.');
console.log();
console.log(' Use the optional --profile <profile> to set the resource allocation for the sandbox, "medium"');
console.log(' is the default. Be careful, more powerful profiles consume more credits. Supported values');
console.log(' are: medium, large, xlarge.');
console.log();
console.log(' You can force the command to wait until the creation of the sandbox has been finished and the');
console.log(' sandbox is available to use (in "started" status) by using the --sync flag. By default the');
console.log(' command will poll the status for 10 minutes. You can overwrite this by using the environment');
console.log(' variable SFCC_SANDBOX_API_POLLING_TIMEOUT to set another timeout in minutes.')
console.log();
console.log(' The created sandbox is being added to the list of instances with its host name. The optional');
console.log(' --set-alias <alias> is used as alias for the new instance. If it is omitted, the host is used');
console.log(' as alias.');
console.log();
console.log(' If executed with --default flag, the created sandbox will be set as new default instance.');
console.log();
console.log(' The TTL (time to live) in hours of the sandbox can be modified via the --ttl flag. The value');
console.log(' must adhere to the maximum TTL quotas) If absent the realms default sandbox TTL is used.');
console.log(' If the sandbox age reaches its TTL, it will be deleted automatically.');
console.log();
console.log(' Use --ocapi-settings and --webdav-settings to pass additional OCAPI and/or WebDAV settings to');
console.log(' the created sandbox as JSON. You may not overwrite the permissions for the CLI client but');
console.log(' amend its permissions or add permissions for other clients. The passed JSON must be valid.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:create');
console.log(' $ sfcc-ci sandbox:create --realm my-realm');
console.log(' $ sfcc-ci sandbox:create -r my-realm --set-alias an-alias');
console.log(' $ sfcc-ci sandbox:create -r my-realm -a an-alias -d');
console.log(' $ sfcc-ci sandbox:create -r my-realm -s');
console.log(' $ sfcc-ci sandbox:create -r my-realm -a an-alias -s');
console.log(' $ sfcc-ci sandbox:create -r my-realm -a an-alias -s -d');
console.log(' $ sfcc-ci sandbox:create -r my-realm -s -j');
console.log(' $ sfcc-ci sandbox:create -r my-realm --ttl 6');
console.log(' $ sfcc-ci sandbox:create -r my-realm --auto-scheduled');
console.log(' $ sfcc-ci sandbox:create -r my-realm -p large');
console.log(' $ sfcc-ci sandbox:create -r my-realm --start-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"08:00:00+03:00"}\'');
console.log(' $ sfcc-ci sandbox:create -r my-realm --stop-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"19:00:00Z"}\'');
console.log();
console.log();
});
program
.command('sandbox:get')
.description('Get detailed information about a sandbox')
.option('-s, --sandbox <id>','sandbox to get details for')
.option('-j, --json','Formats the output in json')
.option('-h, --host','Return the host name of the sandbox')
.option('-O, --open','Opens a browser with the Business Manager on the sandbox')
.option('--show-operations','Display operations performed')
.option('--show-usage','Display detailed usage information')
.option('--show-settings','Display settings applied')
.option('--show-storage','Display detailed storage information')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
var asJson = ( options.json ? options.json : false );
var hostOnly = ( options.host ? options.host : false );
var openBrowser = ( options.open ? options.open : false );
var topic = null;
if ( options.showOperations ) {
topic = 'operations';
} else if ( options.showUsage ) {
topic = 'usage';
} else if ( options.showSettings ) {
topic = 'settings';
} else if ( options.showStorage ) {
topic = 'storage';
}
require('./lib/sandbox').cli.get(spec, asJson, hostOnly, openBrowser, topic);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The sandbox to lookup must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log();
console.log(' Use --show-usage to display detailed usage information, --show-operations to get a list of');
console.log(' previous operations executed on the sandbox, --show-settings to return the settings initially');
console.log(' applied to the sandbox during creation. Use --show-storage to retrieve detailed storage');
console.log(' capacity.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:get --sandbox my-sandbox-id');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id -j');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id -h');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id -O');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id --show-usage');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id --show-operations');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id --show-settings');
console.log(' $ sfcc-ci sandbox:get -s my-sandbox-id --show-storage');
console.log();
});
program
.command('sandbox:update')
.option('-s, --sandbox <id>','sandbox to update')
.option('-t, --ttl <hours>','number of hours to add to the sandbox lifetime')
.option('--auto-scheduled <flag>','Sets the sandbox as being auto scheduled')
.option('--start-scheduler <startScheduler>', 'Start schedule for the sandbox')
.option('--stop-scheduler <stopScheduler>', 'Stop schedule for the sandbox')
.description('Update a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
var ttl = ( options.ttl ? parseInt(options.ttl) : null );
var autoScheduled = ( options.autoScheduled !== null ?
( options.autoScheduled === 'true' ? true : false ) : null );
var scheduleOk = true;
try {
var startScheduler = options.startScheduler ?
(options.startScheduler === "null" ? "null" : JSON.parse(options.startScheduler)) : null;
var stopScheduler = options.stopScheduler ?
(options.stopScheduler === "null" ? "null" : JSON.parse(options.stopScheduler)) : null;
} catch (objError) {
scheduleOk = false;
if (objError instanceof SyntaxError) {
log.error(objError.name + ' : Invalid JSON format for scheduler. Use -h,--help for help.');
} else {
log.error(objError.message);
}
}
if (scheduleOk) {
require('./lib/sandbox').cli.update(spec, ttl, autoScheduled, false, startScheduler, stopScheduler);
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The TTL (time to live) in hours of the sandbox can be prolonged via the --ttl flag. The value');
console.log(' must, together with previous prolongiations, adhere to the maximum TTL quotas). If set to 0 or');
console.log(' less the sandbox will have an infinite lifetime.');
console.log();
console.log(' The --auto-scheduled flag controls if the sandbox is being autoscheduled according to the');
console.log(' schedule configured at sandbox realm level.');
console.log();
console.log(' Use --start-scheduler and --stop-scheduler to update the start and stop schedule for the ');
console.log(' sandbox. You can use the value \'null\' to remove the existing schedule from the sandbox');
console.log();
console.log(' The sandbox to update must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:update --sandbox my-sandbox-id --ttl 8');
console.log(' $ sfcc-ci sandbox:update --sandbox my-sandbox-id --auto-scheduled true');
console.log(' $ sfcc-ci sandbox:update --sandbox my-sandbox-id --auto-scheduled false');
console.log(' $ sfcc-ci sandbox:update --sandbox my-sandbox-id --start-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"08:00:00+03:00"}\'');
console.log(' $ sfcc-ci sandbox:update --sandbox my-sandbox-id --stop-scheduler ' +
'\'{"weekdays":["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY"],"time":"19:00:00Z"}\'');
console.log();
});
program
.command('sandbox:start')
.option('-s, --sandbox <id>','sandbox to start')
.option('--sync','Operates in synchronous mode and waits until the operation has finished.')
.description('Start a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
var sync = ( options.sync ? options.sync : false );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
require('./lib/sandbox').cli.start(spec, false, sync);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The sandbox to start must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log();
console.log(' Use the --sync flag to wait for the sandbox to have `started` status.');
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:start --sandbox my-sandbox-id');
console.log(' $ sfcc-ci sandbox:start --sandbox my-sandbox-id --sync');
console.log();
});
program
.command('sandbox:stop')
.option('-s, --sandbox <id>','sandbox to stop')
.option('--sync','Operates in synchronous mode and waits until the operation has finished.')
.description('Stop a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
var sync = ( options.sync ? options.sync : false );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
require('./lib/sandbox').cli.stop(spec, false, sync);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The sandbox to stop must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log();
console.log(' Use the --sync flag to wait for the sandbox to have `stopped` status.');
console.log();
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:stop --sandbox my-sandbox-id');
console.log(' $ sfcc-ci sandbox:stop --sandbox my-sandbox-id --sync');
console.log();
});
program
.command('sandbox:restart')
.option('-s, --sandbox <id>','sandbox to restart')
.description('Restart a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
require('./lib/sandbox').cli.restart(spec, false);
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' The sandbox to restart must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:restart --sandbox my-sandbox-id');
console.log();
});
program
.command('sandbox:reset')
.option('-s, --sandbox <id>','sandbox to reset')
.option('-N, --noprompt','No prompt to confirm reset')
.description('Reset a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}
// always assume it is a sandbox id
var spec = { id : sandbox_id };
// check if we have to lookup the sandbox by realm and instance
var split = sandbox_id.split(/[-_]/);
if (split.length === 2) {
spec['realm'] = split[0];
spec['instance'] = split[1];
}
var noPrompt = ( options.noprompt ? options.noprompt : false );
if ( noPrompt ) {
require('./lib/sandbox').cli.reset(spec, false);
} else {
prompt({
type : 'confirm',
name : 'ok',
default : false,
message : 'Reset sandbox ' + sandbox_id + '. Are you sure?'
}).then((answers) => {
if (answers['ok']) {
require('./lib/sandbox').cli.reset(spec, false);
}
});
}
}).on('--help', function() {
console.log('');
console.log(' Details:');
console.log();
console.log(' WARNING: This is a destructive operation and you will loose any data stored on the sandbox.');
console.log();
console.log(' The sandbox to reset must be identified by its id. Use may use `sfcc-ci sandbox:list` to');
console.log(' identify the id of your sandboxes.');
console.log();
console.log(' You can also pass the realm and the instance (e.g. zzzz-s01) as <id>.');
console.log('');
console.log(' Examples:');
console.log();
console.log(' $ sfcc-ci sandbox:reset --sandbox my-sandbox-id');
console.log(' $ sfcc-ci sandbox:reset --sandbox my-sandbox-id --noprompt');
console.log();
});
program
.command('sandbox:delete')
.option('-s, --sandbox <id>','sandbox to delete')
.option('-N, --noprompt','No prompt to confirm delete')
.description('Delete a sandbox')
.action(function(options) {
var sandbox_id = ( options.sandbox ? options.sandbox : null );
if (!sandbox_id) {
this.missingArgument('sandbox');
return;
}