halig/halig/commands/show.py
2023-04-01 12:37:10 +02:00

34 lines
1.1 KiB
Python

from pathlib import Path
from rich.console import Console
from rich.markdown import Markdown
from halig.commands.base import BaseCommand
from halig.encryption import Encryptor
from halig.settings import Settings
class ShowCommand(BaseCommand):
def __init__(self, note_path: Path, settings: Settings):
super().__init__(settings)
self.note_path = self.settings.notebooks_root_path / note_path
self.encryptor = Encryptor(self.settings)
self.console = Console()
if self.note_path.is_dir():
self.note_path /= f"{self.today.date()}.age"
if not self.note_path.exists():
err = f"File {self.note_path.name} does not exist"
raise ValueError(err)
if not self.note_path.name.endswith(".age"):
err = f"File {self.note_path.name} is not a valid AGE file"
raise ValueError(err)
def run(self):
with self.note_path.open("rb") as f:
data = f.read()
contents = self.encryptor.decrypt(data)
md_contents = Markdown(contents.decode())
self.console.print(md_contents)