Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samcli/cli/cli_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from click.core import ParameterSource

from samcli.cli.context import Context, get_cmd_names
from samcli.commands._utils.custom_options.structured_output_option import StructuredOutputOption
from samcli.commands.exceptions import ConfigException
from samcli.lib.config.samconfig import DEFAULT_CONFIG_FILE_NAME, DEFAULT_ENV, SamConfig
from samcli.lib.utils.defaults import get_default_aws_region
Expand Down Expand Up @@ -308,6 +309,14 @@ def save_command_line_args_to_config(
"config_env",
]

# The shared structured output flag describes how a single run reports rather than what to
# build, so persisting it would change the output format of later runs, and for sam init it
# would make the interactive flow unreachable. Matched by option type, because sam list and
# sam remote invoke define an unrelated --output that is a display preference worth saving.
params_to_exclude += [
param.name for param in ctx.command.params if isinstance(param, StructuredOutputOption) and param.name
]

saved_params = {}
for param_name, param_source in ctx._parameter_source.items():
if param_name in params_to_exclude:
Expand Down
4 changes: 4 additions & 0 deletions samcli/commands/_utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
DEFAULT_BUILD_DIR_WITH_AUTO_DEPENDENCY_LAYER = os.path.join(".aws-sam", "auto-dependency-layer")
DEFAULT_CACHE_DIR = os.path.join(".aws-sam", "cache")
DEFAULT_BUILT_TEMPLATE_PATH = os.path.join(".aws-sam", "build", "template.yaml")

# Template file names SAM CLI recognises, in resolution order. Order matters, so that a template
# path reported by one command is the one another command would resolve to.
SAM_TEMPLATE_FILE_NAMES = ["template.yaml", "template.yml", "template.json"]
Comment thread
roger-zhangg marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Custom click option for the shared structured output flag
"""

import click


class StructuredOutputOption(click.Option):
"""Marks the shared --output option that selects structured (JSON) output.

Exists so the option can be recognised by type rather than by name. Other commands, such as
sam list and sam remote invoke, have an unrelated --output that selects a display format and
is worth saving to a config file, while this one describes how a single run reports.
"""
6 changes: 5 additions & 1 deletion samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
DEFAULT_BUILT_TEMPLATE_PATH,
DEFAULT_CACHE_DIR,
DEFAULT_STACK_NAME,
SAM_TEMPLATE_FILE_NAMES,
)
from samcli.commands._utils.custom_options.hook_name_option import HookNameOption
from samcli.commands._utils.custom_options.option_nargs import OptionNargs
from samcli.commands._utils.custom_options.replace_help_option import ReplaceHelpSummaryOption
from samcli.commands._utils.custom_options.structured_output_option import StructuredOutputOption
from samcli.commands._utils.parameterized_option import parameterized_option
from samcli.commands._utils.template import TemplateNotFoundException, get_template_artifacts_format, get_template_data
from samcli.lib.hook.hook_wrapper import get_available_hook_packages_ids
Expand Down Expand Up @@ -65,7 +67,7 @@ def get_or_default_template_file_name(ctx, param, provided_value, include_build)

original_template_path = os.path.abspath(provided_value)

search_paths = ["template.yaml", "template.yml", "template.json"]
search_paths = list(SAM_TEMPLATE_FILE_NAMES)

if include_build:
search_paths.insert(0, DEFAULT_BUILT_TEMPLATE_PATH)
Expand Down Expand Up @@ -460,6 +462,8 @@ def structured_output_click_option():
"Supported formats: text (default), json.",
# Derive choices from OutputOption so the accepted CLI values cannot drift from the enum.
type=click.Choice([option.value for option in OutputOption], case_sensitive=False),
# Lets --save-params recognise this option by type, so it is not persisted.
cls=StructuredOutputOption,
)


Expand Down
Loading
Loading