-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sam remote invoke help text and UX fixes (#5366)
* Improve remote invoke help text and fix some UX bugs * Updated help text for parameter option * Updated test class name * Updated test method name * Updated help text for output-format and event-file * Address feedback * Updated help text for parameter option * Changed --output-format name to output and the values to text/json * Handle empty event for lambda and read from stdin when - is passed for event-file
- Loading branch information
Showing
20 changed files
with
449 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
""" | ||
Invoke Command Class. | ||
""" | ||
import json | ||
|
||
from click import Context, style | ||
|
||
from samcli.cli.core.command import CoreCommand | ||
from samcli.cli.row_modifiers import RowDefinition, ShowcaseRowModifier | ||
from samcli.commands.remote.invoke.core.formatters import RemoteInvokeCommandHelpTextFormatter | ||
from samcli.commands.remote.invoke.core.options import OPTIONS_INFO | ||
|
||
|
||
class RemoteInvokeCommand(CoreCommand): | ||
class CustomFormatterContext(Context): | ||
formatter_class = RemoteInvokeCommandHelpTextFormatter | ||
|
||
context_class = CustomFormatterContext | ||
|
||
@staticmethod | ||
def format_examples(ctx: Context, formatter: RemoteInvokeCommandHelpTextFormatter): | ||
with formatter.indented_section(name="Examples", extra_indents=1): | ||
with formatter.indented_section(name="Invoke default lambda function with empty event", extra_indents=1): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style(f"${ctx.command_path} --stack-name hello-world"), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section( | ||
name="Invoke default lambda function with event passed as text input", extra_indents=1 | ||
): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"${ctx.command_path} --stack-name hello-world -e '{json.dumps({'message':'hello!'})}'" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section(name="Invoke named lambda function with an event file", extra_indents=1): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"${ctx.command_path} --stack-name " | ||
f"hello-world HelloWorldFunction --event-file event.json" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section(name="Invoke lambda function with event as stdin input", extra_indents=1): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"$ echo '{json.dumps({'message':'hello!'})}' | " | ||
f"{ctx.command_path} HelloWorldFunction --event-file -" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section( | ||
name="Invoke lambda function using lambda ARN and get the full AWS API response", extra_indents=1 | ||
): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"${ctx.command_path} arn:aws:lambda:us-west-2:123456789012:function:my-function -e <>" | ||
f" --output json" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section( | ||
name="Asynchronously invoke lambda function with additional boto parameters", extra_indents=1 | ||
): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"${ctx.command_path} HelloWorldFunction -e <> " | ||
f"--parameter InvocationType=Event --parameter Qualifier=MyQualifier" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
with formatter.indented_section( | ||
name="Dry invoke a lambda function to validate parameter values and user/role permissions", | ||
extra_indents=1, | ||
): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
text="\n", | ||
), | ||
RowDefinition( | ||
name=style( | ||
f"${ctx.command_path} HelloWorldFunction -e <> --output json " | ||
f"--parameter InvocationType=DryRun" | ||
), | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
|
||
@staticmethod | ||
def format_acronyms(formatter: RemoteInvokeCommandHelpTextFormatter): | ||
with formatter.indented_section(name="Acronyms", extra_indents=1): | ||
formatter.write_rd( | ||
[ | ||
RowDefinition( | ||
name="ARN", | ||
text="Amazon Resource Name", | ||
extra_row_modifiers=[ShowcaseRowModifier()], | ||
), | ||
] | ||
) | ||
|
||
def format_options(self, ctx: Context, formatter: RemoteInvokeCommandHelpTextFormatter) -> None: # type:ignore | ||
# NOTE: `ignore` is put in place here for mypy even though it is the correct behavior, | ||
# as the `formatter_class` can be set in subclass of Command. If ignore is not set, | ||
# mypy raises argument needs to be HelpFormatter as super class defines it. | ||
|
||
self.format_description(formatter) | ||
RemoteInvokeCommand.format_examples(ctx, formatter) | ||
RemoteInvokeCommand.format_acronyms(formatter) | ||
|
||
CoreCommand._format_options( | ||
ctx=ctx, params=self.get_params(ctx), formatter=formatter, formatting_options=OPTIONS_INFO | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Remote Invoke Command Formatter. | ||
""" | ||
from samcli.cli.formatters import RootCommandHelpTextFormatter | ||
from samcli.cli.row_modifiers import BaseLineRowModifier | ||
from samcli.commands.remote.invoke.core.options import ALL_OPTIONS | ||
|
||
|
||
class RemoteInvokeCommandHelpTextFormatter(RootCommandHelpTextFormatter): | ||
ADDITIVE_JUSTIFICATION = 17 | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# NOTE: Add Additional space after determining the longest option. | ||
# However, do not justify with padding for more than half the width of | ||
# the terminal to retain aesthetics. | ||
self.left_justification_length = min( | ||
max([len(option) for option in ALL_OPTIONS]) + self.ADDITIVE_JUSTIFICATION, | ||
self.width // 2 - self.indent_increment, | ||
) | ||
self.modifiers = [BaseLineRowModifier()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Remote Invoke Command Options related Datastructures for formatting. | ||
""" | ||
from typing import Dict, List | ||
|
||
from samcli.cli.core.options import ALL_COMMON_OPTIONS, add_common_options_info | ||
from samcli.cli.row_modifiers import RowDefinition | ||
|
||
# NOTE: The ordering of the option lists matter, they are the order | ||
# in which options will be displayed. | ||
|
||
INFRASTRUCTURE_OPTION_NAMES: List[str] = ["stack_name"] | ||
|
||
INPUT_EVENT_OPTIONS: List[str] = ["event", "event_file"] | ||
|
||
ADDITIONAL_OPTIONS: List[str] = ["parameter", "output"] | ||
|
||
AWS_CREDENTIAL_OPTION_NAMES: List[str] = ["region", "profile"] | ||
|
||
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] | ||
|
||
OTHER_OPTIONS: List[str] = ["debug"] | ||
|
||
ALL_OPTIONS: List[str] = ( | ||
INFRASTRUCTURE_OPTION_NAMES | ||
+ INPUT_EVENT_OPTIONS | ||
+ ADDITIONAL_OPTIONS | ||
+ AWS_CREDENTIAL_OPTION_NAMES | ||
+ CONFIGURATION_OPTION_NAMES | ||
+ ALL_COMMON_OPTIONS | ||
) | ||
|
||
OPTIONS_INFO: Dict[str, Dict] = { | ||
"Infrastructure Options": { | ||
"option_names": {opt: {"rank": idx} for idx, opt in enumerate(INFRASTRUCTURE_OPTION_NAMES)} | ||
}, | ||
"Input Event Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(INPUT_EVENT_OPTIONS)}}, | ||
"Additional Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(ADDITIONAL_OPTIONS)}}, | ||
"AWS Credential Options": { | ||
"option_names": {opt: {"rank": idx} for idx, opt in enumerate(AWS_CREDENTIAL_OPTION_NAMES)} | ||
}, | ||
"Configuration Options": { | ||
"option_names": {opt: {"rank": idx} for idx, opt in enumerate(CONFIGURATION_OPTION_NAMES)}, | ||
"extras": [ | ||
RowDefinition(name="Learn more about configuration files at:"), | ||
RowDefinition( | ||
name="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli" | ||
"-config.html. " | ||
), | ||
], | ||
}, | ||
} | ||
|
||
add_common_options_info(OPTIONS_INFO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.