1use anyhow::{anyhow, Result};
2use std::path::Path;
3
4use crate::db;
5use crate::editor;
6use crate::ops;
7
8pub struct Opts<'a> {
9 pub status: Option<&'a str>,
10 pub priority: Option<db::Priority>,
11 pub effort: Option<db::Effort>,
12 pub title: Option<&'a str>,
13 pub desc: Option<&'a str>,
14 pub json: bool,
15}
16
17pub fn run(root: &Path, id: &str, opts: Opts) -> Result<()> {
18 let store = db::open(root)?;
19 let task_id = db::resolve_task_id(&store, id, false)?;
20
21 let parsed_status = opts.status.map(db::parse_status).transpose()?;
22
23 // If no fields were supplied, open the editor so the user can revise the
24 // task's title and description interactively.
25 let editor_title;
26 let editor_desc;
27 let (title_override, desc_override) = if opts.status.is_none()
28 && opts.priority.is_none()
29 && opts.effort.is_none()
30 && opts.title.is_none()
31 && opts.desc.is_none()
32 {
33 let interactive = std::env::var("TD_FORCE_EDITOR").is_ok()
34 || std::io::IsTerminal::is_terminal(&std::io::stdin());
35 if interactive {
36 // Load the current task so we can pre-populate the template.
37 let task = store
38 .get_task(&task_id, false)?
39 .ok_or_else(|| anyhow!("task not found"))?;
40
41 let template = format!(
42 "{}\n\
43 \n\
44 {}\n\
45 \n\
46 TD: Edit the title and description above. The first line is the\n\
47 TD: title; everything after the blank line is the description.\n\
48 TD: Lines starting with 'TD: ' will be ignored. Saving an empty\n\
49 TD: message aborts the update.",
50 task.title, task.description,
51 );
52
53 let (t, d) = editor::open(&template)?;
54 editor_title = t;
55 editor_desc = d;
56 (Some(editor_title.as_str()), Some(editor_desc.as_str()))
57 } else {
58 return Err(anyhow!(
59 "nothing to update; provide at least one flag or run interactively to open an editor"
60 ));
61 }
62 } else {
63 (opts.title, opts.desc)
64 };
65
66 let task = ops::update_task(
67 &store,
68 &task_id,
69 ops::UpdateOpts {
70 status: parsed_status,
71 priority: opts.priority,
72 effort: opts.effort,
73 title: title_override.map(String::from),
74 description: desc_override.map(String::from),
75 },
76 )?;
77
78 if opts.json {
79 println!("{}", serde_json::to_string(&task)?);
80 } else {
81 let c = crate::color::stdout_theme();
82 println!("{}updated{} {}", c.green, c.reset, task_id);
83 }
84
85 Ok(())
86}