Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple profile listing from profile selecting on CLI #2721

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ lineinfile
linkcheck
lintable
lintables
listrules
listtags
literalinclude
localectl
matchdir
Expand Down
4 changes: 2 additions & 2 deletions docs/using-profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ If a rule is in a profile, Ansible-lint applies that rule to the content.

After you install and configure `ansible-lint`, you can apply profiles as follows:

1. View available profiles with the `-P` flag.
1. View available profiles with the `--list-profiles` flag.

```bash
ansible-lint -P
ansible-lint --list-profiles
```

2. Specify a profile with the `--profile` parameter to lint your content with those rules, for example:
Expand Down
10 changes: 5 additions & 5 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _do_list(rules: RulesCollection) -> int:
rules_as_str,
)

if options.listrules:
if options.list_rules:

_rule_format_map: dict[str, Callable[..., Any]] = {
"plain": rules_as_str,
Expand All @@ -144,8 +144,8 @@ def _do_list(rules: RulesCollection) -> int:
console.print(_rule_format_map[options.format](rules), highlight=False)
return 0

if options.listtags:
console.print(render_yaml(rules.listtags()))
if options.list_tags:
console.print(render_yaml(rules.list_tags()))
return 0

# we should not get here!
Expand Down Expand Up @@ -202,13 +202,13 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901

rules = RulesCollection(options.rulesdirs, profile=options.profile)

if options.profile == []:
if options.list_profiles:
from ansiblelint.generate_docs import profiles_as_rich

console.print(profiles_as_rich())
return 0

if options.listrules or options.listtags:
if options.list_rules or options.list_tags:
return _do_list(rules)

app = get_app()
Expand Down
22 changes: 13 additions & 9 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,18 @@ def get_cli_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()

listing_group = parser.add_mutually_exclusive_group()
listing_group.add_argument(
"-P",
"--list-profiles",
dest="list_profiles",
default=False,
action="store_true",
help="List all profiles, no formatting options available.",
)
listing_group.add_argument(
"-L",
"--list-rules",
dest="listrules",
dest="list_rules",
default=False,
action="store_true",
help="List all the rules. For listing rules only the following formats "
Expand All @@ -229,7 +237,7 @@ def get_cli_parser() -> argparse.ArgumentParser:
listing_group.add_argument(
"-T",
"--list-tags",
dest="listtags",
dest="list_tags",
action="store_true",
help="List all the tags and the rules they cover. Increase the verbosity level "
"with `-v` to include 'opt-in' tag and its rules.",
Expand Down Expand Up @@ -260,16 +268,12 @@ def get_cli_parser() -> argparse.ArgumentParser:
help="quieter, reduce verbosity, can be specified twice.",
)
parser.add_argument(
"-P",
"--profile",
# * allow to distinguish between calling without -P, with -P and
# with -P=foo, while '?' does not. We do not support a real list.
nargs="*",
dest="profile",
default=None, # when called with -P but no arg will load as empty list []
default=None,
action="store",
choices=PROFILES.keys(),
help="Specify which rules profile to be used, or displays available profiles when no argument is given.",
help="Specify which rules profile to be used.",
)
parser.add_argument(
"-p",
Expand Down Expand Up @@ -525,7 +529,7 @@ def get_config(arguments: list[str]) -> Namespace:
options = parser.parse_args(arguments)

# docs is not document, being used for internal documentation building
if options.listrules and options.format not in ["plain", "rich", "md", "docs"]:
if options.list_rules and options.format not in ["plain", "rich", "md", "docs"]:
parser.error(
f"argument -f: invalid choice: '{options.format}'. "
f"In combination with argument -L only 'plain', "
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@
exclude_paths=[],
format="rich",
lintables=[],
listrules=False,
listtags=False,
list_rules=False,
list_tags=False,
write_list=[],
parseable=False,
quiet=False,
Expand Down
6 changes: 3 additions & 3 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ def register(self, obj: AnsibleLintRule, conditional: bool = False) -> None:
self.profile, # when profile is used we load all rules and filter later
"opt-in" not in obj.tags,
obj.id in self.options.enable_list,
self.options.listrules,
self.options.listtags,
self.options.list_rules,
self.options.list_tags,
]
):
self.rules.append(obj)
Expand Down Expand Up @@ -483,7 +483,7 @@ def __repr__(self) -> str:
[rule.verbose() for rule in sorted(self.rules, key=lambda x: x.id)]
)

def listtags(self) -> str:
def list_tags(self) -> str:
"""Return a string with all the tags in the RulesCollection."""
tag_desc = {
"command-shell": "Specific to use of command and shell modules",
Expand Down