From c39ec7e4545dfb057234b39a563259418ce0c8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Alet?= Date: Wed, 23 Mar 2022 11:17:00 +1100 Subject: [PATCH] feat. Added the -n|--nochange command line option. Closes #77. --- README.md | 1 + examples/fakeansibleplaybook.yml | 7 ++++++ setup.cfg | 2 +- yamlfixer/__init__.py | 2 +- yamlfixer/__main__.py | 10 +++++--- yamlfixer/filefixer.py | 43 +++++++++++++++++++------------- yamlfixer/yamlfixer.py | 7 ++++++ 7 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 examples/fakeansibleplaybook.yml diff --git a/README.md b/README.md index 22faf4a..54383f9 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ optional arguments: the default. -d, --debug output debug information to stderr. -l, --listfixers output the list of available fixers. + -n, --nochange don't modify anything. -j, --jsonsummary output JSON summary to stderr. -p, --plainsummary output plain text summary to stderr. -s, --summary output colored plain text summary to stderr. diff --git a/examples/fakeansibleplaybook.yml b/examples/fakeansibleplaybook.yml new file mode 100644 index 0000000..59c7717 --- /dev/null +++ b/examples/fakeansibleplaybook.yml @@ -0,0 +1,7 @@ +#!/usr/bin/ansible-playbook + +# Fake ansible playbook +- name : reload daemon + systemd: + name: "{{ mydaemon }}" + state: reloaded \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 6bb37a2..cafdd8d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [metadata] -version = 0.4.4 +version = 0.4.5 name = yamlfixer-opt-nc description = automates the fixing of problems reported by yamllint long_description = file: README.md diff --git a/yamlfixer/__init__.py b/yamlfixer/__init__.py index 4dd1002..bed9260 100755 --- a/yamlfixer/__init__.py +++ b/yamlfixer/__init__.py @@ -19,7 +19,7 @@ import time -__version__ = "0.4.4" +__version__ = "0.4.5" __author__ = "OPT-NC" __license__ = "GPLv3+" __copyright__ = "Copyright (C) 2021-%s %s" % (time.strftime("%Y", diff --git a/yamlfixer/__main__.py b/yamlfixer/__main__.py index 966134d..70c5caa 100755 --- a/yamlfixer/__main__.py +++ b/yamlfixer/__main__.py @@ -54,9 +54,10 @@ def run(): action="version", version=f"yamlfixer v{__version__}", help="display this program's version number and exit.") - cmdline.add_argument("-b", "--backup", - action="store_true", - help="make a backup copy of original files.") + mutuallyexclusive = cmdline.add_mutually_exclusive_group() + mutuallyexclusive.add_argument("-b", "--backup", + action="store_true", + help="make a backup copy of original files.") cmdline.add_argument("-B", "--backupsuffix", default=".orig", help="sets the suffix for backup files, `%(default)s` is the default.") @@ -66,6 +67,9 @@ def run(): cmdline.add_argument("-l", "--listfixers", action="store_true", help="output the list of available fixers.") + mutuallyexclusive.add_argument("-n", "--nochange", + action="store_true", + help="don't modify anything.") mutuallyexclusive = cmdline.add_mutually_exclusive_group() mutuallyexclusive.add_argument("-j", "--jsonsummary", action="store_true", diff --git a/yamlfixer/filefixer.py b/yamlfixer/filefixer.py index 5e7dd3f..0209c2f 100755 --- a/yamlfixer/filefixer.py +++ b/yamlfixer/filefixer.py @@ -102,24 +102,31 @@ def dump(self, outcontents): retcode = FIX_SKIPPED else: retcode = FIX_MODIFIED - if self.filename == '-': # Always dump to stdout in this case - sys.stdout.write(self.shebang + (outcontents or '')) - sys.stdout.flush() - elif retcode == FIX_MODIFIED: # Don't write unnecessarily - try: - if self.yfixer.arguments.backup: # pylint: disable=no-member - # Try to make a backup of the original file - try: - os.replace(self.filename, - f"{self.filename}{self.yfixer.arguments.backupsuffix}") - except PermissionError as msg: - self.yfixer.error(f"impossible to create a backup : {msg}") - # Overwrite the original file with the new contents - with open(self.filename, 'w') as yamlfile: - yamlfile.write(self.shebang + (outcontents or '')) - except PermissionError as msg: - self.yfixer.error(f"impossible to save fixed contents : {msg}") - retcode = FIX_PERMERROR + if self.yfixer.arguments.nochange: + # We don't want to modify anything + if self.filename == '-': # Always dump original input to stdout in this case + sys.stdout.write(self.shebang + (self.incontents or '')) + sys.stdout.flush() + else: + # It seems we really want to fix things. + if self.filename == '-': # Always dump to stdout in this case + sys.stdout.write(self.shebang + (outcontents or '')) + sys.stdout.flush() + elif retcode == FIX_MODIFIED: # Don't write unnecessarily + try: + if self.yfixer.arguments.backup: # pylint: disable=no-member + # Try to make a backup of the original file + try: + os.replace(self.filename, + f"{self.filename}{self.yfixer.arguments.backupsuffix}") + except PermissionError as msg: + self.yfixer.error(f"impossible to create a backup : {msg}") + # Overwrite the original file with the new contents + with open(self.filename, 'w') as yamlfile: + yamlfile.write(self.shebang + (outcontents or '')) + except PermissionError as msg: + self.yfixer.error(f"impossible to save fixed contents : {msg}") + retcode = FIX_PERMERROR return retcode def fix(self): diff --git a/yamlfixer/yamlfixer.py b/yamlfixer/yamlfixer.py index 0808449..7ce5034 100755 --- a/yamlfixer/yamlfixer.py +++ b/yamlfixer/yamlfixer.py @@ -73,6 +73,12 @@ def statistics(self): if self.arguments.summary and sys.stderr.isatty(): status = f"\033[{COLORSEQ.get(status.strip(), '0m')}{status}\033[0m" self.info(f"{status} {filename}{msg}") + if self.arguments.nochange: + message = "WARNING: No file was modified per user's request !" + if self.arguments.summary and sys.stderr.isatty(): + self.info(f"\033[38;2;255;0;0m{message}\033[0m") + else: + self.info(f"{message}") elif self.arguments.jsonsummary: summarymapping = {"filestofix": len(self.arguments.filenames), "passedstrictmode": self.passed, @@ -81,6 +87,7 @@ def statistics(self): "notwriteable": self.permerrors, "unknown": self.unknown, "details": {}, + "nochangemode": self.arguments.nochange, } for (status, filename, issues, handled) in self.summary: summarymapping["details"][filename] = {"status": status.strip(),