Skip to content

Commit

Permalink
rework PR based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodingsysadmin committed Aug 21, 2024
1 parent 145f1bd commit 0467c14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
55 changes: 41 additions & 14 deletions src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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))))

Expand Down Expand Up @@ -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
12 changes: 3 additions & 9 deletions src/cfnlint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,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

Expand Down Expand Up @@ -430,15 +430,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:
Expand Down

0 comments on commit 0467c14

Please sign in to comment.