Replaced full showing with diff
This commit is contained in:
29
main.py
29
main.py
@@ -4,6 +4,7 @@ import subprocess
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import argparse
|
import argparse
|
||||||
|
import difflib
|
||||||
|
|
||||||
SWAPFILE = "/swapfile"
|
SWAPFILE = "/swapfile"
|
||||||
FSTAB = "/etc/fstab"
|
FSTAB = "/etc/fstab"
|
||||||
@@ -25,11 +26,18 @@ def confirm_file_change(path, new_lines):
|
|||||||
if preview or non_interactive:
|
if preview or non_interactive:
|
||||||
print(f"[preview] Would write changes to {path}")
|
print(f"[preview] Would write changes to {path}")
|
||||||
return True
|
return True
|
||||||
print(f"\n--- Preview of changes to {path} ---")
|
with open(path, "r") as f:
|
||||||
for line in new_lines:
|
old_lines = f.readlines()
|
||||||
print(line.rstrip())
|
diff = list(difflib.unified_diff(old_lines, new_lines, fromfile=path, tofile=path))
|
||||||
answer = input(f"[?] Apply changes to {path}? (y/N): ")
|
if diff:
|
||||||
return answer.lower() == "y"
|
print("\n--- Diff ---")
|
||||||
|
for line in diff:
|
||||||
|
print(line.rstrip())
|
||||||
|
answer = input(f"[?] Apply these changes to {path}? (y/N): ")
|
||||||
|
return answer.lower() == "y"
|
||||||
|
else:
|
||||||
|
print(f"[-] No changes needed for {path}.")
|
||||||
|
return False
|
||||||
|
|
||||||
def create_swapfile(size_gb):
|
def create_swapfile(size_gb):
|
||||||
print(f"[+] Creating {size_gb}G swapfile...")
|
print(f"[+] Creating {size_gb}G swapfile...")
|
||||||
@@ -44,13 +52,14 @@ def update_fstab():
|
|||||||
print(f"[preview] Would append to {FSTAB}: {SWAPFILE} none swap defaults 0 0")
|
print(f"[preview] Would append to {FSTAB}: {SWAPFILE} none swap defaults 0 0")
|
||||||
return
|
return
|
||||||
with open(FSTAB, "r") as f:
|
with open(FSTAB, "r") as f:
|
||||||
if SWAPFILE in f.read():
|
old_lines = f.readlines()
|
||||||
|
if any(SWAPFILE in line for line in old_lines):
|
||||||
print("[-] Swapfile already in fstab.")
|
print("[-] Swapfile already in fstab.")
|
||||||
return
|
return
|
||||||
new_line = f"{SWAPFILE} none swap defaults 0 0\n"
|
new_lines = old_lines + [f"{SWAPFILE} none swap defaults 0 0\n"]
|
||||||
if confirm_file_change(FSTAB, [new_line]):
|
if confirm_file_change(FSTAB, new_lines):
|
||||||
with open(FSTAB, "a") as f:
|
with open(FSTAB, "w") as f:
|
||||||
f.write(new_line)
|
f.writelines(new_lines)
|
||||||
else:
|
else:
|
||||||
print("[!] Skipped writing to fstab.")
|
print("[!] Skipped writing to fstab.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user