Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --stdout to sam-translate.py #3222

Merged
merged 1 commit into from
Jun 21, 2023
Merged
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
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)