sift_queue/cli/commands/
add.rs1use crate::queue::{Queue, Source};
2use crate::AddArgs;
3use anyhow::Result;
4use std::io::Read;
5use std::path::PathBuf;
6
7pub fn execute(args: &AddArgs, queue_path: PathBuf) -> Result<i32> {
9 let queue = Queue::new(queue_path);
10
11 let mut sources: Vec<Source> = Vec::new();
12
13 for path in &args.diff {
14 sources.push(Source {
15 type_: "diff".to_string(),
16 path: Some(path.clone()),
17 content: None,
18 session_id: None,
19 });
20 }
21
22 for path in &args.file {
23 sources.push(Source {
24 type_: "file".to_string(),
25 path: Some(path.clone()),
26 content: None,
27 session_id: None,
28 });
29 }
30
31 for text in &args.text {
32 sources.push(Source {
33 type_: "text".to_string(),
34 path: None,
35 content: Some(text.clone()),
36 session_id: None,
37 });
38 }
39
40 for path in &args.directory {
41 sources.push(Source {
42 type_: "directory".to_string(),
43 path: Some(path.clone()),
44 content: None,
45 session_id: None,
46 });
47 }
48
49 if let Some(ref stdin_type) = args.stdin {
50 let mut content = String::new();
51 std::io::stdin().read_to_string(&mut content)?;
52 sources.push(Source {
53 type_: stdin_type.clone(),
54 path: None,
55 content: Some(content),
56 session_id: None,
57 });
58 }
59
60 if sources.is_empty() {
61 eprintln!("Error: At least one source is required");
62 eprintln!("Use --diff, --file, --text, --directory, or --stdin");
63 return Ok(1);
64 }
65
66 let metadata = match &args.metadata {
67 Some(json_str) => match serde_json::from_str(json_str) {
68 Ok(v) => v,
69 Err(e) => {
70 eprintln!("Error: Invalid JSON for metadata: {}", e);
71 return Ok(1);
72 }
73 },
74 None => serde_json::Value::Object(serde_json::Map::new()),
75 };
76
77 let blocked_by: Vec<String> = match &args.blocked_by {
78 Some(ids) => ids
79 .split(',')
80 .map(|s| s.trim().to_string())
81 .filter(|s| !s.is_empty())
82 .collect(),
83 None => Vec::new(),
84 };
85
86 let item = queue.push(sources, args.title.clone(), metadata, None, blocked_by)?;
87 println!("{}", item.id);
88 eprintln!(
89 "Added item {} with {} source(s)",
90 item.id,
91 item.sources.len()
92 );
93 Ok(0)
94}