From 53eddc7117c78adc509526e640b4bc3ddc238dd2 Mon Sep 17 00:00:00 2001 From: Christoffer Rehn <1280602+hoffa@users.noreply.github.com> Date: Wed, 21 Jun 2023 10:51:44 -0700 Subject: [PATCH] feat: add `--stdout` to `sam-translate.py` (#3222) --- bin/sam-translate.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/bin/sam-translate.py b/bin/sam-translate.py index 2dde9b1d0..80903ae8d 100755 --- a/bin/sam-translate.py +++ b/bin/sam-translate.py @@ -58,6 +58,11 @@ help="Enables verbose logging", action="store_true", ) +parser.add_argument( + "--stdout", + help="Write transformed template to stdout instead of a file", + action="store_true", +) cli_options = parser.parse_args() if cli_options.verbose: @@ -100,7 +105,7 @@ def package(input_file_path: Path) -> Path: return package_output_template_file -def transform_template(input_file_path: Path, output_file_path: Path): # type: ignore[no-untyped-def] +def transform_template(input_file_path: Path, output_file_path: Path, stdout: bool): # type: ignore[no-untyped-def] with input_file_path.open() as f: sam_template = yaml_parse(f) # type: ignore[no-untyped-call] @@ -108,6 +113,10 @@ def transform_template(input_file_path: Path, output_file_path: Path): # type: cloud_formation_template = transform(sam_template, {}, ManagedPolicyLoader(iam_client)) cloud_formation_template_prettified = json.dumps(cloud_formation_template, indent=1) + if stdout: + print(cloud_formation_template_prettified) + return + output_file_path.write_text(cloud_formation_template_prettified, encoding="utf-8") print("Wrote transformed CloudFormation template to: ", output_file_path) @@ -132,10 +141,10 @@ def deploy(template_file: Path) -> None: if cli_options.command == "package": package_output_template_file = package(input_file_path) - transform_template(package_output_template_file, output_file_path) + transform_template(package_output_template_file, output_file_path, cli_options.stdout) elif cli_options.command == "deploy": package_output_template_file = package(input_file_path) - transform_template(package_output_template_file, output_file_path) + transform_template(package_output_template_file, output_file_path, cli_options.stdout) deploy(output_file_path) else: - transform_template(input_file_path, output_file_path) + transform_template(input_file_path, output_file_path, cli_options.stdout)