Skip to content

Commit

Permalink
fix: Updated build success message for hook packages (#4351)
Browse files Browse the repository at this point in the history
* Updated build success message for hook packages

* Fix linting error

* Removed start API suggested command

* Added hook flag to invoke suggestion
  • Loading branch information
lucashuy authored and moelasmar committed Oct 31, 2022
1 parent 54b85e7 commit 7291c3a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
64 changes: 47 additions & 17 deletions samcli/commands/build/build_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(
stack_name: Optional[str] = None,
print_success_message: bool = True,
locate_layer_nested: bool = False,
hook_package_id: Optional[str] = None,
) -> None:
"""
Initialize the class
Expand Down Expand Up @@ -126,6 +127,8 @@ def __init__(
Print successful message
locate_layer_nested: bool
Locate layer to its actual, worked with nested stack
hook_package_id: Optional[str]
Name of the hook package
"""

self._resource_identifier = resource_identifier
Expand Down Expand Up @@ -163,6 +166,7 @@ def __init__(
self._container_manager: Optional[ContainerManager] = None
self._stacks: List[Stack] = []
self._locate_layer_nested = locate_layer_nested
self._hook_package_id = hook_package_id

def __enter__(self) -> "BuildContext":
self.set_up()
Expand Down Expand Up @@ -272,7 +276,7 @@ def run(self):
output_template_path_in_success_message = out_template_path

if self._print_success_message:
msg = self.gen_success_msg(
msg = self._gen_success_msg(
build_dir_in_success_message,
output_template_path_in_success_message,
os.path.abspath(self.build_dir) == os.path.abspath(DEFAULT_BUILD_DIR),
Expand Down Expand Up @@ -344,29 +348,55 @@ def _handle_build_post_processing(self, builder: ApplicationBuilder, build_resul

move_template(stack.location, output_template_path, modified_template)

@staticmethod
def gen_success_msg(artifacts_dir: str, output_template_path: str, is_default_build_dir: bool) -> str:
def _gen_success_msg(self, artifacts_dir: str, output_template_path: str, is_default_build_dir: bool) -> str:
"""
Generates a success message containing some suggested commands to run
invoke_cmd = "sam local invoke"
if not is_default_build_dir:
invoke_cmd += " -t {}".format(output_template_path)
Parameters
----------
artifacts_dir: str
A string path representing the folder of built artifacts
output_template_path: str
A string path representing the final template file
is_default_build_dir: bool
True if the build folder is the folder defined by SAM CLI
Returns
-------
str
A formatted success message string
"""

validate_suggestion = "Validate SAM template: sam validate"
invoke_suggestion = "Invoke Function: sam local invoke"
sync_suggestion = "Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch"
deploy_suggestion = "Deploy: sam deploy --guided"
start_lambda_suggestion = "Emulate local Lambda functions: sam local start-lambda"

deploy_cmd = "sam deploy --guided"
if not is_default_build_dir:
deploy_cmd += " --template-file {}".format(output_template_path)
invoke_suggestion += " -t {}".format(output_template_path)
deploy_suggestion += " --template-file {}".format(output_template_path)

commands = [validate_suggestion, invoke_suggestion, sync_suggestion, deploy_suggestion]

# check if we have used a hook package before building
if self._hook_package_id:
hook_package_flag = f" --hook-package-id {self._hook_package_id}"

start_lambda_suggestion += hook_package_flag
invoke_suggestion += hook_package_flag

msg = """\nBuilt Artifacts : {artifacts_dir}
Built Template : {template}
commands = [invoke_suggestion, start_lambda_suggestion]

msg = f"""\nBuilt Artifacts : {artifacts_dir}
Built Template : {output_template_path}
Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: {invokecmd}
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: {deploycmd}
""".format(
invokecmd=invoke_cmd, deploycmd=deploy_cmd, artifacts_dir=artifacts_dir, template=output_template_path
)
"""

# add bullet point then join all the commands with new line
msg += "[*] " + f"{os.linesep}[*] ".join(commands)

return msg

Expand Down
1 change: 1 addition & 0 deletions samcli/commands/build/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def do_cli( # pylint: disable=too-many-locals, too-many-statements
build_images=processed_build_images,
excluded_resources=exclude,
aws_region=click_ctx.region,
hook_package_id=hook_package_id,
) as ctx:
ctx.run()

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/commands/buildcmd/test_build_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ def test_run_sync_build_context(
create_auto_dependency_layer=auto_dependency_layer,
print_success_message=False,
) as build_context:
with patch("samcli.commands.build.build_context.BuildContext.gen_success_msg") as mock_message:
with patch("samcli.commands.build.build_context.BuildContext._gen_success_msg") as mock_message:
build_context.run()
mock_message.assert_not_called()

Expand Down
1 change: 1 addition & 0 deletions tests/unit/commands/buildcmd/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_must_succeed_build(self, os_mock, BuildContextMock, mock_build_click):
build_images={},
excluded_resources=(),
aws_region=ctx_mock.region,
hook_package_id=None,
)
ctx_mock.run.assert_called_with()
self.assertEqual(ctx_mock.run.call_count, 1)
Expand Down

0 comments on commit 7291c3a

Please sign in to comment.