Add logic to handle editor specific flags dynamically, add --version flag, edit .gitignore, add tests

This commit is contained in:
2025-11-13 16:14:17 +00:00
parent b829faf658
commit 863b1b3a21
5 changed files with 543 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
import importlib.metadata
import argparse
import tempfile
import subprocess
@@ -6,6 +7,13 @@ from pathlib import Path
import time
def get_version():
try:
return importlib.metadata.version("mirro")
except importlib.metadata.PackageNotFoundError:
return "unknown"
def read_file(path: Path) -> str:
if not path.exists():
return ""
@@ -42,7 +50,7 @@ def main():
parser = argparse.ArgumentParser(
description="Safely edit a file with automatic original backup if changed."
)
parser.add_argument("file", type=str, help="Path to file to edit")
parser.add_argument(
"--backup-dir",
type=str,
@@ -50,13 +58,37 @@ def main():
help="Backup directory",
)
args = parser.parse_args()
parser.add_argument(
"--version",
action="version",
version=f"mirro {get_version()}",
)
editor = os.environ.get("EDITOR","nano")
target = Path(args.file).expanduser().resolve()
backup_dir = Path(args.backup_dir).expanduser().resolve()
# Parse only options. Leave everything else untouched.
args, positional = parser.parse_known_args()
# Flexible positional parsing
if not positional:
parser.error("the following arguments are required: file")
file_arg = None
editor_extra = []
for p in positional:
if file_arg is None and not p.startswith("+") and not p.startswith("-"):
file_arg = p
else:
editor_extra.append(p)
if file_arg is None:
parser.error("the following arguments are required: file")
editor = os.environ.get("EDITOR", "nano")
editor_cmd = editor.split()
target = Path(file_arg).expanduser().resolve()
backup_dir = Path(args.backup_dir).expanduser().resolve()
# Permission checks
parent = target.parent
if target.exists() and not os.access(target, os.W_OK):
@@ -77,11 +109,13 @@ def main():
delete=False, prefix="mirro-", suffix=target.suffix
) as tf:
temp_path = Path(tf.name)
# Write prepopulated or original content to temp file
write_file(temp_path, original_content)
# Launch editor
subprocess.call(editor_cmd + [str(temp_path)])
if "nano" in editor_cmd[0]:
subprocess.call(editor_cmd + editor_extra + [str(temp_path)])
else:
subprocess.call(editor_cmd + [str(temp_path)] + editor_extra)
# Read edited
edited_content = read_file(temp_path)