1use anyhow::Result;
2use std::path::Path;
3
4use crate::cli::DepAction;
5use crate::db;
6use crate::ops;
7
8pub fn run(root: &Path, action: &DepAction, json: bool) -> Result<()> {
9 let store = db::open(root)?;
10
11 match action {
12 DepAction::Add { child, parent } => {
13 let child_id = db::resolve_task_id(&store, child, false)?;
14 let parent_id = db::resolve_task_id(&store, parent, false)?;
15 ops::add_dep(&store, &child_id, &parent_id)?;
16 if json {
17 println!(
18 "{}",
19 serde_json::json!({"child": child_id, "blocker": parent_id})
20 );
21 } else {
22 let c = crate::color::stdout_theme();
23 println!(
24 "{}{child_id}{} blocked by {}{parent_id}{}",
25 c.green, c.reset, c.yellow, c.reset
26 );
27 }
28 }
29 DepAction::Rm { child, parent } => {
30 let child_id = db::resolve_task_id(&store, child, false)?;
31 let parent_id = db::resolve_task_id(&store, parent, true)?;
32 ops::remove_dep(&store, &child_id, &parent_id)?;
33 if !json {
34 let c = crate::color::stdout_theme();
35 println!(
36 "{}{child_id}{} no longer blocked by {}{parent_id}{}",
37 c.green, c.reset, c.yellow, c.reset
38 );
39 }
40 }
41 DepAction::Tree { id } => {
42 let root_id = db::resolve_task_id(&store, id, true)?;
43 println!("{}", root_id);
44 let mut children: Vec<_> = store
45 .list_tasks_unfiltered()?
46 .into_iter()
47 .filter(|t| t.parent.as_ref() == Some(&root_id))
48 .map(|t| t.id)
49 .collect();
50 children.sort_by(|a, b| a.as_str().cmp(b.as_str()));
51 for child in children {
52 println!(" {child}");
53 }
54 }
55 }
56
57 Ok(())
58}