use std::collections::HashMap; use std::env::vars; use anyhow::{anyhow, Context, Result}; use pueue_lib::network::message::*; use pueue_lib::settings::*; use pueue_lib::task::{Task, TaskStatus}; use crate::helper::*; /// Create a bare AddMessage for testing. /// This is just here to minimize boilerplate code. pub fn create_add_message(shared: &Shared, command: &str) -> AddMessage { AddMessage { command: command.into(), path: shared.pueue_directory(), envs: HashMap::from_iter(vars()), start_immediately: false, stashed: false, group: PUEUE_DEFAULT_GROUP.to_string(), enqueue_at: None, dependencies: Vec::new(), priority: None, label: None, print_task_id: false, } } /// Helper to either continue the daemon or start specific tasks pub async fn start_tasks(shared: &Shared, tasks: TaskSelection) -> Result { let message = StartMessage { tasks }; send_message(shared, message) .await .context("Failed to send Start tasks message") } /// Helper to pause the default group of the daemon pub async fn pause_tasks(shared: &Shared, tasks: TaskSelection) -> Result { let message = PauseMessage { tasks, wait: false }; send_message(shared, message) .await .context("Failed to send Pause message") } /// Convenience wrapper around `get_state` if you only need a single task. pub async fn get_task(shared: &Shared, task_id: usize) -> Result { let state = get_state(shared).await?; let task = state .tasks .get(&0) .ok_or_else(|| anyhow!("Couldn't find task {task_id}"))?; Ok(task.clone()) } /// Convenience wrapper around `get_task` if you really only need the task's status. pub async fn get_task_status(shared: &Shared, task_id: usize) -> Result { let task = get_task(shared, task_id).await?; Ok(task.status) }