kimun_notes/cli/commands/
journal.rs1use clap::Subcommand;
6use color_eyre::eyre::Result;
7use kimun_core::{NoteVault, nfs::VaultPath};
8
9use crate::cli::output::OutputFormat;
10
11#[derive(clap::Args, Debug)]
12pub struct JournalArgs {
13 #[arg(long)]
15 pub date: Option<String>,
16 pub content: Option<String>,
18 #[command(subcommand)]
19 pub subcommand: Option<JournalSubcommand>,
20}
21
22#[derive(Subcommand, Debug)]
23pub enum JournalSubcommand {
24 Show {
26 #[arg(long)]
28 date: Option<String>,
29 #[arg(long, value_enum, default_value = "text")]
31 format: OutputFormat,
32 },
33}
34
35pub async fn run(args: JournalArgs, vault: &NoteVault, workspace_name: &str) -> Result<()> {
36 match args.subcommand {
37 Some(JournalSubcommand::Show { date, format }) => {
38 run_show(vault, date.as_deref(), format, workspace_name).await
39 }
40 None => run_append(vault, args.date.as_deref(), args.content).await,
41 }
42}
43
44fn resolve_date(date: Option<&str>) -> Result<String> {
46 match date {
47 None => Ok(chrono::Utc::now().format("%Y-%m-%d").to_string()),
48 Some(d) => {
49 chrono::NaiveDate::parse_from_str(d, "%Y-%m-%d").map_err(|_| {
50 color_eyre::eyre::eyre!("Invalid date '{}' — expected format YYYY-MM-DD", d)
51 })?;
52 Ok(d.to_string())
53 }
54 }
55}
56
57fn journal_entry_path(vault: &NoteVault, date_str: &str) -> VaultPath {
59 vault
60 .journal_path()
61 .append(&VaultPath::note_path_from(date_str))
62 .absolute()
63}
64
65async fn run_append(vault: &NoteVault, date: Option<&str>, content: Option<String>) -> Result<()> {
66 use crate::cli::helpers::resolve_content;
67
68 let text = resolve_content(content)?;
69 if text.is_empty() {
70 return Ok(());
71 }
72
73 let (vault_path, existing) = match date {
74 None => {
75 let (details, existing) = vault
77 .journal_entry()
78 .await
79 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?;
80 (details.path, existing)
81 }
82 Some(d) => {
83 let date_str = resolve_date(Some(d))?;
84 let vault_path = journal_entry_path(vault, &date_str);
85 let existing = vault
86 .load_or_create_note(&vault_path, Some(format!("# {}\n\n", date_str)))
87 .await
88 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?;
89 (vault_path, existing)
90 }
91 };
92
93 let combined = format!("{}\n{}", existing, text);
94 vault
95 .save_note(&vault_path, &combined)
96 .await
97 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?;
98
99 println!("Note saved: {}", vault_path);
100 Ok(())
101}
102
103async fn run_show(
104 vault: &NoteVault,
105 date: Option<&str>,
106 format: OutputFormat,
107 workspace_name: &str,
108) -> Result<()> {
109 use crate::cli::commands::note_ops::format_note_show_text;
110 use crate::cli::json_output::{JsonNoteEntry, JsonNoteMetadata, JsonOutput, JsonOutputMetadata};
111 use crate::cli::metadata_extractor::{extract_tags, extract_links, extract_headers};
112 use kimun_core::error::{VaultError, FSError};
113 use chrono::Utc;
114 use std::time::UNIX_EPOCH;
115
116 if matches!(format, OutputFormat::Paths) {
117 return Err(color_eyre::eyre::eyre!(
118 "--format paths is not valid for journal show; use 'text' or 'json'"
119 ));
120 }
121
122 let date_str = resolve_date(date)?;
123 let vault_path = journal_entry_path(vault, &date_str);
124
125 let note_details = vault.load_note(&vault_path).await.map_err(|e| match e {
126 VaultError::FSError(FSError::VaultPathNotFound { .. }) => {
127 color_eyre::eyre::eyre!("No journal entry found for {}", date_str)
128 }
129 _ => color_eyre::eyre::eyre!("{}", e),
130 })?;
131
132 let content = ¬e_details.raw_text;
133 let content_data = note_details.get_content_data();
134
135 let backlinks = vault
136 .get_backlinks(&vault_path)
137 .await
138 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?;
139 let backlink_paths: Vec<String> = backlinks.iter().map(|(e, _)| e.path.to_string()).collect();
140
141 match format {
142 OutputFormat::Text => {
143 let tags = extract_tags(content);
144 let links = extract_links(content);
145 print!(
146 "{}",
147 format_note_show_text(
148 &vault_path,
149 content,
150 &content_data.title,
151 &tags,
152 &links,
153 &backlink_paths,
154 )
155 );
156 }
157 OutputFormat::Json => {
158 let meta = tokio::fs::metadata(vault.path_to_pathbuf(&vault_path))
159 .await
160 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?;
161 let modified_secs = meta
162 .modified()
163 .map(|t| t.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs())
164 .unwrap_or(0);
165 let tags = extract_tags(content);
166 let links = extract_links(content);
167 let headers = extract_headers(content);
168 let journal_date = vault
169 .journal_date(&vault_path)
170 .map(|d| d.format("%Y-%m-%d").to_string());
171 let entry = JsonNoteEntry {
172 path: vault_path.to_string_with_ext(),
173 title: content_data.title.clone(),
174 content: content.clone(),
175 size: meta.len(),
176 modified: modified_secs,
177 created: modified_secs,
178 hash: format!("{:x}", content_data.hash),
179 journal_date,
180 metadata: JsonNoteMetadata { tags, links, headers },
181 backlinks: if backlink_paths.is_empty() {
182 None
183 } else {
184 Some(backlink_paths)
185 },
186 };
187 let output = JsonOutput {
188 metadata: JsonOutputMetadata {
189 workspace: workspace_name.to_string(),
190 workspace_path: vault.workspace_path.to_string_lossy().to_string(),
191 total_results: 1,
192 query: None,
193 is_listing: false,
194 generated_at: Utc::now().to_rfc3339(),
195 },
196 notes: vec![entry],
197 };
198 print!(
199 "{}",
200 serde_json::to_string(&output)
201 .map_err(|e| color_eyre::eyre::eyre!("{}", e))?
202 );
203 }
204 OutputFormat::Paths => unreachable!("guarded above"),
205 }
206
207 Ok(())
208}