in samcli/commands/_utils/options.py [0:0]
def get_or_default_template_file_name(ctx, param, provided_value, include_build):
"""
Default value for the template file name option is more complex than what Click can handle.
This method either returns user provided file name or one of the two default options (template.yaml/template.yml)
depending on the file that exists
:param ctx: Click Context
:param param: Param name
:param provided_value: Value provided by Click. It could either be the default value or provided by user.
:param include_build: A boolean to set whether to search build template or not.
:return: Actual value to be used in the CLI
"""
original_template_path = os.path.abspath(provided_value)
search_paths = ["template.yaml", "template.yml", "template.json"]
if include_build:
search_paths.insert(0, DEFAULT_BUILT_TEMPLATE_PATH)
if provided_value == _TEMPLATE_OPTION_DEFAULT_VALUE:
# "--template" is an alias of "--template-file", however, only the first option name "--template-file" in
# ctx.default_map is used as default value of provided value. Here we add "--template"'s value as second
# default value in this option, so that the command line parameters from config file can load it.
if ctx and ctx.default_map.get("template", None):
provided_value = ctx.default_map.get("template")
else:
# Default value was used. Value can either be template.yaml or template.yml.
# Decide based on which file exists .yml is the default, even if it does not exist.
provided_value = "template.yml"
for option in search_paths:
if os.path.exists(option):
provided_value = option
break
result = os.path.abspath(provided_value)
if ctx:
# sam configuration file should always be relative to the supplied original template and should not to be set
# to be .aws-sam/build/
setattr(ctx, "samconfig_dir", os.path.dirname(original_template_path))
try:
# FIX-ME: figure out a way to insert this directly to sam-cli context and not use click context.
template_data = get_template_data(result)
setattr(ctx, "template_dict", template_data)
except TemplateNotFoundException:
# Ignoring because there are certain cases where template file will not be available, eg: --help
pass
LOG.debug("Using SAM Template at %s", result)
return result