diff --git a/src/mirro/main.py b/src/mirro/main.py index f5b8c96..61c6367 100644 --- a/src/mirro/main.py +++ b/src/mirro/main.py @@ -2,7 +2,6 @@ import argparse import tempfile import subprocess -import shutil import os from pathlib import Path import time @@ -14,10 +13,14 @@ def read_file(path: Path) -> str: return "" return path.read_text(encoding="utf-8", errors="replace") + def write_file(path: Path, content: str): path.write_text(content, encoding="utf-8") -def backup_original(original_path: Path, original_content: str, backup_dir: Path) -> Path: + +def backup_original( + original_path: Path, original_content: str, backup_dir: Path +) -> Path: backup_dir.mkdir(parents=True, exist_ok=True) timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime()) uid = uuid.uuid4().hex[:8] @@ -26,11 +29,24 @@ def backup_original(original_path: Path, original_content: str, backup_dir: Path backup_path.write_text(original_content, encoding="utf-8") return backup_path + def main(): - parser = argparse.ArgumentParser(description="Safely edit a file with automatic original backup if changed.") + 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, default=str(Path.home() / ".local/share/mirro"), help="Backup directory") - parser.add_argument("--editor", type=str, default=os.environ.get("EDITOR", "nano"), help="Editor to use") + parser.add_argument( + "--backup-dir", + type=str, + default=str(Path.home() / ".local/share/mirro"), + help="Backup directory", + ) + parser.add_argument( + "--editor", + type=str, + default=os.environ.get("EDITOR", "nano"), + help="Editor to use", + ) args = parser.parse_args() @@ -38,24 +54,32 @@ def main(): backup_dir = Path(args.backup_dir).expanduser().resolve() editor_cmd = args.editor.split() + # Permission checks + parent = target.parent + if target.exists() and not os.access(target, os.W_OK): + print(f"Need elevated privileges to open {target}") + return 1 + if not target.exists() and not os.access(parent, os.W_OK): + print(f"Need elevated privileges to create {target}") + return 1 + # Read original original_content = read_file(target) # Temp file for editing - with tempfile.NamedTemporaryFile(delete=False, prefix="mirro-", suffix=target.suffix) as tf: + with tempfile.NamedTemporaryFile( + delete=False, prefix="mirro-", suffix=target.suffix + ) as tf: temp_path = Path(tf.name) write_file(temp_path, original_content) - # Launch editor subprocess.call(editor_cmd + [str(temp_path)]) - # Read edited edited_content = read_file(temp_path) temp_path.unlink(missing_ok=True) - if edited_content == original_content: print("file hasn't changed") return @@ -67,5 +91,6 @@ def main(): # Overwrite target target.write_text(edited_content, encoding="utf-8") + if __name__ == "__main__": - main() \ No newline at end of file + main()