Skip to content
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

feat: Add --save-params option #5808

Merged
merged 12 commits into from
Sep 14, 2023
75 changes: 75 additions & 0 deletions samcli/cli/cli_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,81 @@ def get_ctx_defaults(
return provider(config_file, config_env_name, get_cmd_names(cmd_name, ctx))


def save_command_line_args_to_config(
ctx: click.Context, cmd_names: List[str], config_env_name: str, config_file: SamConfig
):
"""Save the provided command line arguments to the provided config file.

Parameters
----------
ctx: click.Context
Click context of the current session.
cmd_names: List[str]
List of representing the entire command. Ex: ["local", "generate-event", "s3", "put"]
config_env_name: str
Name of the config environment the command is being executed under. It will also serve as the environment that
the parameters are saved under.
config_file: SamConfig
Object representing the SamConfig file being read for this execution. It will also be the file to which the
parameters will be written.
"""
if not ctx.params.get("save_params", False):
return # only save params if flag is set

params_to_exclude = [
"save_params", # don't save the provided save-params
"config_file", # don't save config specs to prevent confusion
"config_env",
]

for param_name, param_source in ctx._parameter_source.items():
if param_name in params_to_exclude:
continue # don't save certain params
if param_source != ParameterSource.COMMANDLINE:
continue # only parse params retrieved through CLI

# if param was passed via command line, save to file
param_value = ctx.params.get(param_name, None)
if param_value is None:
Leo10Gama marked this conversation as resolved.
Show resolved Hide resolved
continue # no value given, ignore
config_file.put(cmd_names, "parameters", param_name, param_value, config_env_name)
Leo10Gama marked this conversation as resolved.
Show resolved Hide resolved

config_file.flush()


def save_params(func):
"""Decorator for saving provided parameters to a config file, if the flag is set."""

def wrapper(*args, **kwargs):
ctx = click.get_current_context()
cmd_names = get_cmd_names(ctx.info_name, ctx)

save_command_line_args_to_config(
ctx=ctx,
cmd_names=cmd_names,
config_env_name=ctx.params.get("config_env", None),
config_file=SamConfig(getattr(ctx, "samconfig_dir", os.getcwd()), ctx.params.get("config_file", None)),
)

return func(*args, **kwargs)

return wrapper


def save_params_option(func):
"""Composite decorator to add --save-params flag to a command.

When used, this command should be placed as the LAST of the click option/argument decorators to preserve the flow
of execution. The decorator itself will add the --save-params option, and, if provided, save the provided commands
from the terminal to the config file.
"""
return click.option(
"--save-params",
is_flag=True,
help="Save the terminal-provided parameters to the configuration file.",
Leo10Gama marked this conversation as resolved.
Show resolved Hide resolved
)(save_params(func))


def configuration_option(*param_decls, **attrs):
# pylint does not understand the docstring with the presence of **attrs
# pylint: disable=missing-param-doc,differing-param-doc
Expand Down
2 changes: 2 additions & 0 deletions samcli/cli/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

ALL_COMMON_OPTIONS: List[str] = BETA_OPTIONS + OTHER_OPTIONS

SAVE_PARAMS_OPTIONS: List[str] = ["save_params"]

OPTIONS_INFO: Dict[str, Dict] = {
"Beta Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(BETA_OPTIONS)}},
"Other Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(OTHER_OPTIONS)}},
Expand Down
4 changes: 3 additions & 1 deletion samcli/commands/build/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options, print_cmdline_args
from samcli.commands.build.core.command import BuildCommand
from samcli.lib.telemetry.metric import track_command
from samcli.cli.cli_config_file import configuration_option, ConfigProvider
from samcli.cli.cli_config_file import configuration_option, ConfigProvider, save_params_option
from samcli.lib.utils.version_checker import check_newer_version
from samcli.commands.build.click_container import ContainerOptions
from samcli.commands.build.utils import MountMode
Expand Down Expand Up @@ -130,6 +130,7 @@
@cli_framework_options
@aws_creds_options
@click.argument("resource_logical_id", required=False)
@save_params_option
@pass_context
@track_command
@check_newer_version
Expand All @@ -155,6 +156,7 @@ def cli(
parameter_overrides: dict,
config_file: str,
config_env: str,
save_params: bool,
hook_name: Optional[str],
skip_prepare_infra: bool,
mount_with,
Expand Down
4 changes: 2 additions & 2 deletions samcli/commands/build/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Dict, List

from samcli.cli.row_modifiers import RowDefinition
from samcli.cli.core.options import ALL_COMMON_OPTIONS, add_common_options_info
from samcli.cli.core.options import ALL_COMMON_OPTIONS, SAVE_PARAMS_OPTIONS, add_common_options_info

# NOTE(sriram-mv): The ordering of the option lists matter, they are the order
# in which options will be displayed.
Expand All @@ -23,7 +23,7 @@
"docker_network",
]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"]
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS

EXTENSION_OPTIONS: List[str] = ["hook_name", "skip_prepare_infra"]

Expand Down
13 changes: 12 additions & 1 deletion samcli/commands/delete/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import click

from samcli.cli.cli_config_file import save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.command_exception_handler import command_exception_handler
from samcli.lib.telemetry.metric import track_command
Expand Down Expand Up @@ -82,12 +83,22 @@
)
@aws_creds_options
@common_options
@save_params_option
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
@command_exception_handler
def cli(ctx, stack_name: str, config_file: str, config_env: str, no_prompts: bool, s3_bucket: str, s3_prefix: str):
def cli(
ctx,
stack_name: str,
config_file: str,
config_env: str,
no_prompts: bool,
s3_bucket: str,
s3_prefix: str,
save_params: bool,
):
"""
`sam delete` command entry point
"""
Expand Down
4 changes: 3 additions & 1 deletion samcli/commands/deploy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.cdk_support_decorators import unsupported_command_cdk
from samcli.commands._utils.click_mutex import ClickMutex
Expand Down Expand Up @@ -154,6 +154,7 @@
@capabilities_option
@aws_creds_options
@common_options
@save_params_option
@image_repository_validation()
@pass_context
@track_command
Expand Down Expand Up @@ -186,6 +187,7 @@ def cli(
signing_profiles,
resolve_s3,
resolve_image_repos,
save_params,
config_file,
config_env,
disable_rollback,
Expand Down
4 changes: 2 additions & 2 deletions samcli/commands/deploy/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import Dict, List

from samcli.cli.core.options import ALL_COMMON_OPTIONS, add_common_options_info
from samcli.cli.core.options import ALL_COMMON_OPTIONS, SAVE_PARAMS_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.
Expand Down Expand Up @@ -38,7 +38,7 @@
"force_upload",
]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"]
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS

ADDITIONAL_OPTIONS: List[str] = [
"no_progressbar",
Expand Down
4 changes: 3 additions & 1 deletion samcli/commands/init/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import common_options, pass_context, print_cmdline_args
from samcli.commands._utils.click_mutex import ClickMutex
from samcli.commands.init.core.command import InitCommand
Expand Down Expand Up @@ -227,6 +227,7 @@ def wrapped(*args, **kwargs):
help="Enable CloudWatch Application Insights monitoring for application.",
)
@common_options
@save_params_option
@non_interactive_validation
@pass_context
@track_command
Expand All @@ -248,6 +249,7 @@ def cli(
extra_context,
tracing,
application_insights,
save_params,
config_file,
config_env,
):
Expand Down
4 changes: 2 additions & 2 deletions samcli/commands/init/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import Dict, List

from samcli.cli.core.options import ALL_COMMON_OPTIONS, add_common_options_info
from samcli.cli.core.options import ALL_COMMON_OPTIONS, SAVE_PARAMS_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.
Expand All @@ -23,7 +23,7 @@
# Can be used instead of the options in the first list
NON_INTERACTIVE_OPTIONS: List[str] = ["no_interactive", "no_input", "extra_context"]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"]
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS

ADDITIONAL_OPTIONS: List[str] = [
"tracing",
Expand Down
5 changes: 3 additions & 2 deletions samcli/commands/list/endpoints/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.command_exception_handler import command_exception_handler
from samcli.commands._utils.options import parameter_override_option, template_option_without_build
Expand All @@ -28,12 +28,13 @@
@template_option_without_build
@aws_creds_options
@common_options
@save_params_option
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
@command_exception_handler
def cli(self, parameter_overrides, stack_name, output, template_file, config_file, config_env):
def cli(self, parameter_overrides, stack_name, output, template_file, save_params, config_file, config_env):
"""
`sam list endpoints` command entry point
"""
Expand Down
5 changes: 3 additions & 2 deletions samcli/commands/list/resources/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.command_exception_handler import command_exception_handler
from samcli.commands._utils.options import parameter_override_option, template_option_without_build
Expand All @@ -27,12 +27,13 @@
@template_option_without_build
@aws_creds_options
@common_options
@save_params_option
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
@command_exception_handler
def cli(self, parameter_overrides, stack_name, output, template_file, config_file, config_env):
def cli(self, parameter_overrides, stack_name, output, template_file, save_params, config_file, config_env):
"""
`sam list resources` command entry point
"""
Expand Down
5 changes: 3 additions & 2 deletions samcli/commands/list/stack_outputs/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args
from samcli.commands._utils.command_exception_handler import command_exception_handler
from samcli.commands.list.cli_common.options import output_option
Expand All @@ -27,12 +27,13 @@
@output_option
@aws_creds_options
@common_options
@save_params_option
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
@command_exception_handler
def cli(self, stack_name, output, config_file, config_env):
def cli(self, stack_name, output, save_params, config_file, config_env):
"""
`sam list stack-outputs` command entry point
"""
Expand Down
4 changes: 3 additions & 1 deletion samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, pass_context, print_cmdline_args
from samcli.cli.main import common_options as cli_framework_options
from samcli.commands._utils.experimental import ExperimentalFlag, is_experimental_enabled
Expand Down Expand Up @@ -62,6 +62,7 @@
@cli_framework_options
@aws_creds_options
@click.argument("function_logical_id", required=False)
@save_params_option
@pass_context
@track_command # pylint: disable=R0914
@check_newer_version
Expand All @@ -85,6 +86,7 @@ def cli(
force_image_build,
shutdown,
parameter_overrides,
save_params,
config_file,
config_env,
container_host,
Expand Down
4 changes: 2 additions & 2 deletions samcli/commands/local/invoke/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import Dict, List

from samcli.cli.core.options import ALL_COMMON_OPTIONS, add_common_options_info
from samcli.cli.core.options import ALL_COMMON_OPTIONS, SAVE_PARAMS_OPTIONS, add_common_options_info
from samcli.cli.row_modifiers import RowDefinition

# NOTE(sriram-mv): The ordering of the option lists matter, they are the order
Expand Down Expand Up @@ -35,7 +35,7 @@
"invoke_image",
]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"]
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS

EXTENSION_OPTIONS: List[str] = ["hook_name", "skip_prepare_infra"]

Expand Down
4 changes: 3 additions & 1 deletion samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import click

from samcli.cli.cli_config_file import ConfigProvider, configuration_option
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, save_params_option
from samcli.cli.main import aws_creds_options, pass_context, print_cmdline_args
from samcli.cli.main import common_options as cli_framework_options
from samcli.commands._utils.experimental import ExperimentalFlag, is_experimental_enabled
Expand Down Expand Up @@ -77,6 +77,7 @@
@local_common_options
@cli_framework_options
@aws_creds_options # pylint: disable=R0914
@save_params_option
@pass_context
@track_command
@check_newer_version
Expand All @@ -101,6 +102,7 @@ def cli(
skip_pull_image,
force_image_build,
parameter_overrides,
save_params,
config_file,
config_env,
warm_containers,
Expand Down
Loading
Loading