Skip to content

Commit

Permalink
Change the way rules are listed from command line (#2940
Browse files Browse the repository at this point in the history
- rename plain formatting to brief
- make brief implicit formatting for rule listing
- rename rich formatting to full
  • Loading branch information
ssbarnea authored Feb 1, 2023
1 parent f777905 commit 386d63d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ PYTHONPYCACHEPREFIX
REQPASS
RULEDIRS
RUNLEVEL
Renderable
Representer
SRCROOT
Sbarnea
Expand Down
8 changes: 5 additions & 3 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ def _do_list(rules: RulesCollection) -> int:
if options.list_rules:

_rule_format_map: dict[str, Callable[..., Any]] = {
"plain": rules_as_str,
"rich": rules_as_rich,
"brief": rules_as_str,
"full": rules_as_rich,
"md": rules_as_md,
"docs": rules_as_docs,
}

console.print(_rule_format_map[options.format](rules), highlight=False)
console.print(
_rule_format_map.get(options.format, rules_as_str)(rules), highlight=False
)
return 0

if options.list_tags:
Expand Down
19 changes: 13 additions & 6 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_cli_parser() -> argparse.ArgumentParser:
default=False,
action="store_true",
help="List all the rules. For listing rules only the following formats "
"for argument -f are supported: {plain, rich, md}",
"for argument -f are supported: {brief, full, md} with 'brief' as default.",
)
listing_group.add_argument(
"-T",
Expand All @@ -238,10 +238,11 @@ def get_cli_parser() -> argparse.ArgumentParser:
"-f",
"--format",
dest="format",
default="rich",
default=None,
choices=[
"rich",
"plain",
"brief",
# "plain",
"full",
"md",
"json",
"codeclimate",
Expand Down Expand Up @@ -526,10 +527,16 @@ def get_config(arguments: list[str]) -> Namespace:
options = parser.parse_args(arguments)

# docs is not document, being used for internal documentation building
if options.list_rules and options.format not in ["plain", "rich", "md", "docs"]:
if options.list_rules and options.format not in [
None,
"brief",
"full",
"md",
"docs",
]:
parser.error(
f"argument -f: invalid choice: '{options.format}'. "
f"In combination with argument -L only 'plain', "
f"In combination with argument -L only 'brief', "
f"'rich' or 'md' are supported with -f."
)

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
cwd=".",
display_relative_path=True,
exclude_paths=[],
format="rich",
format="brief",
lintables=[],
list_rules=False,
list_tags=False,
Expand Down
14 changes: 12 additions & 2 deletions src/ansiblelint/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Iterable

from rich import box
from rich.console import RenderableType

# Remove this compatibility try-catch block once we drop support for rich < 10.7.0
try:
Expand Down Expand Up @@ -70,9 +71,18 @@ def rules_as_docs(rules: RulesCollection) -> str:
return "All markdown files for rules were dumped!"


def rules_as_str(rules: RulesCollection) -> str:
def rules_as_str(rules: RulesCollection) -> RenderableType:
"""Return rules as string."""
return "\n".join([str(rule) for rule in rules.alphabetical()])
table = Table(show_header=False, header_style="title", box=box.SIMPLE)
for rule in rules.alphabetical():
if rule.tags:
tag = f"[dim] ({', '.join(rule.tags)})[/dim]"
else:
tag = ""
table.add_row(
f"[link={RULE_DOC_URL}{rule.id}/]{rule.id}[/link]", rule.shortdesc + tag
)
return table


def rules_as_md(rules: RulesCollection) -> str:
Expand Down
6 changes: 3 additions & 3 deletions test/test_list_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def test_list_rules_includes_opt_in_rules() -> None:
@pytest.mark.parametrize(
("result", "returncode", "format_string"),
(
(False, 0, "plain"),
(False, 0, "rich"),
(False, 0, "brief"),
(False, 0, "full"),
(False, 0, "md"),
(True, 2, "json"),
(True, 2, "codeclimate"),
Expand All @@ -35,7 +35,7 @@ def test_list_rules_includes_opt_in_rules() -> None:
),
ids=(
"plain",
"rich",
"full",
"md",
"json",
"codeclimate",
Expand Down
2 changes: 1 addition & 1 deletion test/test_rules_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_rich_rule_listing() -> None:
descriptions in the console output.
"""
rules_path = os.path.abspath("./test/rules/fixtures")
result = run_ansible_lint("-r", rules_path, "-f", "rich", "-L")
result = run_ansible_lint("-r", rules_path, "-f", "full", "-L")
assert result.returncode == 0

for rule in RulesCollection([rules_path]):
Expand Down

0 comments on commit 386d63d

Please sign in to comment.