Skip to content

Commit 599dd63

Browse files
acmcelweephobologic
authored andcommitted
Use the ui I/O helper to properly serialize diff outputs (#717)
1 parent 7f98feb commit 599dd63

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

stacker/actions/diff.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .base import plan, build_walker
1212
from . import build
13+
from ..ui import ui
1314
from .. import exceptions
1415
from ..util import parse_cloudformation_template
1516
from ..status import (
@@ -167,9 +168,10 @@ def normalize_json(template):
167168
return result
168169

169170

170-
def print_stack_changes(stack_name, new_stack, old_stack, new_params,
171+
def build_stack_changes(stack_name, new_stack, old_stack, new_params,
171172
old_params):
172-
"""Prints out the parameters (if changed) and stack diff"""
173+
"""Builds a list of strings to represent the the parameters (if changed)
174+
and stack diff"""
173175
from_file = "old_%s" % (stack_name,)
174176
to_file = "new_%s" % (stack_name,)
175177
lines = difflib.context_diff(
@@ -178,13 +180,15 @@ def print_stack_changes(stack_name, new_stack, old_stack, new_params,
178180
n=7) # ensure at least a few lines of context are displayed afterward
179181

180182
template_changes = list(lines)
183+
log_lines = []
181184
if not template_changes:
182-
print("*** No changes to template ***")
185+
log_lines.append("*** No changes to template ***")
183186
param_diffs = diff_parameters(old_params, new_params)
184187
if param_diffs:
185-
print(format_params_diff(param_diffs))
188+
log_lines.append(format_params_diff(param_diffs))
186189
if template_changes:
187-
print("".join(template_changes))
190+
log_lines.append("".join(template_changes))
191+
return log_lines
188192

189193

190194
class Action(build.Action):
@@ -199,15 +203,19 @@ class Action(build.Action):
199203
config.
200204
"""
201205

202-
def _print_new_stack(self, stack, parameters):
203-
"""Prints out the parameters & stack contents of a new stack"""
204-
print("New template parameters:")
206+
def _build_new_template(self, stack, parameters):
207+
"""Constructs the parameters & contents of a new stack and returns a
208+
list(str) representation to be output to the user
209+
"""
210+
log_lines = ["New template parameters:"]
205211
for param in sorted(parameters,
206212
key=lambda param: param['ParameterKey']):
207-
print("%s = %s" % (param['ParameterKey'], param['ParameterValue']))
213+
log_lines.append("%s = %s" % (param['ParameterKey'],
214+
param['ParameterValue']))
208215

209-
print("\nNew template contents:")
210-
print("".join(stack))
216+
log_lines.append("\nNew template contents:")
217+
log_lines.append("".join(stack))
218+
return log_lines
211219

212220
def _diff_stack(self, stack, **kwargs):
213221
"""Handles the diffing a stack in CloudFormation vs our config"""
@@ -241,10 +249,10 @@ def _diff_stack(self, stack, **kwargs):
241249
new_template = stack.blueprint.rendered
242250
new_stack = normalize_json(new_template)
243251

244-
print("============== Stack: %s ==============" % (stack.name,))
252+
output = ["============== Stack: %s ==============" % (stack.name,)]
245253
# If this is a completely new template dump our params & stack
246254
if not old_template:
247-
self._print_new_stack(new_stack, parameters)
255+
output.extend(self._build_new_template(new_stack, parameters))
248256
else:
249257
# Diff our old & new stack/parameters
250258
old_template = parse_cloudformation_template(old_template)
@@ -260,8 +268,9 @@ def _diff_stack(self, stack, **kwargs):
260268
indent=4,
261269
default=str)
262270
)
263-
print_stack_changes(stack.name, new_stack, old_stack, new_params,
264-
old_params)
271+
output.extend(build_stack_changes(stack.name, new_stack, old_stack,
272+
new_params, old_params))
273+
ui.info('\n' + '\n'.join(output))
265274

266275
stack.set_outputs(
267276
provider.get_output_dict(provider_stack))
@@ -285,6 +294,7 @@ def run(self, concurrency=0, *args, **kwargs):
285294
plan.execute(walker)
286295

287296
"""Don't ever do anything for pre_run or post_run"""
297+
288298
def pre_run(self, *args, **kwargs):
289299
pass
290300

0 commit comments

Comments
 (0)