Skip to content

Commit 1ed62bd

Browse files
authored
Add ability to only warn about some rules (#989)
Adds a `warn_list` option were user can add rules that should not affect the outcome of the linting, while they will still be identified. This softer option makes linter easier to adopt while still reminding users about issues they should seek to address.
1 parent 5916be8 commit 1ed62bd

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/ansiblelint/__main__.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,31 @@ def choose_formatter_factory(
7979
return r
8080

8181

82-
def hint_about_skips(matches: List["MatchError"]):
83-
"""Display information about how to skip found rules."""
82+
def report_outcome(matches: List["MatchError"], options) -> int:
83+
"""Display information about how to skip found rules.
84+
85+
Returns exit code, 2 if erros were found, 0 when only warnings were found.
86+
"""
87+
failure = False
8488
msg = """\
8589
You can skip specific rules by adding them to the skip_list section of your configuration file:
8690
```yaml
8791
# .ansible-lint
88-
skip_list:
92+
warn_list: # or 'skip_list' to silence them completely
8993
"""
9094
matched_rules = {match.rule.id: match.rule.shortdesc for match in matches}
9195
for id in sorted(matched_rules.keys()):
92-
msg += f" - '{id}' # {matched_rules[id]}'\n"
96+
if id not in options.warn_list:
97+
msg += f" - '{id}' # {matched_rules[id]}'\n"
98+
failure = True
9399
msg += "```"
94-
console.print(Markdown(msg))
100+
101+
if failure:
102+
if not options.quiet and not options.parseable:
103+
console.print(Markdown(msg))
104+
return 2
105+
else:
106+
return 0
95107

96108

97109
def main() -> int:
@@ -154,9 +166,7 @@ def main() -> int:
154166
print(formatter.format(match))
155167

156168
if matches:
157-
if not options.quiet and not options.parseable:
158-
hint_about_skips(matches)
159-
return 2
169+
return report_outcome(matches, options=options)
160170
else:
161171
return 0
162172

lib/ansiblelint/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def get_cli_parser() -> argparse.ArgumentParser:
134134
parser.add_argument('-x', dest='skip_list', default=[], action='append',
135135
help="only check rules whose id/tags do not "
136136
"match these values")
137+
parser.add_argument('-w', dest='warn_list', default=[], nargs="+",
138+
help="only warn about these rules")
137139
parser.add_argument('--nocolor', dest='colored',
138140
default=hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(),
139141
action='store_false',
@@ -173,6 +175,7 @@ def merge_config(file_config, cli_config) -> NamedTuple:
173175
'rulesdir',
174176
'skip_list',
175177
'tags',
178+
'warn_list',
176179
)
177180

178181
if not file_config:

0 commit comments

Comments
 (0)