Skip to content

Commit 944f79f

Browse files
authored
Merge pull request actions#67 from akamai/feature/WEEPKRK-234
WEEPKRK-234: Add print logs on console argument
2 parents 9aa9012 + 2a76bb1 commit 944f79f

File tree

2 files changed

+101
-72
lines changed

2 files changed

+101
-72
lines changed

src/cli-main.ts

+53-52
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ function validateSchema(json) {
4141
}
4242

4343
function readFileAsString(path) {
44-
var data = fs.readFileSync(path);
44+
const data = fs.readFileSync(path);
4545
return data.toString();
4646
}
4747

48-
var program = require('commander');
48+
const program = require('commander');
4949

5050
program
5151
.version(pkginfo.version)
@@ -74,7 +74,7 @@ program
7474
if (!arg) {
7575
program.outputHelp();
7676
} else {
77-
var command = program.commands.find(c => c._name == arg);
77+
const command = program.commands.find(c => c._name == arg);
7878
if (!command) {
7979
console.log(`Could not find a command for ${arg}`);
8080
} else {
@@ -86,7 +86,7 @@ program
8686
program
8787
.command('install')
8888
.description('Downloads and installs the Sandbox Client software.')
89-
.action(async function (dir, cmd) {
89+
.action(async function () {
9090
try {
9191
if (sandboxClientManager.isAlreadyInstalled()) {
9292
console.log("Sandbox Client is already installed.");
@@ -100,7 +100,7 @@ program
100100

101101
function showLocalSandboxes() {
102102
console.log("Local sandboxes:\n");
103-
var sandboxes = sandboxClientManager.getAllSandboxes().map(sb => {
103+
const sandboxes = sandboxClientManager.getAllSandboxes().map(sb => {
104104
return {
105105
current: sb.current ? "YES" : "",
106106
name: sb.name,
@@ -120,11 +120,11 @@ function showSandboxesTable(sandboxes) {
120120

121121
async function showRemoteSandboxes() {
122122
console.log("Loading sandboxes: \n");
123-
var localIds = new Set();
123+
const localIds = new Set();
124124
sandboxClientManager.getAllSandboxes().forEach(sb => localIds.add(sb.sandboxId));
125125
const allSandboxesResult = await cliUtils.spinner(sandboxSvc.getAllSandboxes());
126126
const quota = allSandboxesResult.quota;
127-
var sandboxes = allSandboxesResult.result.sandboxes.map(sb => {
127+
const sandboxes = allSandboxesResult.result.sandboxes.map(sb => {
128128
return {
129129
has_local: localIds.has(sb.sandboxId) ? "Y" : "N",
130130
name: sb.name,
@@ -154,12 +154,12 @@ program
154154
});
155155

156156
async function getRulesForSandboxId(sandboxId: string) {
157-
var sandbox: any = await cliUtils.spinner(sandboxSvc.getSandbox(sandboxId));
158-
var pIds = sandbox.properties.map(p => p.sandboxPropertyId);
159-
var r = [];
157+
const sandbox: any = await cliUtils.spinner(sandboxSvc.getSandbox(sandboxId));
158+
const pIds = sandbox.properties.map(p => p.sandboxPropertyId);
159+
const r = [];
160160

161-
for (var pid of pIds) {
162-
var obj: any = await cliUtils.spinner(sandboxSvc.getRules(sandboxId, pid));
161+
for (let pid of pIds) {
162+
const obj: any = await cliUtils.spinner(sandboxSvc.getRules(sandboxId, pid));
163163
r.push({
164164
title: `sandbox_property_id: ${pid}`,
165165
rules: obj.rules
@@ -189,20 +189,20 @@ function populateOrigins(papiNode, originsList) {
189189
}
190190

191191
function getOriginsForPapiRules(papiRules) {
192-
var o = [];
192+
const o = [];
193193
populateOrigins(papiRules, o);
194194
return o;
195195
}
196196

197197
async function showSandboxOverview(sandboxId: string) {
198-
var localSandbox = sandboxClientManager.getSandboxLocalData(sandboxId);
198+
const localSandbox = sandboxClientManager.getSandboxLocalData(sandboxId);
199199
if (localSandbox) {
200200
cliUtils.logWithBorder("Local sandbox information:");
201201
console.log("sandbox_id: " + sandboxId);
202202
console.log("local directory: " + localSandbox.sandboxFolder);
203203
console.log(`current: ${localSandbox.isCurrent}\n`);
204204
}
205-
var sandbox = await cliUtils.spinner(sandboxSvc.getSandbox(sandboxId));
205+
const sandbox = await cliUtils.spinner(sandboxSvc.getSandbox(sandboxId));
206206

207207
cliUtils.logWithBorder("Detailed information for the sandbox:");
208208

@@ -222,9 +222,9 @@ async function showSandboxOverview(sandboxId: string) {
222222
program
223223
.command('show [sandbox-identifier]')
224224
.description('Provides details about a sandbox.')
225-
.action(async function (arg, cmd) {
225+
.action(async function (arg) {
226226
try {
227-
var sandboxIdToUse = null;
227+
let sandboxIdToUse = null;
228228
if (!arg) {
229229
if (sandboxClientManager.hasCurrent()) {
230230
sandboxIdToUse = sandboxClientManager.getCurrentSandboxId();
@@ -243,9 +243,9 @@ program
243243
program
244244
.command('rules [sandbox-identifier]')
245245
.description('Shows rule tree for sandbox.')
246-
.action(async function (arg, cmd) {
246+
.action(async function (arg) {
247247
try {
248-
var sandboxIdToUse = null;
248+
let sandboxIdToUse = null;
249249
if (!arg) {
250250
if (sandboxClientManager.hasCurrent()) {
251251
sandboxIdToUse = sandboxClientManager.getCurrentSandboxId();
@@ -255,7 +255,7 @@ program
255255
} else {
256256
sandboxIdToUse = getSandboxIdFromIdentifier(arg);
257257
}
258-
var rulesList = await getRulesForSandboxId(sandboxIdToUse);
258+
const rulesList = await getRulesForSandboxId(sandboxIdToUse);
259259
rulesList.forEach(o => {
260260
cliUtils.logWithBorder(o.title, 'err');
261261
console.log(cliUtils.toJsonPretty(o.rules));
@@ -266,7 +266,7 @@ program
266266
});
267267

268268
function getLocalSandboxForIdentifier(identifier: string, failOnNoResult = true) {
269-
var results = sandboxClientManager.searchLocalSandboxes(identifier);
269+
const results = sandboxClientManager.searchLocalSandboxes(identifier);
270270
if (results.length == 0) {
271271
if (failOnNoResult) {
272272
cliUtils.logAndExit(1, `ERROR: Could not find any local sandboxes matching input: ${identifier}`)
@@ -291,7 +291,7 @@ function orCurrent(sandboxIdentifier) {
291291
}
292292

293293
function getSandboxIdFromIdentifier(sandboxIdentifier: string) {
294-
var sb = getLocalSandboxForIdentifier(sandboxIdentifier, false);
294+
const sb = getLocalSandboxForIdentifier(sandboxIdentifier, false);
295295
if (sb) {
296296
return sb.sandboxId;
297297
} else {
@@ -302,22 +302,22 @@ function getSandboxIdFromIdentifier(sandboxIdentifier: string) {
302302
program
303303
.command('use <sandbox-identifier>')
304304
.description('Sets the identified sandbox as currently active.')
305-
.action(function (arg, options) {
306-
var sb = getLocalSandboxForIdentifier(arg);
305+
.action(function (arg) {
306+
const sb = getLocalSandboxForIdentifier(arg);
307307
sandboxClientManager.makeCurrent(sb.sandboxId);
308308
console.log(`Sandbox: ${sb.name} is now active`)
309309
});
310310

311311
program
312312
.command('delete <sandbox-id>')
313313
.description('Deletes the sandbox.')
314-
.action(async function (sandboxId, options) {
314+
.action(async function (sandboxId) {
315315
try {
316316
if (!await cliUtils.confirm('Are you sure you want to delete this sandbox?')) {
317317
return;
318318
}
319319

320-
var progressMsg = `Deleting sandboxId: ${sandboxId}`;
320+
const progressMsg = `Deleting sandboxId: ${sandboxId}`;
321321
await cliUtils.spinner(sandboxSvc.deleteSandbox(sandboxId), progressMsg);
322322

323323
sandboxClientManager.flushLocalSandbox(sandboxId);
@@ -331,7 +331,7 @@ function parseToBoolean(str: string) {
331331
return false;
332332
}
333333
const parsedInput = str.trim().toLowerCase();
334-
var strToBool = new Map([
334+
const strToBool = new Map([
335335
['true', true],
336336
['t', true],
337337
['y', true],
@@ -354,8 +354,8 @@ program
354354
.option('-r, --rules <file>', 'JSON file containing a PAPI rule tree.')
355355
.option('-H, --requesthostnames <string>', 'Comma-delimited list of request hostnames within the sandbox.')
356356
.action(async function (sandboxId, sandboxPropertyId, options) {
357-
var rules = options.rules;
358-
var requestHostnames = options.requesthostnames;
357+
const rules = options.rules;
358+
const requestHostnames = options.requesthostnames;
359359
try {
360360
await updateHostnamesAndRules(requestHostnames, rules, sandboxId, sandboxPropertyId);
361361
console.log(`Successfully updated sandbox_id: ${sandboxId} sandbox_property_id: ${sandboxPropertyId}`);
@@ -463,15 +463,15 @@ async function createFromRules(papiFilePath: string, propForRules: string, hostn
463463
cliUtils.logAndExit(1, `ERROR: File: ${papiFilePath} does not exist.`);
464464
}
465465
const papiJson = getJsonFromFile(papiFilePath);
466-
const propertySpecObjMsg = `${JSON.stringify(propForRules)}`;
466+
`${JSON.stringify(propForRules)}`;
467467
return await cliUtils.spinner(sandboxSvc.createFromRules(papiJson, propForRules, hostnames, name, isClonable, cpcode), "creating new sandbox");
468468
}
469469

470470
function parsePropertySpecifier(propertySpecifier) {
471-
var propertySpec;
472-
var propertyVersion;
471+
let propertySpec;
472+
let propertyVersion;
473473
if (propertySpecifier.indexOf(':') > -1) {
474-
var parts = propertySpecifier.split(':').map(s => s.trim().toLowerCase());
474+
const parts = propertySpecifier.split(':').map(s => s.trim().toLowerCase());
475475
propertySpec = parts[0];
476476
propertyVersion = parts[1];
477477
} else {
@@ -483,7 +483,7 @@ function parsePropertySpecifier(propertySpecifier) {
483483
}
484484

485485
const propertySpecObj: any = {};
486-
var key;
486+
let key;
487487
if (validator.isInt(propertySpec)) {
488488
key = 'propertyId';
489489
} else {
@@ -524,7 +524,7 @@ async function createFromHostname(hostname: string, hostnames: Array<string>, is
524524
}
525525
526526
async function getOriginListForSandboxId(sandboxId: string): Promise<Array<string>> {
527-
var rulesList = await getRulesForSandboxId(sandboxId);
527+
const rulesList = await getRulesForSandboxId(sandboxId);
528528
const origins = new Set();
529529
rulesList.forEach(entry => {
530530
const originsForRules = getOriginsForPapiRules(entry.rules);
@@ -560,7 +560,7 @@ async function createFromPropertiesRecipe(recipe, cpcode) {
560560
console.log(`Creating sandbox and property 1 from recipe.`);
561561
const r = await cliUtils.spinner(createRecipeSandboxAndProperty(firstProp, propForRules, sandboxRecipe, cpcode));
562562
563-
for (var i = 1; i < properties.length; i++) {
563+
for (let i = 1; i < properties.length; i++) {
564564
try {
565565
console.log(`Creating property ${i + 1} from recipe.`);
566566
await cliUtils.spinner(createRecipeProperty(properties[i], r.sandboxId));
@@ -593,14 +593,14 @@ function validateAndBuildRecipe(recipeFilePath, name, clonable): any {
593593
cliUtils.logAndExit(1, `ERROR: File ${recipeFilePath} does not exist.`);
594594
}
595595
const recipe = getJsonFromFile(recipeFilePath);
596-
var r = validateSchema(recipe);
596+
const r = validateSchema(recipe);
597597
if (r.errors.length > 0) {
598598
cliUtils.logAndExit(1, `ERROR: There are issues with your recipe file\n ${r}`);
599599
}
600600
const sandboxRecipe = recipe.sandbox;
601601
sandboxRecipe.clonable = clonable || sandboxRecipe.clonable;
602602
sandboxRecipe.name = name || sandboxRecipe.name;
603-
var idx = 0;
603+
let idx = 0;
604604
605605
if (sandboxRecipe.properties) {
606606
sandboxRecipe.properties.forEach(p => {
@@ -647,9 +647,9 @@ async function updateFromRecipe(sandboxId, recipeFilePath, name, clonable) {
647647
console.log(`Updating sandbox information for sandbox_id: ${sandboxId}`);
648648
await sandboxSvc.updateSandbox(sandbox);
649649

650-
var pIds = sandbox.properties.map(p => p.sandboxPropertyId);
650+
const pIds = sandbox.properties.map(p => p.sandboxPropertyId);
651651
const first = pIds[0];
652-
for (var i = 1; i < pIds.length; i++) {
652+
for (let i = 1; i < pIds.length; i++) {
653653
const propertyId = pIds[i];
654654
console.log(`Deleting sandbox_property_id: ${propertyId}`);
655655
await cliUtils.spinner(sandboxSvc.deleteProperty(sandboxId, propertyId));
@@ -663,7 +663,7 @@ async function updateFromRecipe(sandboxId, recipeFilePath, name, clonable) {
663663
console.log(`Updating sandbox_property_id: ${first}`);
664664
await cliUtils.spinner(sandboxSvc.updateProperty(sandboxId, propertyObj));
665665

666-
for (var i = 0; i < sandboxRecipe.properties.length; i++) {
666+
for (let i = 0; i < sandboxRecipe.properties.length; i++) {
667667
const rp = sandboxRecipe.properties[i];
668668
console.log(`Re-building property: ${i + 1}`);
669669
await cliUtils.spinner(createRecipeProperty(rp, sandboxId));
@@ -679,7 +679,7 @@ async function createFromRecipe(recipeFilePath, name, clonable, cpcode) {
679679

680680
const sandboxRecipe = recipe.sandbox;
681681

682-
var res = null;
682+
let res = null;
683683
if (sandboxRecipe.properties) {
684684
res = await createFromPropertiesRecipe(recipe, cpcode);
685685
} else if (sandboxRecipe.cloneFrom) {
@@ -719,8 +719,8 @@ function createRecipeSandboxAndProperty(rp, propertyForRules, recipe, cpcode) {
719719
}
720720

721721
function oneOf(...args: any[]) {
722-
var r = false;
723-
for (var i = 0; i < args.length; i++) {
722+
let r = false;
723+
for (let i = 0; i < args.length; i++) {
724724
if (args[i]) {
725725
if (r) {
726726
return false;
@@ -793,7 +793,7 @@ program
793793

794794
const hostnames = hostnamesCsv ? parseHostnameCsv(hostnamesCsv) : undefined;
795795

796-
var r = null;
796+
let r = null;
797797
if (papiFilePath) {
798798
r = await createFromRules(papiFilePath, propForRules, hostnames, isClonable, name, cpcode);
799799
} else if (propertySpecifier) {
@@ -811,8 +811,8 @@ program
811811

812812
async function registerSandbox(sandboxId: string, jwt: string, name: string, clientConfig = null) {
813813
console.log('building origin list');
814-
var origins: Array<string> = await getOriginListForSandboxId(sandboxId);
815-
var passThrough = false;
814+
const origins: Array<string> = await getOriginListForSandboxId(sandboxId);
815+
let passThrough = false;
816816
let hasVariableForOrigin = false;
817817
if (origins.length > 0) {
818818
console.log(`Detected the following origins: ${origins.join(', ')}`);
@@ -824,7 +824,7 @@ async function registerSandbox(sandboxId: string, jwt: string, name: string, cli
824824
}
825825

826826
console.log('registering sandbox in local datastore');
827-
var registration = sandboxClientManager.registerNewSandbox(sandboxId, jwt, name, origins, clientConfig, passThrough);
827+
const registration = sandboxClientManager.registerNewSandbox(sandboxId, jwt, name, origins, clientConfig, passThrough);
828828

829829
console.info(`Successfully created sandbox_id ${sandboxId} Generated sandbox client configuration at ${registration.configPath} Edit this file to specify the port and host for your dev environment.`);
830830
if (hasVariableForOrigin) {
@@ -847,13 +847,14 @@ async function downloadClientIfNecessary() {
847847
program
848848
.command('start')
849849
.description('Starts the sandbox client.')
850-
.action(async function (dir, cmd) {
850+
.option('--print-logs', 'Print logs to standard output.')
851+
.action(async function (options) {
851852
try {
852853
if (sandboxClientManager.getAllSandboxes().length == 0) {
853854
console.log('there are no sandboxes configured');
854855
} else {
855856
await downloadClientIfNecessary();
856-
await sandboxClientManager.executeSandboxClient();
857+
await sandboxClientManager.executeSandboxClient(!!options.printLogs);
857858
}
858859
} catch (e) {
859860
cliUtils.logAndExit(1, 'ERROR: got exception during client execution: ' + e);
@@ -888,7 +889,7 @@ program
888889
const hostnameSpecifier = options.hostname;
889890
const hostnamesCsv = options.requesthostnames;
890891

891-
let sandboxId = null;
892+
let sandboxId;
892893
if (!arg) {
893894
sandboxId = sandboxClientManager.getCurrentSandboxId();
894895
if (!sandboxId) {
@@ -1075,7 +1076,7 @@ program
10751076
});
10761077

10771078
function helpExitOnNoArgs(cmd) {
1078-
var len = process.argv.slice(2).length;
1079+
const len = process.argv.slice(2).length;
10791080
if (!len || len <= 1) {
10801081
cmd.outputHelp();
10811082
process.exit();

0 commit comments

Comments
 (0)