aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ts/tskm/src/interface/neorg
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/neorg')
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs31
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!");