Skip to main content

sift_queue/cli/commands/
status.rs

1use crate::cli::help::{HelpDoc, HelpSection};
2use crate::queue::{Queue, UpdateAttrs};
3use crate::StatusArgs;
4use anyhow::Result;
5use clap::builder::{StyledStr, Styles};
6use std::path::PathBuf;
7
8pub fn close_after_help(styles: &Styles) -> StyledStr {
9    HelpDoc::new()
10        .section(
11            HelpSection::new("Behavior:")
12                .item(
13                    "sq close <id>",
14                    "Keep an item in history with status closed",
15                )
16                .item(
17                    "sq close <id> --json",
18                    "Return the updated item payload as JSON",
19                ),
20        )
21        .render(styles)
22}
23
24pub fn execute(args: &StatusArgs, queue_path: PathBuf, status: &str) -> Result<i32> {
25    let queue = Queue::new(queue_path);
26
27    let id = match &args.id {
28        Some(id) => id.as_str(),
29        None => {
30            eprintln!("Error: Item ID is required");
31            return Ok(1);
32        }
33    };
34
35    let existing = match queue.find(id) {
36        Some(item) => item,
37        None => {
38            eprintln!("Error: Item not found: {}", id);
39            return Ok(1);
40        }
41    };
42
43    if existing.status == status {
44        let existing_id = existing.id.clone();
45        if args.json {
46            let existing = queue.item_with_computed_status(existing);
47            let json = serde_json::to_string_pretty(&existing.to_json_value())?;
48            println!("{}", json);
49        } else {
50            println!("{}", existing_id);
51        }
52        eprintln!("Item {} is already {}", existing_id, status);
53        return Ok(0);
54    }
55
56    let attrs = UpdateAttrs {
57        status: Some(status.to_string()),
58        ..Default::default()
59    };
60
61    match queue.update(id, attrs)? {
62        Some(updated) => {
63            if args.json {
64                let updated = queue.item_with_computed_status(updated);
65                let json = serde_json::to_string_pretty(&updated.to_json_value())?;
66                println!("{}", json);
67            } else {
68                println!("{}", updated.id);
69                eprintln!("Updated item {}", updated.id);
70            }
71
72            Ok(0)
73        }
74        None => {
75            eprintln!("Error: Item not found: {}", id);
76            Ok(1)
77        }
78    }
79}