From a9f0f1ac239afba5d1b6d5a2d1750dcf045327c3 Mon Sep 17 00:00:00 2001 From: Lucas <12496191+lucashuy@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:33:24 -0700 Subject: [PATCH 1/4] Updated build success message for hook packages --- samcli/commands/build/build_context.py | 64 ++++++++++++++----- samcli/commands/build/command.py | 1 + .../commands/buildcmd/test_build_context.py | 2 +- tests/unit/commands/buildcmd/test_command.py | 1 + 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/samcli/commands/build/build_context.py b/samcli/commands/build/build_context.py index e512671401..4cbd5c6352 100644 --- a/samcli/commands/build/build_context.py +++ b/samcli/commands/build/build_context.py @@ -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 @@ -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 @@ -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() @@ -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), @@ -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_api_suggestion = "Start a local API: sam local start-api" + 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_api_suggestion += hook_package_flag + start_lambda_suggestion += hook_package_flag - msg = """\nBuilt Artifacts : {artifacts_dir} -Built Template : {template} + commands = [invoke_suggestion, start_api_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 diff --git a/samcli/commands/build/command.py b/samcli/commands/build/command.py index e7b7298494..fa4866c0fb 100644 --- a/samcli/commands/build/command.py +++ b/samcli/commands/build/command.py @@ -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() diff --git a/tests/unit/commands/buildcmd/test_build_context.py b/tests/unit/commands/buildcmd/test_build_context.py index a517325715..8cdfd22ae4 100644 --- a/tests/unit/commands/buildcmd/test_build_context.py +++ b/tests/unit/commands/buildcmd/test_build_context.py @@ -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() diff --git a/tests/unit/commands/buildcmd/test_command.py b/tests/unit/commands/buildcmd/test_command.py index aacabaa754..0fbae66e53 100644 --- a/tests/unit/commands/buildcmd/test_command.py +++ b/tests/unit/commands/buildcmd/test_command.py @@ -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) From babd15bf7f7227c40138f24263cc381c071bf332 Mon Sep 17 00:00:00 2001 From: Lucas <12496191+lucashuy@users.noreply.github.com> Date: Thu, 27 Oct 2022 17:11:26 -0700 Subject: [PATCH 2/4] Fix linting error --- tests/unit/commands/buildcmd/test_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/commands/buildcmd/test_command.py b/tests/unit/commands/buildcmd/test_command.py index 0fbae66e53..1186044715 100644 --- a/tests/unit/commands/buildcmd/test_command.py +++ b/tests/unit/commands/buildcmd/test_command.py @@ -59,7 +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 + hook_package_id=None, ) ctx_mock.run.assert_called_with() self.assertEqual(ctx_mock.run.call_count, 1) From f91edbcbec52f4dc47dcb2d447334b809fe7ef28 Mon Sep 17 00:00:00 2001 From: Lucas <12496191+lucashuy@users.noreply.github.com> Date: Fri, 28 Oct 2022 10:02:21 -0700 Subject: [PATCH 3/4] Removed start API suggested command --- samcli/commands/build/build_context.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samcli/commands/build/build_context.py b/samcli/commands/build/build_context.py index 4cbd5c6352..e1eb889233 100644 --- a/samcli/commands/build/build_context.py +++ b/samcli/commands/build/build_context.py @@ -371,7 +371,6 @@ def _gen_success_msg(self, artifacts_dir: str, output_template_path: str, is_def 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_api_suggestion = "Start a local API: sam local start-api" start_lambda_suggestion = "Emulate local Lambda functions: sam local start-lambda" if not is_default_build_dir: @@ -383,10 +382,9 @@ def _gen_success_msg(self, artifacts_dir: str, output_template_path: str, is_def # 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_api_suggestion += hook_package_flag start_lambda_suggestion += hook_package_flag - commands = [invoke_suggestion, start_api_suggestion, start_lambda_suggestion] + commands = [invoke_suggestion, start_lambda_suggestion] msg = f"""\nBuilt Artifacts : {artifacts_dir} Built Template : {output_template_path} From 425c94fd6ec959edaf7bebbcc422f68dae12eb1c Mon Sep 17 00:00:00 2001 From: Lucas <12496191+lucashuy@users.noreply.github.com> Date: Fri, 28 Oct 2022 11:51:00 -0700 Subject: [PATCH 4/4] Added hook flag to invoke suggestion --- samcli/commands/build/build_context.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samcli/commands/build/build_context.py b/samcli/commands/build/build_context.py index e1eb889233..eb06694ae8 100644 --- a/samcli/commands/build/build_context.py +++ b/samcli/commands/build/build_context.py @@ -382,7 +382,9 @@ def _gen_success_msg(self, artifacts_dir: str, output_template_path: str, is_def # 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 commands = [invoke_suggestion, start_lambda_suggestion]