halig/halig/commands/reencrypt.py
cătălin f611fa5f2a
Some checks failed
checks / tests (push) Waiting to run
checks / pre-commit (push) Failing after 1m30s
ci: add pre-commit and tests workflows
2024-08-04 10:54:59 +02:00

26 lines
974 B
Python

import pyrage
from rich import print
from halig.commands.base import BaseCommand
from halig.encryption import Encryptor
from halig.settings import Settings
class ReencryptCommand(BaseCommand):
def __init__(self, settings: Settings, *args, **kwargs):
super().__init__(settings, *args, **kwargs)
self.encryptor = Encryptor(settings)
def run(self):
for note_path in self.traverse():
try:
with note_path.open("rb") as fr:
orig_data = self.encryptor.decrypt(fr.read())
new_data = self.encryptor.encrypt(orig_data)
with note_path.open("wb") as fw:
fw.write(new_data)
except pyrage.DecryptError: # type: ignore[reportGeneralTypeIssues] # noqa: PERF203
print(
f"[yellow] Could not reencrypt {note_path} because no matching keys"
f" were found, skipping ...",
)