about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
blob: 577de02c39407c9878b0da63dd2ce299410f7d35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{
    env,
    fs::{self, read_to_string, File, OpenOptions},
    io::Write,
    process::Command,
};

use anyhow::{bail, Context, Result};

use crate::{cli::NeorgCommand, state::State};

pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> {
    match command {
        NeorgCommand::Task { id } => {
            let project = id.project(state)?;
            let path = dirs::data_local_dir()
                .expect("This should exists")
                .join("tskm/notes")
                .join(project.get_neorg_path()?);

            fs::create_dir_all(path.parent().expect("This should exist"))?;

            {
                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(false);

                    let mut file = options.open(&path)?;
                    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()))?;
                }
            }

            let editor = env::var("EDITOR").unwrap_or("nvim".to_owned());
            let status = Command::new(editor)
                .args([
                    path.to_str().expect("Should be a utf-8 str"),
                    "-c",
                    format!("/% {}", id.uuid()).as_str(),
                ])
                .status()?;
            if !status.success() {
                bail!("$EDITOR fail with error code: {status}");
            }

            {
                let status = Command::new("git")
                    .args(["add", "."])
                    .current_dir(path.parent().expect("Will exist"))
                    .status()?;
                if !status.success() {
                    bail!("Git add . failed!");
                }

                let status = Command::new("git")
                    .args([
                        "commit",
                        "--message",
                        format!(
                            "chore({}): Update",
                            path.parent().expect("Should have a parent").display()
                        )
                        .as_str(),
                        "--no-gpg-sign",
                    ])
                    .current_dir(path.parent().expect("Will exist"))
                    .status()?;
                if !status.success() {
                    bail!("Git commit failed!");
                }
            }

            {
                id.mark_neorg_data(state)?;
            }
        }
    }
    Ok(())
}