Skip to content

Commit

Permalink
fix: edit existing note instead of returning an error when trying to …
Browse files Browse the repository at this point in the history
…create a note that already exists

Change the note creation flow to check for the existence of a note file at the given path before attempting file creation. If the file exists, edit the note instead of returning an error to the user.

Fixes #2
  • Loading branch information
dogue committed Nov 7, 2023
1 parent 33aba20 commit 42c1da5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ pub fn move_file(note_path: &NotePath, new_path: &NotePath) -> anyhow::Result<()
Ok(())
}

// TODO: LSP claims unused. Remove it.
#[allow(dead_code)]
pub fn file_exists(note_path: &NotePath) -> bool {
pub fn exists(note_path: &NotePath) -> bool {
let path = note_path.absolute_path_with_ext();
let path = Path::new(&path);

Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;

use jottem::{
cli::{Cli, Command},
file,
path::NotePath,
utils,
};
Expand All @@ -14,8 +15,12 @@ fn main() -> anyhow::Result<()> {
match cli.command {
Command::Create { path, tags } => {
let path = NotePath::parse(&path)?;
let note = utils::create_note(&path, &tags)?;
utils::open_note(&note.absolute_path)?;
if file::exists(&path) {
jottem::edit_note(Some(path.relative_path()))?;
} else {
let note = utils::create_note(&path, &tags)?;
utils::open_note(&note.absolute_path)?;
}
}
Command::Edit { path } => jottem::edit_note(path)?,
Command::Find { args } => jottem::find_notes(&args)?,
Expand Down

0 comments on commit 42c1da5

Please sign in to comment.