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

Add --write option for upcoming reformat/transform features #1926

Merged
merged 1 commit into from
Feb 18, 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
17 changes: 17 additions & 0 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
if TYPE_CHECKING:
# RulesCollection must be imported lazily or ansible gets imported too early.
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import LintResult


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,6 +138,19 @@ def _do_list(rules: "RulesCollection") -> int:
return 1


def _do_transform(result: "LintResult") -> None:
"""Create and run Transformer."""
# On purpose lazy-imports to avoid loading transforms unless requested
# pylint: disable=import-outside-toplevel
from ansiblelint.transformer import Transformer

# future: maybe pass options to Transformer
transformer = Transformer(result)

# this will mark any matches as fixed if the transforms repaired the issue
transformer.run()


def main(argv: Optional[List[str]] = None) -> int:
"""Linter CLI entry point."""
# alter PATH if needed (venv support)
Expand Down Expand Up @@ -174,6 +188,9 @@ def main(argv: Optional[List[str]] = None) -> int:

result = _get_matches(rules, options)

if options.write:
_do_transform(result)

mark_as_success = False
if result.matches and options.progressive:
_logger.info(
Expand Down
7 changes: 7 additions & 0 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ def get_cli_parser() -> argparse.ArgumentParser:
dest="use_default_rules",
help="Keep default rules when using -r",
)
parser.add_argument(
"--write",
dest="write",
action="store_true",
help="Reformat YAML files to standardize spacing, quotes, etc. "
"Future versions will expand this option so it fixes more issues.",
)
parser.add_argument(
"--show-relpath",
dest="display_relative_path",
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
lintables=[],
listrules=False,
listtags=False,
write=False,
parseable=False,
quiet=False,
rulesdirs=[],
Expand Down
28 changes: 28 additions & 0 deletions src/ansiblelint/transformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Transformer implementation."""
import logging
from typing import List, Set

from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.runner import LintResult

__all__ = ["Transformer"]

_logger = logging.getLogger(__name__)


class Transformer:
"""Transformer class marshals transformations.

The Transformer is similar to the ``ansiblelint.runner.Runner`` which manages
running each of the rules. We only expect there to be one ``Transformer`` instance
which should be instantiated from the main entrypoint function.
"""

def __init__(self, result: LintResult):
"""Initialize a Transformer instance."""
self.matches: List[MatchError] = result.matches
self.files: Set[Lintable] = result.files

def run(self) -> None:
"""For each file, read it, execute transforms on it, then write it."""