Skip to content

Commit

Permalink
feat: add --stdout to sam-translate.py (aws#3222)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffa committed Jun 21, 2023
1 parent 3bd8a54 commit 53eddc7
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions bin/sam-translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -100,14 +105,18 @@ 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]

try:
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)
Expand All @@ -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)

0 comments on commit 53eddc7

Please sign in to comment.