fix(application): exit after committing changes

This commit is contained in:
Nathaniel Landau
2023-01-22 19:53:23 +00:00
parent c427a987c1
commit 759fc3434f
4 changed files with 20 additions and 26 deletions

View File

@@ -4,7 +4,6 @@
from typing import Any
import questionary
import typer
from rich import print
from obsidian_metadata._config import Config
@@ -132,8 +131,8 @@ class Application:
if operation == "review_changes":
self.review_changes()
if operation == "commit_changes":
self.commit_changes()
if operation == "commit_changes" and self.commit_changes():
break
if operation == "abort":
break
@@ -346,25 +345,29 @@ class Application:
break
changed_notes[note_to_review].print_diff()
def commit_changes(self) -> None:
"""Write all changes to disk."""
def commit_changes(self) -> bool:
"""Write all changes to disk.
Returns:
True if changes were committed, False otherwise.
"""
changed_notes = self.vault.get_changed_notes()
if len(changed_notes) == 0:
print("\n")
alerts.notice("No changes to commit.\n")
return
return False
backup = questionary.confirm("Create backup before committing changes").ask()
if backup is None:
return
return False
if backup:
self.vault.backup()
if questionary.confirm(f"Commit {len(changed_notes)} changed files to disk?").ask():
self.vault.write()
alerts.success("Changes committed to disk. Exiting.")
typer.Exit()
alerts.success(f"{len(changed_notes)} changes committed to disk. Exiting")
return True
return
return False

View File

@@ -350,11 +350,14 @@ class Note:
self.sub(current_frontmatter, new_frontmatter)
def write(self, path: Path | None = None) -> None:
def write(self, path: Path = None) -> None:
"""Writes the note's content to disk.
Args:
path (Path): Path to write the note to. Defaults to the note's path.
Raises:
typer.Exit: If the note's path is not found.
"""
p = self.note_path if path is None else path

View File

@@ -24,7 +24,6 @@ class Vault:
vault (Path): Path to the vault.
dry_run (bool): Whether to perform a dry run.
backup_path (Path): Path to the backup of the vault.
new_vault (Path): Path to a new vault.
notes (list[Note]): List of all notes in the vault.
"""
@@ -32,7 +31,6 @@ class Vault:
self.vault_path: Path = config.vault_path
self.dry_run: bool = dry_run
self.backup_path: Path = self.vault_path.parent / f"{self.vault_path.name}.bak"
self.new_vault_path: Path = self.vault_path.parent / f"{self.vault_path.name}.new"
self.exclude_paths: list[Path] = []
self.metadata = VaultMetadata()
for p in config.exclude_paths:
@@ -60,7 +58,6 @@ class Vault:
yield "vault_path", self.vault_path
yield "dry_run", self.dry_run
yield "backup_path", self.backup_path
yield "new_vault", self.new_vault_path
yield "num_notes", self.num_notes()
yield "exclude_paths", self.exclude_paths
@@ -285,18 +282,10 @@ class Vault:
return True
return False
def write(self, new_vault: bool = False) -> None:
def write(self) -> None:
"""Write changes to the vault."""
log.debug("Writing changes to vault...")
if new_vault:
log.debug("Writing changes to backup")
if self.dry_run is False:
for _note in self.notes:
_new_note_path: Path = Path(
self.new_vault_path / Path(_note.note_path).relative_to(self.vault_path)
)
log.debug(f"writing to {_new_note_path}")
_note.write(path=_new_note_path)
else:
for _note in self.notes:
log.debug(f"writing to {_note.note_path}")
log.trace(f"writing to {_note.note_path}")
_note.write()

View File

@@ -16,7 +16,6 @@ def test_vault_creation(test_vault):
assert vault.vault_path == vault_path
assert vault.backup_path == Path(f"{vault_path}.bak")
assert vault.new_vault_path == Path(f"{vault_path}.new")
assert vault.dry_run is False
assert str(vault.exclude_paths[0]) == Regex(r".*\.git")
assert vault.num_notes() == 2