Skip to content

Commit a793af5

Browse files
authored
Allow for debug in .cfnlintrc file (#3898)
* Allow for debug in .cfnlintrc file
1 parent e087f10 commit a793af5

File tree

7 files changed

+68
-67
lines changed

7 files changed

+68
-67
lines changed

src/cfnlint/config.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def __call__(self, parser, namespace, values, option_string=None):
313313
setattr(namespace, self.dest, items)
314314
except Exception: # pylint: disable=W0703
315315
parser.print_help()
316-
parser.exit()
316+
parser.exit(1)
317317

318318

319319
class CliArgs:
@@ -331,7 +331,7 @@ class ArgumentParser(argparse.ArgumentParser):
331331

332332
def error(self, message):
333333
self.print_help(sys.stderr)
334-
self.exit(32, f"{self.prog}: error: {message}\n")
334+
self.exit(1, f"{self.prog}: error: {message}\n")
335335

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

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

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

722721
@property
723722
def debug(self):
724-
return self._get_argument_value("debug", False, False)
723+
return self._get_argument_value("debug", False, True)
725724

726725
@property
727726
def info(self):

src/cfnlint/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from typing import Sequence
1010

1111
from cfnlint.config import _DEFAULT_RULESDIR, ConfigMixIn, ManualArgs
12+
from cfnlint.exceptions import UnexpectedRuleException
1213
from cfnlint.match import Match
1314
from cfnlint.rules import RulesCollection
14-
from cfnlint.runner import TemplateRunner, UnexpectedRuleException
15+
from cfnlint.runner import TemplateRunner
1516

1617

1718
def get_rules(

src/cfnlint/data/CfnLintCli/config/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
"description": "custom rule file to use",
5959
"type": "string"
6060
},
61+
"debug": {
62+
"description": "Debug mode",
63+
"type": "boolean"
64+
},
6165
"ignore_bad_template": {
6266
"description": "Ignore bad templates",
6367
"type": "boolean"

src/cfnlint/exceptions.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
1-
class CfnLintError(Exception):
1+
class CfnLintExitException(Exception):
22
"""
3-
The base exception class for cfn-lint exceptions.
4-
:ivar msg: The descriptive message associated with the error.
3+
An exception that is raised to indicate that the CloudFormation linter should exit.
4+
5+
This exception is used to signal that the linter should exit
6+
with a specific exit code, typically indicating the severity
7+
of the issues found in the CloudFormation template.
8+
9+
Attributes:
10+
exit_code (int): The exit code to be used when the linter exits.
11+
12+
Methods:
13+
__init__(self, exit_code: int) -> None:
14+
Initialize a new CfnLintExitException instance with the specified exit code.
515
"""
616

7-
fmt = "An unspecified error occurred"
17+
def __init__(self, msg=None, exit_code=1):
18+
"""
19+
Initialize a new CfnLintExitException instance with the specified exit code.
20+
21+
Args:
22+
exit_code (int): The exit code to be used when the linter exits.
23+
"""
24+
if msg is None:
25+
msg = f"process failed with exit code {exit_code}"
26+
super().__init__(msg)
27+
self.exit_code = exit_code
28+
29+
30+
class InvalidRegionException(CfnLintExitException):
31+
"""
32+
An exception that is raised when an invalid AWS region is encountered.
833
9-
def __init__(self, **kwargs):
10-
msg = self.fmt.format(**kwargs)
11-
Exception.__init__(self, msg)
12-
self.kwargs = kwargs
34+
This exception is raised when the CloudFormation linter encounters a resource
35+
or parameter that references an AWS region that is not valid or supported.
36+
"""
37+
38+
39+
class UnexpectedRuleException(CfnLintExitException):
40+
"""
41+
An exception that is raised when an unexpected error occurs while loading rules.
42+
43+
This exception is raised when the CloudFormation linter encounters an error
44+
while attempting to load custom rules or rules from a specified directory or
45+
module. This could be due to a variety of reasons, such as a missing file,
46+
a syntax error in the rule code, or an issue with the rule implementation.
47+
"""
1348

1449

15-
class DuplicateRuleError(CfnLintError):
50+
class DuplicateRuleError(CfnLintExitException):
1651
"""
1752
The data associated with a particular path could not be loaded.
1853
:ivar data_path: The data path that the user attempted to load.
1954
"""
2055

21-
fmt = "Rule already included: {rule_id}"
56+
def __init__(self, rule_id: str):
57+
super().__init__(f"Rule already included: {rule_id}")

src/cfnlint/runner.py

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
import cfnlint.maintenance
1616
from cfnlint.config import ConfigMixIn, configure_logging
1717
from cfnlint.decode.decode import decode
18+
from cfnlint.exceptions import (
19+
CfnLintExitException,
20+
InvalidRegionException,
21+
UnexpectedRuleException,
22+
)
1823
from cfnlint.helpers import REGIONS
1924
from cfnlint.rules import Match, Rules
2025
from cfnlint.rules.errors import ConfigError, ParseError, TransformError
@@ -452,55 +457,10 @@ def cli(self) -> None:
452457

453458

454459
def main() -> None:
455-
config = ConfigMixIn(sys.argv[1:])
460+
try:
461+
config = ConfigMixIn(sys.argv[1:])
462+
except Exception as e:
463+
print(e)
464+
sys.exit(1)
456465
runner = Runner(config)
457466
runner.cli()
458-
459-
460-
class CfnLintExitException(Exception):
461-
"""
462-
An exception that is raised to indicate that the CloudFormation linter should exit.
463-
464-
This exception is used to signal that the linter should exit
465-
with a specific exit code, typically indicating the severity
466-
of the issues found in the CloudFormation template.
467-
468-
Attributes:
469-
exit_code (int): The exit code to be used when the linter exits.
470-
471-
Methods:
472-
__init__(self, exit_code: int) -> None:
473-
Initialize a new CfnLintExitException instance with the specified exit code.
474-
"""
475-
476-
def __init__(self, msg=None, exit_code=1):
477-
"""
478-
Initialize a new CfnLintExitException instance with the specified exit code.
479-
480-
Args:
481-
exit_code (int): The exit code to be used when the linter exits.
482-
"""
483-
if msg is None:
484-
msg = f"process failed with exit code {exit_code}"
485-
super().__init__(msg)
486-
self.exit_code = exit_code
487-
488-
489-
class InvalidRegionException(CfnLintExitException):
490-
"""
491-
An exception that is raised when an invalid AWS region is encountered.
492-
493-
This exception is raised when the CloudFormation linter encounters a resource
494-
or parameter that references an AWS region that is not valid or supported.
495-
"""
496-
497-
498-
class UnexpectedRuleException(CfnLintExitException):
499-
"""
500-
An exception that is raised when an unexpected error occurs while loading rules.
501-
502-
This exception is raised when the CloudFormation linter encounters an error
503-
while attempting to load custom rules or rules from a specified directory or
504-
module. This could be due to a variety of reasons, such as a missing file,
505-
a syntax error in the rule code, or an issue with the rule implementation.
506-
"""

test/unit/module/config/test_config_file_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from test.testlib.testcase import BaseTestCase
99
from unittest.mock import patch
1010

11-
import cfnlint.config # pylint: disable=E0401
11+
import cfnlint.config
1212
from cfnlint.jsonschema import ValidationError
1313

1414
LOGGER = logging.getLogger("cfnlint")

test/unit/module/runner/test_rule_configuration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from test.testlib.testcase import BaseTestCase
88

99
from cfnlint.config import ConfigMixIn
10-
from cfnlint.runner import Runner, UnexpectedRuleException
10+
from cfnlint.exceptions import UnexpectedRuleException
11+
from cfnlint.runner import Runner
1112

1213

1314
class TestGetRules(BaseTestCase):

0 commit comments

Comments
 (0)