use std::io::Write;
use brush_core::{ExecutionExitCode, ExecutionResult, builtins, error};
use clap::Parser;
#[derive(Parser)]
pub(crate) struct WaitCommand {
#[arg(short = 'f')]
wait_for_terminate: bool,
#[arg(short = 'n')]
wait_for_first_or_next: bool,
#[arg(short = 'p', value_name = "VAR_NAME")]
variable_to_receive_id: Option<String>,
ids: Vec<String>,
}
impl builtins::Command for WaitCommand {
type Error = brush_core::Error;
async fn execute<SE: brush_core::ShellExtensions>(
&self,
context: brush_core::ExecutionContext<'_, SE>,
) -> Result<ExecutionResult, Self::Error> {
if self.wait_for_terminate {
return error::unimp("wait -f");
}
if self.wait_for_first_or_next {
return error::unimp("wait -n");
}
if self.variable_to_receive_id.is_some() {
return error::unimp("wait -p");
}
let mut result = ExecutionResult::success();
if !self.ids.is_empty() {
for id in &self.ids {
if id.starts_with('%') {
if let Some(job) = context.shell.jobs_mut().resolve_job_spec(id) {
job.wait().await?;
} else {
writeln!(context.stderr(), "{}: no such job: {}", context.command_name, id)?;
result = ExecutionExitCode::GeneralError.into();
}
} else {
return error::unimp("wait with process IDs");
}
}
} else {
let jobs = context.shell.jobs_mut().wait_all().await?;
if context.shell.options().enable_job_control {
for job in jobs {
writeln!(context.stdout(), "{job}")?;
}
}
}
Ok(result)
}
}