about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs')
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs51
1 files changed, 30 insertions, 21 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 a9a46ee7..d904b12e 100644
--- a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
+++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
@@ -1,33 +1,46 @@
 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;
+use crate::{cli::NeorgCommand, state::State};
 
-pub fn handle(command: NeorgCommand) -> Result<()> {
+pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> {
     match command {
         NeorgCommand::Task { id } => {
-            let project = id.project()?;
-            let path = dirs::data_local_dir()
+            let project = id.project(state)?;
+            let base = dirs::data_local_dir()
                 .expect("This should exists")
-                .join("notes")
-                .join(project.get_neorg_path()?);
+                .join("tskm/notes");
+            let path = base.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()))?;
                 }
             }
 
@@ -36,7 +49,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
                 .args([
                     path.to_str().expect("Should be a utf-8 str"),
                     "-c",
-                    format!("/% {}", id.to_uuid()?).as_str(),
+                    format!("/% {}", id.uuid()).as_str(),
                 ])
                 .status()?;
             if !status.success() {
@@ -46,7 +59,7 @@ pub fn handle(command: NeorgCommand) -> 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!");
@@ -56,14 +69,10 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
                     .args([
                         "commit",
                         "--message",
-                        format!(
-                            "chore({}): Update",
-                            path.parent().expect("Should have a parent").display()
-                        )
-                        .as_str(),
+                        format!("chore({}): Update", project.get_neorg_path()?.display()).as_str(),
                         "--no-gpg-sign",
                     ])
-                    .current_dir(&path)
+                    .current_dir(path.parent().expect("Will exist"))
                     .status()?;
                 if !status.success() {
                     bail!("Git commit failed!");
@@ -71,7 +80,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
             }
 
             {
-                id.annotate("[neorg data]")?;
+                id.mark_neorg_data(state)?;
             }
         }
     }