diff --git a/src/cfnlint/config.py b/src/cfnlint/config.py index e4a8db52e3..e903de3337 100644 --- a/src/cfnlint/config.py +++ b/src/cfnlint/config.py @@ -618,12 +618,14 @@ class ConfigMixIn(TemplateArgs, CliArgs, ConfigFileArgs): def __init__(self, cli_args: list[str] | None = None, **kwargs: Unpack[ManualArgs]): self._manual_args = kwargs or ManualArgs() + self._templates_to_process = False CliArgs.__init__(self, cli_args) # configure debug as soon as we can TemplateArgs.__init__(self, {}) ConfigFileArgs.__init__( self, config_file=self._get_argument_value("config_file", False, False) ) + self.templates def __repr__(self): return format_json_string( @@ -726,23 +728,46 @@ def format(self): @property def templates(self): - templates_args = self._get_argument_value("templates", False, True) - template_alt_args = self._get_argument_value("template_alt", False, False) - if template_alt_args: - filenames = template_alt_args - elif templates_args: - filenames = templates_args + """ + + Returns a list of Cloudformation templates to lint. + + Order of precedence: + - Filenames provided via `-t` CLI + - Filenames specified in the config file. + - Arguments provided via `cfn-lint` CLI. + """ + + all_filenames = [] + + cli_alt_args = self._get_argument_value("template_alt", False, False) + file_args = self._get_argument_value("templates", False, True) + cli_args = self._get_argument_value("templates", False, False) + + if cli_alt_args: + filenames = cli_alt_args + elif file_args: + filenames = file_args + elif cli_args: + filenames = cli_args else: + # No filenames found, could be piped in or be using the api. return None - # if only one is specified convert it to array + # If we're still haven't returned, we've got templates to lint - build up list of templates to lint. + self.templates_to_process = True + if isinstance(filenames, str): filenames = [filenames] ignore_templates = self._ignore_templates() - all_filenames = self._glob_filenames(filenames) + all_filenames.extend(self._glob_filenames(filenames)) - return [i for i in all_filenames if i not in ignore_templates] + found_files = [i for i in all_filenames if i not in ignore_templates] + LOGGER.debug( + f"List of Cloudformation Templates to lint: {found_files} from {filenames}" + ) + return found_files def _ignore_templates(self): ignore_template_args = self._get_argument_value("ignore_templates", False, True) @@ -768,7 +793,7 @@ def _glob_filenames(self, filenames: Sequence[str]) -> list[str]: if isinstance(add_filenames, list): all_filenames.extend(add_filenames) else: - all_filenames.append(filename) + LOGGER.error(f"{filename} could not be processed by glob.glob") return sorted(list(map(str, map(Path, all_filenames)))) @@ -836,7 +861,9 @@ def force(self): return self._get_argument_value("force", False, False) @property - def get_input_templates(self): - templates = self._get_argument_value("templates", False, True) - templates.extend(self._get_argument_value("template_alt", False, True)) - return templates + def templates_to_process(self): + return self._templates_to_process + + @templates_to_process.setter + def templates_to_process(self, value: bool): + self._templates_to_process = value diff --git a/src/cfnlint/runner.py b/src/cfnlint/runner.py index 0f270348a4..9e8571cb69 100644 --- a/src/cfnlint/runner.py +++ b/src/cfnlint/runner.py @@ -391,7 +391,7 @@ def run(self) -> Iterator[Match]: Raises: None: This function does not raise any exceptions. """ - if not sys.stdin.isatty() and not self.config.templates: + if not sys.stdin.isatty() and not self.config.templates_to_process: yield from self._validate_filenames([None]) return @@ -433,15 +433,9 @@ def cli(self) -> None: print(self.rules) sys.exit(0) - if not self.config.templates: - self.config.parser.print_help() - - # If templates were specified, but didn't create any file matches, print out message and exit - template = self.config.get_input_templates - if template: - print(f"\nNo matching files found for template arguments: {template}") - return + if not self.config.templates_to_process: if sys.stdin.isatty(): + self.config.parser.print_help() sys.exit(1) try: