Skip to main content

sift_queue/cli/commands/
add.rs

1use crate::queue::{Queue, Source};
2use crate::AddArgs;
3use anyhow::Result;
4use std::io::Read;
5use std::path::PathBuf;
6
7/// Execute the `sq add` command.
8pub 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    let has_source = !sources.is_empty();
61    let has_description = args.description.is_some();
62    let has_title = args.title.is_some();
63    let has_metadata = args.metadata.is_some();
64
65    if !has_source && !has_description && !has_title && !has_metadata {
66        eprintln!("Error: At least one of --description, --title, --metadata, or a source is required");
67        eprintln!("Use --diff, --file, --text, --directory, --stdin, --description, --title, or --metadata");
68        return Ok(1);
69    }
70
71    let metadata = match &args.metadata {
72        Some(json_str) => match serde_json::from_str(json_str) {
73            Ok(v) => v,
74            Err(e) => {
75                eprintln!("Error: Invalid JSON for metadata: {}", e);
76                return Ok(1);
77            }
78        },
79        None => serde_json::Value::Object(serde_json::Map::new()),
80    };
81
82    let blocked_by: Vec<String> = match &args.blocked_by {
83        Some(ids) => ids
84            .split(',')
85            .map(|s| s.trim().to_string())
86            .filter(|s| !s.is_empty())
87            .collect(),
88        None => Vec::new(),
89    };
90
91    let item = queue.push_with_description(
92        sources,
93        args.title.clone(),
94        args.description.clone(),
95        metadata,
96        None,
97        blocked_by,
98    )?;
99
100    if args.json {
101        let json = serde_json::to_string_pretty(&item.to_json_value())?;
102        println!("{}", json);
103    } else {
104        println!("{}", item.id);
105        eprintln!(
106            "Added item {} with {} source(s)",
107            item.id,
108            item.sources.len()
109        );
110    }
111
112    Ok(0)
113}