-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: add a table for package help text. #5298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
aa43185
7cda0da
5438fa7
158e55e
8fc38c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """ | ||
| `sam package` command class for help text visual layer. | ||
| """ | ||
| import click | ||
| from click import Context, style | ||
| from rich.table import Table | ||
|
|
||
| from samcli.cli.core.command import CoreCommand | ||
| from samcli.cli.row_modifiers import RowDefinition, ShowcaseRowModifier | ||
| from samcli.commands.package.core.formatters import PackageCommandHelpTextFormatter | ||
| from samcli.commands.package.core.options import OPTIONS_INFO | ||
| from samcli.lib.utils.resources import resources_generator | ||
|
|
||
| COL_SIZE_MODIFIER = 38 | ||
|
|
||
|
|
||
| class PackageCommand(CoreCommand): | ||
sriram-mv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| `sam` package specific command class that specializes in the visual appearance | ||
| of `sam package` help text. | ||
| It hosts a custom formatter, examples, table for supported resources, acronyms | ||
| and how options are to be used in the CLI for `sam package`. | ||
| """ | ||
|
|
||
| class CustomFormatterContext(Context): | ||
| formatter_class = PackageCommandHelpTextFormatter | ||
|
|
||
| context_class = CustomFormatterContext | ||
|
|
||
| @staticmethod | ||
| def format_examples(ctx: Context, formatter: PackageCommandHelpTextFormatter): | ||
| with formatter.indented_section(name="Examples", extra_indents=1): | ||
| with formatter.indented_section(name="Automatic resolution of S3 buckets", extra_indents=1): | ||
| formatter.write_rd( | ||
| [ | ||
| RowDefinition( | ||
| text="\n", | ||
| ), | ||
| RowDefinition( | ||
| name=style(f"$ {ctx.command_path} --resolve-s3"), | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| ], | ||
| col_max=COL_SIZE_MODIFIER, | ||
| ) | ||
| with formatter.indented_section(name="Get packaged template", extra_indents=1): | ||
| formatter.write_rd( | ||
| [ | ||
| RowDefinition( | ||
| text="\n", | ||
| ), | ||
| RowDefinition( | ||
| name=style(f"$ {ctx.command_path} --resolve-s3 --output-template-file packaged.yaml"), | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| ], | ||
| col_max=COL_SIZE_MODIFIER, | ||
| ) | ||
| with formatter.indented_section(name="Customized location for uploading artifacts", extra_indents=1): | ||
| formatter.write_rd( | ||
| [ | ||
| RowDefinition( | ||
| text="\n", | ||
| ), | ||
| RowDefinition( | ||
| name=style( | ||
| f"$ {ctx.command_path} --s3-bucket S3_BUCKET --output-template-file packaged.yaml" | ||
| ), | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| ], | ||
| col_max=COL_SIZE_MODIFIER, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def format_table(formatter: PackageCommandHelpTextFormatter): | ||
| with formatter.section(name="Supported Resources"): | ||
| pass | ||
| ctx = click.get_current_context() | ||
| table = Table(width=ctx.max_content_width) | ||
| table.add_column("Resource") | ||
| table.add_column("Location") | ||
| for resource, location in resources_generator(): | ||
| table.add_row(resource, location) | ||
| with ctx.obj.console.capture() as capture: | ||
| ctx.obj.console.print(table) | ||
| formatter.write_rd( | ||
| [ | ||
| RowDefinition(name="\n"), | ||
| RowDefinition(name=capture.get()), | ||
| ], | ||
| col_max=COL_SIZE_MODIFIER, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def format_acronyms(formatter: PackageCommandHelpTextFormatter): | ||
| with formatter.indented_section(name="Acronyms", extra_indents=1): | ||
| formatter.write_rd( | ||
| [ | ||
| RowDefinition( | ||
| text="\n", | ||
| ), | ||
| RowDefinition( | ||
| name="S3", | ||
| text="Simple Storage Service", | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| RowDefinition( | ||
| name="ECR", | ||
| text="Elastic Container Registry", | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| RowDefinition( | ||
| name="KMS", | ||
| text="Key Management Service", | ||
| extra_row_modifiers=[ShowcaseRowModifier()], | ||
| ), | ||
| ], | ||
| col_max=COL_SIZE_MODIFIER, | ||
| ) | ||
|
|
||
| def format_options(self, ctx: Context, formatter: PackageCommandHelpTextFormatter) -> None: # type:ignore | ||
| # `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) | ||
| PackageCommand.format_examples(ctx, formatter) | ||
| PackageCommand.format_table(formatter) | ||
| PackageCommand.format_acronyms(formatter) | ||
|
|
||
| CoreCommand._format_options( | ||
| ctx=ctx, | ||
| params=self.get_params(ctx), | ||
| formatter=formatter, | ||
| formatting_options=OPTIONS_INFO, | ||
| write_rd_overrides={"col_max": COL_SIZE_MODIFIER}, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from samcli.cli.formatters import RootCommandHelpTextFormatter | ||
| from samcli.cli.row_modifiers import BaseLineRowModifier | ||
| from samcli.commands.deploy.core.options import ALL_OPTIONS | ||
|
|
||
|
|
||
| class PackageCommandHelpTextFormatter(RootCommandHelpTextFormatter): | ||
| # Picked an additive constant that gives an aesthetically pleasing look. | ||
| ADDITIVE_JUSTIFICATION = 15 | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| # 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()] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """ | ||
| Package 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 | ||
|
|
||
| # The ordering of the option lists matter, they are the order in which options will be displayed. | ||
|
|
||
| REQUIRED_OPTIONS: List[str] = ["s3_bucket", "resolve_s3"] | ||
|
|
||
| AWS_CREDENTIAL_OPTION_NAMES: List[str] = ["region", "profile"] | ||
|
|
||
| INFRASTRUCTURE_OPTION_NAMES: List[str] = [ | ||
| "s3_prefix", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can we make sure these are not duplicated between the command itself and its help text? We could at least think about adding generic unit tests which will do a cross check if certain parameter is defined in one place and missed in other.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "image_repository", | ||
| "image_repositories", | ||
| "kms_key_id", | ||
| "metadata", | ||
| ] | ||
|
|
||
| DEPLOYMENT_OPTIONS: List[str] = [ | ||
| "force_upload", | ||
| ] | ||
|
|
||
| CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] | ||
|
|
||
| ADDITIONAL_OPTIONS: List[str] = [ | ||
| "no_progressbar", | ||
| "signing_profiles", | ||
| "template_file", | ||
| "output_template_file", | ||
| "use_json", | ||
| ] | ||
|
|
||
| ALL_OPTIONS: List[str] = ( | ||
| REQUIRED_OPTIONS | ||
| + AWS_CREDENTIAL_OPTION_NAMES | ||
| + INFRASTRUCTURE_OPTION_NAMES | ||
| + DEPLOYMENT_OPTIONS | ||
| + CONFIGURATION_OPTION_NAMES | ||
| + ADDITIONAL_OPTIONS | ||
| + ALL_COMMON_OPTIONS | ||
| ) | ||
|
|
||
| OPTIONS_INFO: Dict[str, Dict] = { | ||
| "Required Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(REQUIRED_OPTIONS)}}, | ||
| "AWS Credential Options": { | ||
| "option_names": {opt: {"rank": idx} for idx, opt in enumerate(AWS_CREDENTIAL_OPTION_NAMES)} | ||
| }, | ||
| "Infrastructure Options": { | ||
| "option_names": {opt: {"rank": idx} for idx, opt in enumerate(INFRASTRUCTURE_OPTION_NAMES)} | ||
| }, | ||
| "Package Management Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(DEPLOYMENT_OPTIONS)}}, | ||
| "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. " | ||
| ), | ||
| ], | ||
| }, | ||
| "Additional Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(ADDITIONAL_OPTIONS)}}, | ||
| } | ||
| add_common_options_info(OPTIONS_INFO) | ||
Uh oh!
There was an error while loading. Please reload this page.