Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ export const scenarios = {
import { fileURLToPath } from "node:url";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
parseScenarioArgs(scenarios);
parseScenarioArgs(scenarios, {
name: "Resilient Workflow",
synopsis:
"node index.js --scenario <deploy | demo | destroy> [-h|--help] [-y|--yes] [-v|--verbose]",
description: "Deploy and interact with scalable EC2 instances.",
});
}
87 changes: 40 additions & 47 deletions javascriptv3/example_code/libs/scenario/scenario-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,67 @@
// SPDX-License-Identifier: Apache-2.0

import { parseArgs } from "node:util";
import { printManPage } from "../utils/util-node.js";
import { logger } from "../utils/util-log.js";

/**
* @param {Record<string, import('./scenario.js').Scenario>} scenarios
* @param {{name: string, synopsis: string, description: string}} [info] - High level info describing the suite of scenarios.
*/
export function parseScenarioArgs(scenarios) {
const help = `Usage:
node . -s <${Object.keys(scenarios).join("|")}>
node . -h

Options:
[-s|--scenario, <scenario>] [-h|--help] [-y|--yes] [-v|--verbose]

-h, --help Show this help message.
-s, --scenario The name of a scenario to run.
-y, --yes Assume "yes" to all prompts.
-v, --verbose Show debug information.
`;

const { values } = parseArgs({
options: {
help: {
type: "boolean",
short: "h",
},
scenario: {
short: "s",
type: "string",
},
yes: {
short: "y",
type: "boolean",
},
verbose: {
short: "v",
type: "boolean",
},
export const parseScenarioArgs = (
scenarios,
{ name = "", synopsis = "", description = "" } = {},
) => {
const options = {
help: {
type: "boolean",
short: "h",
},
});
scenario: {
short: "s",
type: "string",
},
yes: {
short: "y",
type: "boolean",
},
verbose: {
short: "v",
type: "boolean",
},
};

const { values } = parseArgs({ options });
const helpPage = () => {
printManPage(options, {
name,
synopsis: synopsis ?? `node . -s <${Object.keys(scenarios).join("|")}>`,
description,
});
};

if (values.help) {
console.log(help);
helpPage();
return;
}

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

if (!(values.scenario in scenarios)) {
throw new Error(`Invalid scenario: ${values.scenario}\n${help}`);
logger.error(`Invalid scenario: ${values.scenario}`);
}

if (values.verbose) {
console.log(
`[DEBUG ${new Date().toISOString()}] Running scenario: ${
scenarios[values.scenario].name
}`,
);
console.log(
`[DEBUG ${new Date().toISOString()}] State: ${JSON.stringify(
scenarios[values.scenario].state,
)}`,
);
logger.debug(`Running scenario: ${scenarios[values.scenario].name}`);
logger.debug(`State: ${JSON.stringify(scenarios[values.scenario].state)}`);
}

return scenarios[values.scenario].run({
confirmAll: values.yes,
verbose: values.verbose,
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,11 @@ const scenarios = {
// Call function if run directly
import { fileURLToPath } from "node:url";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
parseScenarioArgs(scenarios);
parseScenarioArgs(scenarios, {
name: "Health Imaging Workflow",
description:
"Work with DICOM images using an AWS Health Imaging data store.",
synopsis:
"node index.js --scenario <deploy | demo | destroy> [-h|--help] [-y|--yes] [-v|--verbose]",
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,11 @@ import { replAction } from "./repl.steps.js";

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