Skip to content

Commit

Permalink
Allow for debug in .cfnlintrc file (#3898)
Browse files Browse the repository at this point in the history
* Allow for debug in .cfnlintrc file
  • Loading branch information
kddejong authored Jan 7, 2025
1 parent e087f10 commit a793af5
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 67 deletions.
7 changes: 3 additions & 4 deletions src/cfnlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, items)
except Exception: # pylint: disable=W0703
parser.print_help()
parser.exit()
parser.exit(1)


class CliArgs:
Expand All @@ -331,7 +331,7 @@ class ArgumentParser(argparse.ArgumentParser):

def error(self, message):
self.print_help(sys.stderr)
self.exit(32, f"{self.prog}: error: {message}\n")
self.exit(1, f"{self.prog}: error: {message}\n")

class ExtendAction(argparse.Action):
"""Support argument types that are lists and can
Expand Down Expand Up @@ -620,7 +620,6 @@ class ManualArgs(TypedDict, total=False):

# pylint: disable=too-many-public-methods
class ConfigMixIn(TemplateArgs, CliArgs, ConfigFileArgs):
"""Mixin for the Configs"""

def __init__(self, cli_args: list[str] | None = None, **kwargs: Unpack[ManualArgs]):
self._manual_args = kwargs or ManualArgs()
Expand Down Expand Up @@ -721,7 +720,7 @@ def ignore_bad_template(self):

@property
def debug(self):
return self._get_argument_value("debug", False, False)
return self._get_argument_value("debug", False, True)

@property
def info(self):
Expand Down
3 changes: 2 additions & 1 deletion src/cfnlint/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from typing import Sequence

from cfnlint.config import _DEFAULT_RULESDIR, ConfigMixIn, ManualArgs
from cfnlint.exceptions import UnexpectedRuleException
from cfnlint.match import Match
from cfnlint.rules import RulesCollection
from cfnlint.runner import TemplateRunner, UnexpectedRuleException
from cfnlint.runner import TemplateRunner


def get_rules(
Expand Down
4 changes: 4 additions & 0 deletions src/cfnlint/data/CfnLintCli/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"description": "custom rule file to use",
"type": "string"
},
"debug": {
"description": "Debug mode",
"type": "boolean"
},
"ignore_bad_template": {
"description": "Ignore bad templates",
"type": "boolean"
Expand Down
56 changes: 46 additions & 10 deletions src/cfnlint/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
class CfnLintError(Exception):
class CfnLintExitException(Exception):
"""
The base exception class for cfn-lint exceptions.
:ivar msg: The descriptive message associated with the error.
An exception that is raised to indicate that the CloudFormation linter should exit.
This exception is used to signal that the linter should exit
with a specific exit code, typically indicating the severity
of the issues found in the CloudFormation template.
Attributes:
exit_code (int): The exit code to be used when the linter exits.
Methods:
__init__(self, exit_code: int) -> None:
Initialize a new CfnLintExitException instance with the specified exit code.
"""

fmt = "An unspecified error occurred"
def __init__(self, msg=None, exit_code=1):
"""
Initialize a new CfnLintExitException instance with the specified exit code.
Args:
exit_code (int): The exit code to be used when the linter exits.
"""
if msg is None:
msg = f"process failed with exit code {exit_code}"
super().__init__(msg)
self.exit_code = exit_code


class InvalidRegionException(CfnLintExitException):
"""
An exception that is raised when an invalid AWS region is encountered.
def __init__(self, **kwargs):
msg = self.fmt.format(**kwargs)
Exception.__init__(self, msg)
self.kwargs = kwargs
This exception is raised when the CloudFormation linter encounters a resource
or parameter that references an AWS region that is not valid or supported.
"""


class UnexpectedRuleException(CfnLintExitException):
"""
An exception that is raised when an unexpected error occurs while loading rules.
This exception is raised when the CloudFormation linter encounters an error
while attempting to load custom rules or rules from a specified directory or
module. This could be due to a variety of reasons, such as a missing file,
a syntax error in the rule code, or an issue with the rule implementation.
"""


class DuplicateRuleError(CfnLintError):
class DuplicateRuleError(CfnLintExitException):
"""
The data associated with a particular path could not be loaded.
:ivar data_path: The data path that the user attempted to load.
"""

fmt = "Rule already included: {rule_id}"
def __init__(self, rule_id: str):
super().__init__(f"Rule already included: {rule_id}")
60 changes: 10 additions & 50 deletions src/cfnlint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import cfnlint.maintenance
from cfnlint.config import ConfigMixIn, configure_logging
from cfnlint.decode.decode import decode
from cfnlint.exceptions import (
CfnLintExitException,
InvalidRegionException,
UnexpectedRuleException,
)
from cfnlint.helpers import REGIONS
from cfnlint.rules import Match, Rules
from cfnlint.rules.errors import ConfigError, ParseError, TransformError
Expand Down Expand Up @@ -452,55 +457,10 @@ def cli(self) -> None:


def main() -> None:
config = ConfigMixIn(sys.argv[1:])
try:
config = ConfigMixIn(sys.argv[1:])
except Exception as e:
print(e)
sys.exit(1)
runner = Runner(config)
runner.cli()


class CfnLintExitException(Exception):
"""
An exception that is raised to indicate that the CloudFormation linter should exit.
This exception is used to signal that the linter should exit
with a specific exit code, typically indicating the severity
of the issues found in the CloudFormation template.
Attributes:
exit_code (int): The exit code to be used when the linter exits.
Methods:
__init__(self, exit_code: int) -> None:
Initialize a new CfnLintExitException instance with the specified exit code.
"""

def __init__(self, msg=None, exit_code=1):
"""
Initialize a new CfnLintExitException instance with the specified exit code.
Args:
exit_code (int): The exit code to be used when the linter exits.
"""
if msg is None:
msg = f"process failed with exit code {exit_code}"
super().__init__(msg)
self.exit_code = exit_code


class InvalidRegionException(CfnLintExitException):
"""
An exception that is raised when an invalid AWS region is encountered.
This exception is raised when the CloudFormation linter encounters a resource
or parameter that references an AWS region that is not valid or supported.
"""


class UnexpectedRuleException(CfnLintExitException):
"""
An exception that is raised when an unexpected error occurs while loading rules.
This exception is raised when the CloudFormation linter encounters an error
while attempting to load custom rules or rules from a specified directory or
module. This could be due to a variety of reasons, such as a missing file,
a syntax error in the rule code, or an issue with the rule implementation.
"""
2 changes: 1 addition & 1 deletion test/unit/module/config/test_config_file_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from test.testlib.testcase import BaseTestCase
from unittest.mock import patch

import cfnlint.config # pylint: disable=E0401
import cfnlint.config
from cfnlint.jsonschema import ValidationError

LOGGER = logging.getLogger("cfnlint")
Expand Down
3 changes: 2 additions & 1 deletion test/unit/module/runner/test_rule_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from test.testlib.testcase import BaseTestCase

from cfnlint.config import ConfigMixIn
from cfnlint.runner import Runner, UnexpectedRuleException
from cfnlint.exceptions import UnexpectedRuleException
from cfnlint.runner import Runner


class TestGetRules(BaseTestCase):
Expand Down

0 comments on commit a793af5

Please sign in to comment.