Skip to content

Commit 53eddc7

Browse files
authored
feat: add --stdout to sam-translate.py (#3222)
1 parent 3bd8a54 commit 53eddc7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

bin/sam-translate.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
help="Enables verbose logging",
5959
action="store_true",
6060
)
61+
parser.add_argument(
62+
"--stdout",
63+
help="Write transformed template to stdout instead of a file",
64+
action="store_true",
65+
)
6166
cli_options = parser.parse_args()
6267

6368
if cli_options.verbose:
@@ -100,14 +105,18 @@ def package(input_file_path: Path) -> Path:
100105
return package_output_template_file
101106

102107

103-
def transform_template(input_file_path: Path, output_file_path: Path): # type: ignore[no-untyped-def]
108+
def transform_template(input_file_path: Path, output_file_path: Path, stdout: bool): # type: ignore[no-untyped-def]
104109
with input_file_path.open() as f:
105110
sam_template = yaml_parse(f) # type: ignore[no-untyped-call]
106111

107112
try:
108113
cloud_formation_template = transform(sam_template, {}, ManagedPolicyLoader(iam_client))
109114
cloud_formation_template_prettified = json.dumps(cloud_formation_template, indent=1)
110115

116+
if stdout:
117+
print(cloud_formation_template_prettified)
118+
return
119+
111120
output_file_path.write_text(cloud_formation_template_prettified, encoding="utf-8")
112121

113122
print("Wrote transformed CloudFormation template to: ", output_file_path)
@@ -132,10 +141,10 @@ def deploy(template_file: Path) -> None:
132141

133142
if cli_options.command == "package":
134143
package_output_template_file = package(input_file_path)
135-
transform_template(package_output_template_file, output_file_path)
144+
transform_template(package_output_template_file, output_file_path, cli_options.stdout)
136145
elif cli_options.command == "deploy":
137146
package_output_template_file = package(input_file_path)
138-
transform_template(package_output_template_file, output_file_path)
147+
transform_template(package_output_template_file, output_file_path, cli_options.stdout)
139148
deploy(output_file_path)
140149
else:
141-
transform_template(input_file_path, output_file_path)
150+
transform_template(input_file_path, output_file_path, cli_options.stdout)

0 commit comments

Comments
 (0)