log.rs

 1use anyhow::{bail, Result};
 2use std::path::Path;
 3
 4use crate::db;
 5
 6pub fn run(root: &Path, id: &str, message: &str, json: bool) -> Result<()> {
 7    let conn = db::open(root)?;
 8
 9    if !db::task_exists(&conn, id)? {
10        bail!("task {id} not found");
11    }
12
13    let timestamp = db::now_utc();
14    conn.execute(
15        "INSERT INTO task_logs (task_id, timestamp, body)
16         VALUES (?1, ?2, ?3)",
17        rusqlite::params![id, timestamp, message],
18    )?;
19    let log_id = conn.last_insert_rowid();
20    conn.execute(
21        "UPDATE tasks SET updated = ?1 WHERE id = ?2",
22        rusqlite::params![db::now_utc(), id],
23    )?;
24
25    let entry = db::LogEntry {
26        id: log_id,
27        task_id: id.to_string(),
28        timestamp,
29        body: message.to_string(),
30    };
31
32    if json {
33        println!("{}", serde_json::to_string(&entry)?);
34    } else {
35        println!("logged to {id}");
36    }
37
38    Ok(())
39}