Skip to content

Commit

Permalink
Add --write option to trigger running Transforms
Browse files Browse the repository at this point in the history
Transforms, including read/write of YAML files, will be added in a follow-up PR.
  • Loading branch information
cognifloyd committed Feb 17, 2022
1 parent 4103e7e commit 46b27dd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
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."""

0 comments on commit 46b27dd

Please sign in to comment.