diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-04-06 18:39:14 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-04-06 18:39:14 +0200 |
commit | b542620286f2407e821c382b5fd22fe263006b39 (patch) | |
tree | 0757fd8ad4abe70a53b3ebdb3b2291e13e658fc7 /pkgs/by-name/ts/tskm/src/interface | |
parent | chore(pkgs/tskm): Correctly allow missing panics_doc and errors doc (diff) | |
download | nixos-config-b542620286f2407e821c382b5fd22fe263006b39.zip |
chore(pkgs/tskm/neorg): Handle file operations correctly
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface')
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs index 45e1f916..577de02c 100644 --- a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs @@ -1,11 +1,11 @@ use std::{ env, - fs::{self, read_to_string, OpenOptions}, + fs::{self, read_to_string, File, OpenOptions}, io::Write, process::Command, }; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use crate::{cli::NeorgCommand, state::State}; @@ -15,19 +15,32 @@ pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { let project = id.project(state)?; let path = dirs::data_local_dir() .expect("This should exists") - .join("notes") + .join("tskm/notes") .join(project.get_neorg_path()?); fs::create_dir_all(path.parent().expect("This should exist"))?; { - let contents = read_to_string(&path)?; - if contents.contains(format!("% {}", id.to_uuid()?).as_str()) { + let contents = if path.exists() { + read_to_string(&path) + .with_context(|| format!("Failed to read file: '{}'", path.display()))? + } else { + File::create(&path) + .with_context(|| format!("Failed to create file: '{}'", path.display()))?; + String::new() + }; + + if !contents.contains(format!("% {}", id.uuid()).as_str()) { let mut options = OpenOptions::new(); - options.append(true).create(true); + options.append(true).create(false); let mut file = options.open(&path)?; - file.write_all(format!("* TITLE (% {})", id.to_uuid()?).as_bytes())?; + file.write_all(format!("* TITLE (% {})", id.uuid()).as_bytes()) + .with_context(|| { + format!("Failed to write task uuid to file: '{}'", path.display()) + })?; + file.flush() + .with_context(|| format!("Failed to flush file: '{}'", path.display()))?; } } @@ -46,7 +59,7 @@ pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { { let status = Command::new("git") .args(["add", "."]) - .current_dir(&path) + .current_dir(path.parent().expect("Will exist")) .status()?; if !status.success() { bail!("Git add . failed!"); @@ -63,7 +76,7 @@ pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { .as_str(), "--no-gpg-sign", ]) - .current_dir(&path) + .current_dir(path.parent().expect("Will exist")) .status()?; if !status.success() { bail!("Git commit failed!"); |