Skip to content

Commit

Permalink
feat. Added the -n|--nochange command line option. Closes #77.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamere-allo-peter committed Mar 23, 2022
1 parent 3abcbac commit c39ec7e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions examples/fakeansibleplaybook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ansible-playbook

# Fake ansible playbook
- name : reload daemon
systemd:
name: "{{ mydaemon }}"
state: reloaded
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion yamlfixer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions yamlfixer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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",
Expand Down
43 changes: 25 additions & 18 deletions yamlfixer/filefixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions yamlfixer/yamlfixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down

0 comments on commit c39ec7e

Please sign in to comment.