Skip to content

Commit d4d5d10

Browse files
committed
JavaScript (v3): Scenarios - Add metadata to parseScenarioArgs input and refactor.
1 parent f978cc0 commit d4d5d10

File tree

4 files changed

+60
-50
lines changed

4 files changed

+60
-50
lines changed

javascriptv3/example_code/cross-services/wkflw-resilient-service/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ export const scenarios = {
4242
import { fileURLToPath } from "node:url";
4343

4444
if (process.argv[1] === fileURLToPath(import.meta.url)) {
45-
parseScenarioArgs(scenarios);
45+
parseScenarioArgs(scenarios, {
46+
name: "Resilient Workflow",
47+
synopsis:
48+
"node index.js --scenario <deploy | demo | destroy> [-h|--help] [-y|--yes] [-v|--verbose]",
49+
description: "Deploy and interact with scalable EC2 instances.",
50+
});
4651
}

javascriptv3/example_code/libs/scenario/scenario-parser.js

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,67 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { parseArgs } from "node:util";
5+
import { printManPage } from "../utils/util-node.js";
6+
import { logger } from "../utils/util-log.js";
57

68
/**
79
* @param {Record<string, import('./scenario.js').Scenario>} scenarios
10+
* @param {{name: string, synopsis: string, description: string}} [info] - High level info describing the suite of scenarios.
811
*/
9-
export function parseScenarioArgs(scenarios) {
10-
const help = `Usage:
11-
node . -s <${Object.keys(scenarios).join("|")}>
12-
node . -h
13-
14-
Options:
15-
[-s|--scenario, <scenario>] [-h|--help] [-y|--yes] [-v|--verbose]
16-
17-
-h, --help Show this help message.
18-
-s, --scenario The name of a scenario to run.
19-
-y, --yes Assume "yes" to all prompts.
20-
-v, --verbose Show debug information.
21-
`;
22-
23-
const { values } = parseArgs({
24-
options: {
25-
help: {
26-
type: "boolean",
27-
short: "h",
28-
},
29-
scenario: {
30-
short: "s",
31-
type: "string",
32-
},
33-
yes: {
34-
short: "y",
35-
type: "boolean",
36-
},
37-
verbose: {
38-
short: "v",
39-
type: "boolean",
40-
},
12+
export const parseScenarioArgs = (
13+
scenarios,
14+
{ name = "", synopsis = "", description = "" } = {},
15+
) => {
16+
const options = {
17+
help: {
18+
type: "boolean",
19+
short: "h",
4120
},
42-
});
21+
scenario: {
22+
short: "s",
23+
type: "string",
24+
},
25+
yes: {
26+
short: "y",
27+
type: "boolean",
28+
},
29+
verbose: {
30+
short: "v",
31+
type: "boolean",
32+
},
33+
};
34+
35+
const { values } = parseArgs({ options });
36+
const helpPage = () => {
37+
printManPage(options, {
38+
name,
39+
synopsis: synopsis ?? `node . -s <${Object.keys(scenarios).join("|")}>`,
40+
description,
41+
});
42+
};
4343

4444
if (values.help) {
45-
console.log(help);
45+
helpPage();
4646
return;
4747
}
4848

4949
if (!values.scenario) {
50-
console.log(`Missing required argument: -s, --scenario\n\n${help}`);
50+
logger.error("Missing required argument: -s, --scenario");
51+
helpPage();
5152
return;
5253
}
5354

5455
if (!(values.scenario in scenarios)) {
55-
throw new Error(`Invalid scenario: ${values.scenario}\n${help}`);
56+
logger.error(`Invalid scenario: ${values.scenario}`);
5657
}
5758

5859
if (values.verbose) {
59-
console.log(
60-
`[DEBUG ${new Date().toISOString()}] Running scenario: ${
61-
scenarios[values.scenario].name
62-
}`,
63-
);
64-
console.log(
65-
`[DEBUG ${new Date().toISOString()}] State: ${JSON.stringify(
66-
scenarios[values.scenario].state,
67-
)}`,
68-
);
60+
logger.debug(`Running scenario: ${scenarios[values.scenario].name}`);
61+
logger.debug(`State: ${JSON.stringify(scenarios[values.scenario].state)}`);
6962
}
7063

7164
return scenarios[values.scenario].run({
7265
confirmAll: values.yes,
7366
verbose: values.verbose,
7467
});
75-
}
68+
};

javascriptv3/example_code/medical-imaging/scenarios/health-image-sets/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,11 @@ const scenarios = {
9797
// Call function if run directly
9898
import { fileURLToPath } from "node:url";
9999
if (process.argv[1] === fileURLToPath(import.meta.url)) {
100-
parseScenarioArgs(scenarios);
100+
parseScenarioArgs(scenarios, {
101+
name: "Health Imaging Workflow",
102+
description:
103+
"Work with DICOM images using an AWS Health Imaging data store.",
104+
synopsis:
105+
"node index.js --scenario <deploy | demo | destroy> [-h|--help] [-y|--yes] [-v|--verbose]",
106+
});
101107
}

javascriptv3/example_code/s3/scenarios/object-locking/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,11 @@ import { replAction } from "./repl.steps.js";
101101

102102
if (process.argv[1] === fileURLToPath(import.meta.url)) {
103103
const objectLockingScenarios = getWorkflowStages(Scenarios);
104-
Scenarios.parseScenarioArgs(objectLockingScenarios);
104+
Scenarios.parseScenarioArgs(objectLockingScenarios, {
105+
name: "Amazon S3 object locking workflow",
106+
description:
107+
"Work with Amazon Simple Storage Service (Amazon S3) object locking features.",
108+
synopsis:
109+
"node index.js --scenario <deploy | demo | clean> [-h|--help] [-y|--yes] [-v|--verbose]",
110+
});
105111
}

0 commit comments

Comments
 (0)